Created
August 6, 2019 15:14
-
-
Save dmmulroy/91ae886d81b49c050e96dfe225d5e2e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const withIsRequired = validatorFn => { | |
const _customPropValidator = (isRequired = false) => (props, propName, componentName) => { | |
const value = props[propName]; | |
if (value == null) { | |
if (isRequired) { | |
return new Error(`Required \`${propName}\` was not specified in \`${componentName}\`.`); | |
} | |
return undefined; | |
} | |
return validatorFn(props, propName, componentName); | |
}; | |
| |
const customPropValidator = _customPropValidator(); | |
customPropValidator.isRequired = _customPropValidator(true); | |
| |
return customPropValidator; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: