Added new documentation showing use of a custom validator for arrayOf 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.
Matthew Herbst committed
Apr 2, 2016 at 19:28 UTC
0c89da5b2506f317381b843d10596cfa24150bf5
1 file changed
+19
-2
docs/docs/05-reusable-components.md
+19
-2
@@ -70,9 +70,26 @@ React.createClass({
70
// won't work inside `oneOfType`.
71
customProp: function(props, propName, componentName) {
72
if (!/matchme/.test(props[propName])) {
73
- return new Error('Validation failed!');
73
+ return new Error(
74
+ 'Invalid prop `' + propName + '` supplied to' +
75
+ ' `' + componentName + '`. Validation failed.'
76
+ );
77
}
75
- }
78
+ },
79
+
80
+ // You can also supply a custom validator to `arrayOf` and `objectOf`.
81
+ // It should return an Error object if the validation fails. The validator
82
+ // will be called for each key in the array or object. The first two
83
+ // arguments of the validator are the array or object itself, and the
84
+ // current item's key.
85
+ customArrayProp: React.PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
86
+ if (!/matchme/.test(propValue[key])) {
87
+ return new Error(
88
+ 'Invalid prop `' + propFullName + '` supplied to' +
89
+ ' `' + componentName + '`. Validation failed.'
90
+ );
91
+ }
92
+ })
93
},
94
/* ... */
95
});