Skip to content

Commit

Permalink
Improve PropTypes.oneOf warning (issue facebook#1919)
Browse files Browse the repository at this point in the history
  • Loading branch information
philipshurpik committed Jun 1, 2016
1 parent 51f8b1b commit 478d4b4
Showing 1 changed file with 20 additions and 1 deletion.
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") {
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

0 comments on commit 478d4b4

Please sign in to comment.