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

Improve PropTypes.oneOf warning for non-primitive values #5569

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,24 @@ function createEnumTypeChecker(expectedValues) {

function validate(props, propName, componentName, location, propFullName) {
var propValue = props[propName];
var locationName = ReactPropTypeLocationNames[location];

for (var i = 0; i < expectedValues.length; i++) {
if (expectedValues[i] !== null &&
(typeof expectedValues[i] === 'object' || typeof expectedValues[i] === 'function')

Choose a reason for hiding this comment

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

Expected value can of type object/function, why restrict it?

) {
return new Error(
`Invalid ${locationName} \`${propFullName}\` of value \`${propValue}\` ` +
`supplied to \`${componentName}\`, \`oneOf\` expects an enum of primitive values. ` +
`Did you mean to use \`oneOfType\`?`
);
}

if (propValue === expectedValues[i]) {
return null;
}
}

var locationName = ReactPropTypeLocationNames[location];
var valuesString = JSON.stringify(expectedValues);
return new Error(
`Invalid ${locationName} \`${propFullName}\` of value \`${propValue}\` ` +
Expand Down
12 changes: 12 additions & 0 deletions src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,18 @@ describe('ReactPropTypes', function() {
'Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false"].'
);
typeCheckFail(
PropTypes.oneOf(['red', 'blue']),
{},
'Invalid prop `testProp` of value `[object Object]` supplied to ' +
'`testComponent`, expected one of ["red","blue"].'
);
typeCheckFail(
PropTypes.oneOf(['red', 'blue']),
PropTypes.string,
'Invalid prop `testProp` of value `function () { [native code] }` supplied to ' +
'`testComponent`, expected one of ["red","blue"].'
);
});

it('should not warn for valid values', function() {
Expand Down