Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for arrayOf and objectOf in ReactPropTypes #5390

Merged
merged 2 commits into from Nov 12, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/isomorphic/classic/types/ReactPropTypes.js
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(
`Invalid argument \`${propFullName}\` supplied to \`${componentName}\`, expected valid PropType.`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message isn't awesome. You're actually trying to say that the component author wrote a bad propType spec, but the way this error will show sounds like the user provided an invalid prop value. Can we tune it so that's more clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it more clear now?

);
}
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(
`Invalid argument \`${propFullName}\` 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
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` 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` 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