-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Added 'objectOf' PropType validator to iterate on objects and validate properties. #1611
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically you want me to wrap it with if (propValue.hasOwnProperty(key)) for each iteration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind adding it to be on the "safe side", especially if it's already the approach. Prop validation is dev-only anyway so that's negligible overhead.
|
It seems like a sound idea and the code looks great to me. |
|
Interestingly enough, we just had somebody work on almost exactly this on Friday internally (hi @dschafer!). I wasn't sure if we really wanted to support it in core since it's a bit specialized, but perhaps I was wrong... |
|
We also wanted this at KA two weeks ago. For naming, I was thinking perhaps |
|
👍 |
|
The version we had internally used the name I don't have any code comments, so I'm going to go ahead and pull this in. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: please remove trailing whitespace here
validate properties.
|
IMO, using objects as dictionaries should be highly discouraged. You should be using an ES6 Map and we could add an mapOf function. |
|
@sebmarkbage no one is going to be able to use ES6 Map in a long time. I think that it's worth supporting objectOf in the meantime. |
|
@vjeux We'll require a shim of ES6 Map in React core soon anyway. So everyone will be able to use it. |
|
Internally merged. We can have both! |
Added 'objectOf' PropType validator to iterate on objects and validate properties.
The rationale behind this is when you have object literals used as dictionaries and you want to validate each property (without knowing its name ahead of time). PropTypes.shape allows to validate specific properties, but this rather iterates on the properties of an object (no matter their name) and validates their content (which then can be any of the PropTypes). It works very similarly to arrayOf, so I strongly inspired myself from its code both for the implementation and the tests.
If you're interested in merging this, I imagine the name can change but I had trouble coming up with something as straightforward and unambiguous.