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 warnings - Fixes #1919 #6940

Closed
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
21 changes: 20 additions & 1 deletion src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,38 @@ function createEnumTypeChecker(expectedValues) {

function validate(props, propName, componentName, location, propFullName) {
var propValue = props[propName];
var isOneOfPropType = false;
for (var i = 0; i < expectedValues.length; i++) {
if (is(propValue, expectedValues[i])) {
return null;
}
if (getPropType(expectedValues[i]) === 'function' && expectedValues[i].name === 'bound checkType') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you set a flag on the bound checkType instead? This way if we change its name, this won’t start creating false positives.

isOneOfPropType = true;
}
}

var locationName = ReactPropTypeLocationNames[location];
var valuesString = JSON.stringify(expectedValues);
if (isOneOfPropType) {
return new Error(
`Invalid ${locationName} \`${propFullName}\` of value \`${propValue}\` supplied to \`${componentName}\`.\n` +
`Possibly expected to use \`oneOfType\` instead of \`oneOf\`.`
);
}
var valuesString = JSON.stringify(expectedValues, functionPrettifier);
return new Error(
`Invalid ${locationName} \`${propFullName}\` of value \`${propValue}\` ` +
`supplied to \`${componentName}\`, expected one of ${valuesString}.`
);
}

function functionPrettifier(name, values) {
for (var i = 0; i< values.length; i++) {
if (getPropType(values[i]) === 'function') {
values[i] = values[i].toString();
}
}
return values;
}
return createChainableTypeChecker(validate);
}

Expand Down