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

Commit 6e49d20

Browse files
committed
[Fix] ensure nullish values in oneOf do not crash
Fixes #256.
1 parent 2ae9e71 commit 6e49d20

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

__tests__/PropTypesDevelopmentReact15.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,17 @@ describe('PropTypesDevelopmentReact15', () => {
983983
);
984984
});
985985

986+
it('does not fail when the valid types contain null or undefined', () => {
987+
typeCheckFail(
988+
PropTypes.oneOf([0, 'false', null, undefined]),
989+
false,
990+
'Warning: Failed prop type: Invalid prop `testProp` of value `false` supplied to ' +
991+
'`testComponent`, expected one of [0,"false",null,null].',
992+
// TODO: uncomment and fix implementation
993+
// '`testComponent`, expected one of [0,"false",null,undefined].',
994+
);
995+
});
996+
986997
it('should not warn for valid values', () => {
987998
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'red');
988999
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'blue');

__tests__/PropTypesDevelopmentStandalone-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,17 @@ describe('PropTypesDevelopmentStandalone', () => {
10571057
);
10581058
});
10591059

1060+
it('does not fail when the valid types contain null or undefined', () => {
1061+
typeCheckFail(
1062+
PropTypes.oneOf([0, 'false', null, undefined]),
1063+
false,
1064+
'Warning: Failed prop type: Invalid prop `testProp` of value `false` supplied to ' +
1065+
'`testComponent`, expected one of [0,"false",null,null].',
1066+
// TODO: uncomment and fix implementation
1067+
// '`testComponent`, expected one of [0,"false",null,undefined].',
1068+
);
1069+
});
1070+
10601071
it('should not warn for valid values', () => {
10611072
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'red');
10621073
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'blue');

__tests__/PropTypesProductionReact15-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,15 @@ describe('PropTypesProductionReact15', () => {
768768
);
769769
});
770770

771+
it('does not fail when the valid types contain null or undefined', () => {
772+
expectNoop(
773+
PropTypes.oneOf([0, 'false', null, undefined]),
774+
false,
775+
'Warning: Failed prop type: Invalid prop `testProp` of value `false` supplied to ' +
776+
'`testComponent`, expected one of [0,"false",null,undefined].',
777+
);
778+
});
779+
771780
it('should not warn for valid values', () => {
772781
expectNoop(PropTypes.oneOf(['red', 'blue']), 'red');
773782
expectNoop(PropTypes.oneOf(['red', 'blue']), 'blue');

factoryWithTypeCheckers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
326326
}
327327

328328
var valuesString = JSON.stringify(expectedValues, function replacer(key, value) {
329-
if (getPropType(value) === 'symbol') {
329+
var type = getPreciseType(value);
330+
if (type === 'symbol') {
330331
return String(value);
331332
}
332333
return value;
@@ -504,6 +505,11 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
504505
return true;
505506
}
506507

508+
// falsy value can't be a Symbol
509+
if (!propValue) {
510+
return false;
511+
}
512+
507513
// 19.4.3.5 Symbol.prototype[@@toStringTag] === 'Symbol'
508514
if (propValue['@@toStringTag'] === 'Symbol') {
509515
return true;

0 commit comments

Comments
 (0)