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

Commit

Permalink
[Fix] ensure nullish values in oneOf do not crash
Browse files Browse the repository at this point in the history
Fixes #256.
  • Loading branch information
ljharb committed Feb 13, 2019
1 parent 2ae9e71 commit 6e49d20
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
11 changes: 11 additions & 0 deletions __tests__/PropTypesDevelopmentReact15.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,17 @@ describe('PropTypesDevelopmentReact15', () => {
);
});

it('does not fail when the valid types contain null or undefined', () => {
typeCheckFail(
PropTypes.oneOf([0, 'false', null, undefined]),
false,
'Warning: Failed prop type: Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false",null,null].',
// TODO: uncomment and fix implementation
// '`testComponent`, expected one of [0,"false",null,undefined].',
);
});

it('should not warn for valid values', () => {
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'red');
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'blue');
Expand Down
11 changes: 11 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,17 @@ describe('PropTypesDevelopmentStandalone', () => {
);
});

it('does not fail when the valid types contain null or undefined', () => {
typeCheckFail(
PropTypes.oneOf([0, 'false', null, undefined]),
false,
'Warning: Failed prop type: Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false",null,null].',
// TODO: uncomment and fix implementation
// '`testComponent`, expected one of [0,"false",null,undefined].',
);
});

it('should not warn for valid values', () => {
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'red');
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'blue');
Expand Down
9 changes: 9 additions & 0 deletions __tests__/PropTypesProductionReact15-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,15 @@ describe('PropTypesProductionReact15', () => {
);
});

it('does not fail when the valid types contain null or undefined', () => {
expectNoop(
PropTypes.oneOf([0, 'false', null, undefined]),
false,
'Warning: Failed prop type: Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false",null,undefined].',
);
});

it('should not warn for valid values', () => {
expectNoop(PropTypes.oneOf(['red', 'blue']), 'red');
expectNoop(PropTypes.oneOf(['red', 'blue']), 'blue');
Expand Down
8 changes: 7 additions & 1 deletion factoryWithTypeCheckers.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
}

var valuesString = JSON.stringify(expectedValues, function replacer(key, value) {
if (getPropType(value) === 'symbol') {
var type = getPreciseType(value);
if (type === 'symbol') {
return String(value);
}
return value;
Expand Down Expand Up @@ -504,6 +505,11 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
return true;
}

// falsy value can't be a Symbol
if (!propValue) {
return false;
}

// 19.4.3.5 Symbol.prototype[@@toStringTag] === 'Symbol'
if (propValue['@@toStringTag'] === 'Symbol') {
return true;
Expand Down

0 comments on commit 6e49d20

Please sign in to comment.