Documentation for arrayOf custom item validator#5731
Documentation for arrayOf custom item validator#5731gaearon merged 1 commit intofacebook:masterfrom MatthewHerbst:docs-arrayof-custom-validator
Conversation
|
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks! |
|
@MatthewHerbst updated the pull request. |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
|
Hi, thanks for the PR! I would like to amend this a little bit. The important part is not that The prop type function signature that We are cheating a little bit by passing So while I think your edits are useful, it would help to clarify it Sorry it took a long time to review, and thank you for contributing! I’ll be happy to review an update, if you’d like to keep working on this. For now, marking as needing an update. |
|
@MatthewHerbst updated the pull request. |
|
@gaearon Thanks for getting back to this. Actually, the entire reason I made the PR was to highlight the I used that knowledge to write a custom validator that validates the format of the keys within each object of the array, rather than the values of the array. The original SO post that I asked might give you more context. Based on my knowledge and the knowledge within that SO post, the only way to individual object key validation when the keys are dynamic is to know that you have access to the object's container, which in this case is the |
|
Whoops, I get it now. |
|
As for your question—I would probably write something like As for your PR—Let’s just remove the two last arguments? They are not used in the example and distract from the main point. |
docs/docs/05-reusable-components.md
Outdated
| }, | ||
|
|
||
| // Further, you can also supply a custom type checker to `arrayOf`. The | ||
| // function will be called on each item in the array, The first two |
There was a problem hiding this comment.
Nit: Can we make a linebreak before the first The so it starts on the next line?
Another nit: on each time => for each item.
Yet another nit: please change , The to . and start The on the next line it doesn’t “hang” here either.
Thank you!
|
@MatthewHerbst updated the pull request. |
|
@gaearon per your comments, I've reworked it a little bit.
Let me know your thoughts. |
docs/docs/05-reusable-components.md
Outdated
| if (!/matchme/.test(props[propName])) { | ||
| return new Error('Validation failed!'); | ||
| return new Error( | ||
| `Invalid prop \`${propName}\` supplied to` + |
There was a problem hiding this comment.
While we started using ES6 in the docs, I think we try to call it out explicitly when we do this (e.g. when we use classes and arrows). In this particular case I think we’re better off concatenating strings the traditional way so people who aren’t entirely comfortable with ES6 don’t get distracted by this.
|
👍 This is looking good. I left a few nits and it’s ready to merge then. |
|
@MatthewHerbst updated the pull request. |
docs/docs/05-reusable-components.md
Outdated
| // arguments of the validator are the array or object itself, and the | ||
| // current item's key. | ||
| customArrayProp: React.PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) { | ||
| if(!/matchme/.test(propValue[key])) { |
There was a problem hiding this comment.
Please add a space between if and (
|
@MatthewHerbst updated the pull request. |
|
Looks great! Can you please squash these into a single commit? |
|
@gaearon I think I might have done that slightly wrong - how to I avoid having to re-merge into myself after the rebase? |
|
I usually do something like this: git checkout mybranch // fix conflicts if any git reset --soft origin/master // remove all commits There are probably simpler ways but that is what I do. |
… or objectOf, specifically how the method signature for such a validator differs from the customProp validator method signature. Made minor edits to error message for customProp example to match error messages found in src.
|
@gaearon That did it! Thanks 👍 |
|
Thanks for sticking through with this! |
The docs currently show how to use a custom validator for a prop. However, it fails to show that this can be extended to doing custom validation on items within an array by passing a custom validator to
arrayOf. This change makes that clear, as well as shows that the method signature for the custom validator passed intoarrayOfis different versus just validating a prop.