Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(customPropTypes): false demand() errors #411

Merged
merged 2 commits into from
Aug 21, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/lib/customPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ export const some = (validators) => {
*/
export const givenProps = (propsShape, validator) => {
return (props, propName, componentName, ...rest) => {
// only validate if all propsShape validators pass
if (!_.every(propsShape, (val, key) => !val(props, key, componentName, ...rest))) return
const shouldValidate = _.every(propsShape, (val, key) => {
// require propShape validators to pass or prop values to match
return _.isFunction(val) ? !val(props, key, componentName, ...rest) : val === props[propName]
})
Copy link
Member Author

@levithomason levithomason Aug 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support defining giveProps by value instead of by PropType function only:

control: customPropTypes.givenProps(
  { control: 'input' },
  customPropTypes.disallow(['children'])
),


if (!shouldValidate) return

if (!_.isPlainObject(propsShape)) {
throw new Error(
Expand Down Expand Up @@ -170,9 +174,9 @@ export const demand = (requiredProps) => {
}

// do not require requiredProps if the prop does not exist in props
if (!_.has(props, propName)) return
if (_.isUndefined(props, propName)) return

const missingRequired = requiredProps.filter(required => !_.has(props, required))
const missingRequired = requiredProps.filter(required => _.isUndefined(props, required))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't demand props when the prop at hand is undefined. React treats undefined props as not present. Previously, _.has here was demanding props when the prop implementing demand() was undefined.

if (!_.isEmpty(missingRequired)) {
return new Error(
`\`${componentName}\` prop \`${propName}\` requires props: \`${missingRequired.join('`, `')}\`.`,
Expand Down