Skip to content

Commit

Permalink
Add expected types to oneOfType warning (facebook#9)
Browse files Browse the repository at this point in the history
Adds data object to returned error in checker so that the expected types can be accessed from within oneOfType check. Also, updates the tests slightly.

Fixes: facebook#9
  • Loading branch information
joshalling committed Jul 20, 2018
1 parent cfc7f43 commit a558e75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions __tests__/PropTypesDevelopmentReact15.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ describe('PropTypesDevelopmentReact15', () => {
typeCheckFail(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
[],
'Invalid prop `testProp` supplied to `testComponent`.',
'Invalid prop `testProp` supplied to `testComponent`',
);

const checker = PropTypes.oneOfType([
Expand All @@ -923,7 +923,7 @@ describe('PropTypesDevelopmentReact15', () => {
typeCheckFail(
checker,
{c: 1},
'Invalid prop `testProp` supplied to `testComponent`.',
'Invalid prop `testProp` supplied to `testComponent`',
);
});

Expand Down
4 changes: 2 additions & 2 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ describe('PropTypesDevelopmentStandalone', () => {
typeCheckFail(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
[],
'Invalid prop `testProp` supplied to `testComponent`.',
'Invalid prop `testProp` supplied to `testComponent`',
);

const checker = PropTypes.oneOfType([
Expand All @@ -925,7 +925,7 @@ describe('PropTypesDevelopmentStandalone', () => {
typeCheckFail(
checker,
{c: 1},
'Invalid prop `testProp` supplied to `testComponent`.',
'Invalid prop `testProp` supplied to `testComponent`',
);
});

Expand Down
16 changes: 12 additions & 4 deletions factoryWithTypeCheckers.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
*/
function PropTypeError(message) {
this.message = message;
this.data = (typeof data == 'object') ? data: {};
this.stack = '';
}
// Make `instanceof Error` still work for returned errors.
Expand Down Expand Up @@ -232,7 +233,10 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
// 'of type `object`'.
var preciseType = getPreciseType(propValue);

return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + preciseType + '` supplied to `' + componentName + '`, expected ') + ('`' + expectedType + '`.'));
return new PropTypeError(
'Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + preciseType + '` supplied to `' + componentName + '`, expected ') + ('`' + expectedType + '`.'),
{expectedType: expectedType}
);
}
return null;
}
Expand Down Expand Up @@ -349,14 +353,18 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
}

function validate(props, propName, componentName, location, propFullName) {
var expectedTypes = [];
for (var i = 0; i < arrayOfTypeCheckers.length; i++) {
var checker = arrayOfTypeCheckers[i];
if (checker(props, propName, componentName, location, propFullName, ReactPropTypesSecret) == null) {
var checkerResult = checker(props, propName, componentName, location, propFullName, ReactPropTypesSecret);
if (checkerResult == null) {
return null;
} else if (checkerResult.data.hasOwnProperty('expectedType')) {
expectedTypes.push(checkerResult.data.expectedType);
}
}

return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` supplied to ' + ('`' + componentName + '`.'));
var expectedTypesMessage = (typeof expectedTypes !== 'undefined') ? ', expected one of type [' + expectedTypes.join(', ') + ']': '';
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` supplied to ' + ('`' + componentName + '`' + expectedTypesMessage + '.'));
}
return createChainableTypeChecker(validate);
}
Expand Down

0 comments on commit a558e75

Please sign in to comment.