Adding validation for arrayOf and objectOf in ReactPropTypes
chico committed
Nov 4, 2015 at 23:30 UTC
7ae67916cc118af6b2af0ed91f4823c3e7701c9c
2 files changed
+26
src/isomorphic/classic/types/ReactPropTypes.js
+10
@@ -144,6 +144,11 @@ function createAnyTypeChecker() {
144
145
function createArrayOfTypeChecker(typeChecker) {
146
function validate(props, propName, componentName, location, propFullName) {
147
+ if (typeof typeChecker !== 'function') {
148
+ return new Error(
149
+ `Invalid argument \`${propFullName}\` supplied to \`${componentName}\`, expected valid PropType.`
150
+ );
151
+ }
152
var propValue = props[propName];
153
if (!Array.isArray(propValue)) {
154
var locationName = ReactPropTypeLocationNames[location];
@@ -230,6 +235,11 @@ function createEnumTypeChecker(expectedValues) {
235
236
function createObjectOfTypeChecker(typeChecker) {
237
function validate(props, propName, componentName, location, propFullName) {
238
+ if (typeof typeChecker !== 'function') {
239
+ return new Error(
240
+ `Invalid argument \`${propFullName}\` supplied to \`${componentName}\`, expected valid PropType.`
241
+ );
242
+ }
243
var propValue = props[propName];
244
var propType = getPropType(propValue);
245
if (propType !== 'object') {
src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
+16
@@ -138,6 +138,14 @@ describe('ReactPropTypes', function() {
138
});
139
140
describe('ArrayOf Type', function() {
141
+ it('should fail for invalid argument', function() {
142
+ typeCheckFail(
143
+ PropTypes.arrayOf({ foo: PropTypes.string }),
144
+ { foo: 'bar' },
145
+ 'Invalid argument `testProp` supplied to `testComponent`, expected valid PropType.'
146
+ );
147
+ });
148
+
149
it('should support the arrayOf propTypes', function() {
150
typeCheckPass(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]);
151
typeCheckPass(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']);
@@ -461,6 +469,14 @@ describe('ReactPropTypes', function() {
469
});
470
471
describe('ObjectOf Type', function() {
472
+ it('should fail for invalid argument', function() {
473
+ typeCheckFail(
474
+ PropTypes.objectOf({ foo: PropTypes.string }),
475
+ { foo: 'bar' },
476
+ 'Invalid argument `testProp` supplied to `testComponent`, expected valid PropType.'
477
+ );
478
+ });
479
+
480
it('should support the objectOf propTypes', function() {
481
typeCheckPass(PropTypes.objectOf(PropTypes.number), {a: 1, b: 2, c: 3});
482
typeCheckPass(