-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathisRequiredIf.js
60 lines (47 loc) · 1.78 KB
/
isRequiredIf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const VALIDATOR_ARG_ERROR_MESSAGE =
'The typeValidator argument must be a function ' +
'with the signature function(props, propName, componentName).';
const MESSAGE_ARG_ERROR_MESSAGE =
'The error message is optional, but must be a string if provided.';
const propIsRequired = (condition, props, propName, componentName) => {
if (typeof condition === 'boolean') {
return condition;
} else if (typeof condition === 'function') {
return condition(props, propName, componentName);
} else if (Boolean(condition) === true) {
return Boolean(condition);
}
return false;
};
const propExists = (props, propName) => Object.hasOwnProperty.call(props, propName);
const missingPropError = (props, propName, componentName, message) => {
if (message) {
return new Error(message);
}
return new Error(
`Required ${props[propName]} \`${propName}\`` +
` was not specified in \`${componentName}\`.`,
);
};
const guardAgainstInvalidArgTypes = (typeValidator, message) => {
if (typeof typeValidator !== 'function') {
throw new TypeError(VALIDATOR_ARG_ERROR_MESSAGE);
}
if (Boolean(message) && typeof message !== 'string') {
throw new TypeError(MESSAGE_ARG_ERROR_MESSAGE);
}
};
const isRequiredIf = (typeValidator, condition, message) => {
guardAgainstInvalidArgTypes(typeValidator, message);
return (props, propName, componentName, ...rest) => {
if (propIsRequired(condition, props, propName, componentName)) {
if (propExists(props, propName)) {
return typeValidator(props, propName, componentName, ...rest);
}
return missingPropError(props, propName, componentName, message);
}
// Is not required, so just run typeValidator.
return typeValidator(props, propName, componentName, ...rest);
};
};
export default isRequiredIf;