Skip to content

Commit

Permalink
Merge pull request #5390 from chicoxyzzy/arrayof-objectof-tests
Browse files Browse the repository at this point in the history
Add validation for arrayOf and objectOf in ReactPropTypes
  • Loading branch information
zpao committed Nov 12, 2015
2 parents 951f3b6 + a86d25d commit 8104262
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ function createAnyTypeChecker() {

function createArrayOfTypeChecker(typeChecker) {
function validate(props, propName, componentName, location, propFullName) {
if (typeof typeChecker !== 'function') {
return new Error(
`Property \`${propFullName}\` of component \`${componentName}\` has invalid PropType notation inside arrayOf.`
);
}
var propValue = props[propName];
if (!Array.isArray(propValue)) {
var locationName = ReactPropTypeLocationNames[location];
Expand Down Expand Up @@ -230,6 +235,11 @@ function createEnumTypeChecker(expectedValues) {

function createObjectOfTypeChecker(typeChecker) {
function validate(props, propName, componentName, location, propFullName) {
if (typeof typeChecker !== 'function') {
return new Error(
`Property \`${propFullName}\` of component \`${componentName}\` has invalid PropType notation inside objectOf.`
);
}
var propValue = props[propName];
var propType = getPropType(propValue);
if (propType !== 'object') {
Expand Down
16 changes: 16 additions & 0 deletions src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ describe('ReactPropTypes', function() {
});

describe('ArrayOf Type', function() {
it('should fail for invalid argument', function() {
typeCheckFail(
PropTypes.arrayOf({ foo: PropTypes.string }),
{ foo: 'bar' },
'Property `testProp` of component `testComponent` has invalid PropType notation inside arrayOf.'
);
});

it('should support the arrayOf propTypes', function() {
typeCheckPass(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]);
typeCheckPass(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']);
Expand Down Expand Up @@ -461,6 +469,14 @@ describe('ReactPropTypes', function() {
});

describe('ObjectOf Type', function() {
it('should fail for invalid argument', function() {
typeCheckFail(
PropTypes.objectOf({ foo: PropTypes.string }),
{ foo: 'bar' },
'Property `testProp` of component `testComponent` has invalid PropType notation inside objectOf.'
);
});

it('should support the objectOf propTypes', function() {
typeCheckPass(PropTypes.objectOf(PropTypes.number), {a: 1, b: 2, c: 3});
typeCheckPass(
Expand Down

0 comments on commit 8104262

Please sign in to comment.