Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Add expected types to oneOfType warning #198

Merged
merged 1 commit into from
Feb 28, 2019
Merged
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
2 changes: 1 addition & 1 deletion __tests__/PropTypesDevelopmentReact15.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ describe('PropTypesDevelopmentReact15', () => {
typeCheckFail(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
[],
'Invalid prop `testProp` supplied to `testComponent`.',
'Invalid prop `testProp` supplied to `testComponent`, expected one of type [string, number].',
);

const checker = PropTypes.oneOfType([
Expand Down
2 changes: 1 addition & 1 deletion __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ describe('PropTypesDevelopmentStandalone', () => {
typeCheckFail(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
[],
'Invalid prop `testProp` supplied to `testComponent`.',
'Invalid prop `testProp` supplied to `testComponent`, expected one of type [string, number].',
);

const checker = PropTypes.oneOfType([
Expand Down
19 changes: 14 additions & 5 deletions factoryWithTypeCheckers.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
* is prohibitively expensive if they are created too often, such as what
* happens in oneOfType() for any type before the one that matched.
*/
function PropTypeError(message) {
function PropTypeError(message, data) {
this.message = message;
this.data = data && typeof data === 'object' ? data: {};
this.stack = '';
}
// Make `instanceof Error` still work for returned errors.
Expand Down Expand Up @@ -234,7 +235,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 @@ -378,14 +382,19 @@ 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;
}
if (checkerResult.data.hasOwnProperty('expectedType')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

for posterity: assuming .data exists, and also calling .hasOwnProperty directly off of an object, caused #369.

expectedTypes.push(checkerResult.data.expectedType);
}
}

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