I started out with a very simple PropTypes validation:
name: PropTypes.string.isRequired,
Now I need to only have string be required if a certain other condition is false, so:
name: ({name, otherProp}) => {
if (otherProp === 'foo') return null;
if (typeof name === 'string') return null;
return new Error('Error');
}
Which is fine in this case, but what I'd really like to be able to do after the first check is run the regular PropTypes check for string.isRequired
inside my custom validator, like maybe:
name: ({name, otherProp}) => {
if (otherProp === 'foo') return null;
return PropTypes.string.isRequired;
}
It seems like there should be a way to do this with PropTypes.checkPropTypes
, but I can't figure it out.
I discovered while poking around the code for Material UI a clever way to do something equivalent to this: parallel prop-types checks. They have a very simple utility function chainPropTypes which goes like this:
You use this to do a custom prop types check along with a standard check. For example like this: