Skip to content

Commit 6cc037b

Browse files
troydemonbreungaearon
authored andcommitted
Fix for #5468: Validate PropTypes.oneOf(Type) arguments early (#6316)
* Fix for 5468: Validate proptype definitions sooner Added typeCheckWarn() func and updated the oneOf/oneOfType tests Added __DEV__ warning for invalid oneOf/OneOfType args * Suppress redundant error on warn; typeCheckWarn() removed * Return no-op * Using emptyFunction module for consistency * Remove createChainableTypeChecker() call * Adjust test to assert type check passes when warned
1 parent f949125 commit 6cc037b

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/isomorphic/classic/types/ReactPropTypes.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
1616

1717
var emptyFunction = require('emptyFunction');
1818
var getIteratorFn = require('getIteratorFn');
19+
var warning = require('warning');
1920

2021
/**
2122
* Collection of methods that allow declaration and validation of props that are
@@ -226,11 +227,8 @@ function createInstanceTypeChecker(expectedClass) {
226227

227228
function createEnumTypeChecker(expectedValues) {
228229
if (!Array.isArray(expectedValues)) {
229-
return createChainableTypeChecker(function() {
230-
return new Error(
231-
`Invalid argument supplied to oneOf, expected an instance of array.`
232-
);
233-
});
230+
warning(false, 'Invalid argument supplied to oneOf, expected an instance of array.');
231+
return emptyFunction.thatReturnsNull;
234232
}
235233

236234
function validate(props, propName, componentName, location, propFullName) {
@@ -288,11 +286,8 @@ function createObjectOfTypeChecker(typeChecker) {
288286

289287
function createUnionTypeChecker(arrayOfTypeCheckers) {
290288
if (!Array.isArray(arrayOfTypeCheckers)) {
291-
return createChainableTypeChecker(function() {
292-
return new Error(
293-
`Invalid argument supplied to oneOfType, expected an instance of array.`
294-
);
295-
});
289+
warning(false, 'Invalid argument supplied to oneOfType, expected an instance of array.');
290+
return emptyFunction.thatReturnsNull;
296291
}
297292

298293
function validate(props, propName, componentName, location, propFullName) {

src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,16 @@ describe('ReactPropTypes', function() {
590590
});
591591

592592
describe('OneOf Types', function() {
593-
it('should fail for invalid argument', function() {
594-
typeCheckFail(
595-
PropTypes.oneOf('red', 'blue'),
596-
'red',
597-
'Invalid argument supplied to oneOf, expected an instance of array.'
598-
);
593+
it('should warn but not error for invalid argument', function() {
594+
spyOn(console, 'error');
595+
596+
PropTypes.oneOf('red', 'blue');
597+
598+
expect(console.error).toHaveBeenCalled();
599+
expect(console.error.calls.argsFor(0)[0])
600+
.toContain('Invalid argument supplied to oneOf, expected an instance of array.');
601+
602+
typeCheckPass(PropTypes.oneOf('red', 'blue'), 'red');
599603
});
600604

601605
it('should warn for invalid values', function() {
@@ -651,12 +655,16 @@ describe('ReactPropTypes', function() {
651655
});
652656

653657
describe('Union Types', function() {
654-
it('should fail for invalid argument', function() {
655-
typeCheckFail(
656-
PropTypes.oneOfType(PropTypes.string, PropTypes.number),
657-
'red',
658-
'Invalid argument supplied to oneOfType, expected an instance of array.'
659-
);
658+
it('should warn but not error for invalid argument', function() {
659+
spyOn(console, 'error');
660+
661+
PropTypes.oneOfType(PropTypes.string, PropTypes.number);
662+
663+
expect(console.error).toHaveBeenCalled();
664+
expect(console.error.calls.argsFor(0)[0])
665+
.toContain('Invalid argument supplied to oneOfType, expected an instance of array.');
666+
667+
typeCheckPass(PropTypes.oneOf(PropTypes.string, PropTypes.number), []);
660668
});
661669

662670
it('should warn if none of the types are valid', function() {

0 commit comments

Comments
 (0)