Skip to content

Commit

Permalink
[Fix] Fix oneOf when used with Symbols
Browse files Browse the repository at this point in the history
- Fixes error due to an attempt to coerce a Symbol to a string
- Improves formatting of the "expected" portion of the generated
  warning, outputting for example `["Symbol(A)","Symbol(B)"]` rather
  than `[null,null]`

Fixes facebook#10
  • Loading branch information
jimf authored and ljharb committed Oct 2, 2018
1 parent ef3541f commit 3b39cab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions __tests__/PropTypesDevelopmentReact15.js
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,12 @@ describe('PropTypesDevelopmentReact15', () => {
'Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false"].',
);
typeCheckFail(
PropTypes.oneOf([Symbol('red'), Symbol('blue')]),
Symbol('green'),
'Invalid prop `testProp` of value `Symbol(green)` supplied to ' +
'`testComponent`, expected one of ["Symbol(red)","Symbol(blue)"].',
);
});

it('should not warn for valid values', () => {
Expand Down
6 changes: 6 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,12 @@ describe('PropTypesDevelopmentStandalone', () => {
'Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false"].',
);
typeCheckFail(
PropTypes.oneOf([Symbol('red'), Symbol('blue')]),
Symbol('green'),
'Invalid prop `testProp` of value `Symbol(green)` supplied to ' +
'`testComponent`, expected one of ["Symbol(red)","Symbol(blue)"].',
);
});

it('should not warn for valid values', () => {
Expand Down
6 changes: 6 additions & 0 deletions __tests__/PropTypesProductionReact15-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,12 @@ describe('PropTypesProductionReact15', () => {
'Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false"].',
);
expectNoop(
PropTypes.oneOf([Symbol('red'), Symbol('blue')]),
Symbol('green'),
'Invalid prop `testProp` of value `Symbol(green)` supplied to ' +
'`testComponent`, expected one of ["Symbol(red)","Symbol(blue)"].',
);
});

it('should not warn for valid values', () => {
Expand Down
9 changes: 7 additions & 2 deletions factoryWithTypeCheckers.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,13 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
}
}

var valuesString = JSON.stringify(expectedValues);
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of value `' + propValue + '` ' + ('supplied to `' + componentName + '`, expected one of ' + valuesString + '.'));
var valuesString = JSON.stringify(expectedValues, function replacer(key, value) {
if (getPropType(value) === 'symbol') {
return String(value);
}
return value;
});
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of value `' + String(propValue) + '` ' + ('supplied to `' + componentName + '`, expected one of ' + valuesString + '.'));
}
return createChainableTypeChecker(validate);
}
Expand Down

0 comments on commit 3b39cab

Please sign in to comment.