From 5693f309593178a5873e38ebc8d40ed6738b9ff3 Mon Sep 17 00:00:00 2001 From: Matthew Herbst Date: Sat, 2 Apr 2016 19:28:25 +0800 Subject: [PATCH] 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. --- docs/05-reusable-components.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/05-reusable-components.md b/docs/05-reusable-components.md index 0808d102..f6fd219a 100644 --- a/docs/05-reusable-components.md +++ b/docs/05-reusable-components.md @@ -70,9 +70,26 @@ React.createClass({ // won't work inside `oneOfType`. customProp: function(props, propName, componentName) { if (!/matchme/.test(props[propName])) { - return new Error('Validation failed!'); + return new Error( + 'Invalid prop `' + propName + '` supplied to' + + ' `' + componentName + '`. Validation failed.' + ); } - } + }, + + // You can also supply a custom validator to `arrayOf` and `objectOf`. + // It should return an Error object if the validation fails. The validator + // will be called for each key in the array or object. The first two + // 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])) { + return new Error( + 'Invalid prop `' + propFullName + '` supplied to' + + ' `' + componentName + '`. Validation failed.' + ); + } + }) }, /* ... */ });