Skip to content

Commit 7ae6791

Browse files
committed
Adding validation for arrayOf and objectOf in ReactPropTypes
1 parent 84af306 commit 7ae6791

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/isomorphic/classic/types/ReactPropTypes.js

+10
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ function createAnyTypeChecker() {
144144

145145
function createArrayOfTypeChecker(typeChecker) {
146146
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+
}
147152
var propValue = props[propName];
148153
if (!Array.isArray(propValue)) {
149154
var locationName = ReactPropTypeLocationNames[location];
@@ -230,6 +235,11 @@ function createEnumTypeChecker(expectedValues) {
230235

231236
function createObjectOfTypeChecker(typeChecker) {
232237
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+
}
233243
var propValue = props[propName];
234244
var propType = getPropType(propValue);
235245
if (propType !== 'object') {

src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ describe('ReactPropTypes', function() {
138138
});
139139

140140
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+
141149
it('should support the arrayOf propTypes', function() {
142150
typeCheckPass(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]);
143151
typeCheckPass(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']);
@@ -461,6 +469,14 @@ describe('ReactPropTypes', function() {
461469
});
462470

463471
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+
464480
it('should support the objectOf propTypes', function() {
465481
typeCheckPass(PropTypes.objectOf(PropTypes.number), {a: 1, b: 2, c: 3});
466482
typeCheckPass(

0 commit comments

Comments
 (0)