Skip to content

Commit

Permalink
Adding validation for arrayOf and objectOf in ReactPropTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
chicoxyzzy committed Nov 4, 2015
1 parent 84af306 commit 4573cd8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ function createAnyTypeChecker() {

function createArrayOfTypeChecker(typeChecker) {
function validate(props, propName, componentName, location, propFullName) {
if (typeof typeChecker !== 'function') {
return new Error(
`Invalid argument \`${propFullName}\` of type ` +
`\`${typeof typeChecker}\` supplied to \`${componentName}\`, expected valid PropType.`
);
}
var propValue = props[propName];
if (!Array.isArray(propValue)) {
var locationName = ReactPropTypeLocationNames[location];
Expand Down Expand Up @@ -230,6 +236,12 @@ function createEnumTypeChecker(expectedValues) {

function createObjectOfTypeChecker(typeChecker) {
function validate(props, propName, componentName, location, propFullName) {
if (typeof typeChecker !== 'function') {
return new Error(
`Invalid argument \`${propFullName}\` of type ` +
`\`${typeof typeChecker}\` supplied to \`${componentName}\`, expected valid PropType.`
);
}
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' },
'Invalid argument `testProp` of type `object` supplied to `testComponent`, expected valid PropType.'
);
});

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' },
'Invalid argument `testProp` of type `object` supplied to `testComponent`, expected valid PropType.'
);
});

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 4573cd8

Please sign in to comment.