Fix PropTypes.{oneOf, oneOfType} validation
Follow-up to #3963. (Returning an Error wasn't useful; it just caused a later error when actually using it because type checkers need to be functions.)
Ben Alpert committed
May 27, 2015 at 15:57 UTC
ac349cfbe520c95bd6ba2d31138569a6750b7ba8
2 files changed
+20
-14
src/isomorphic/classic/types/ReactPropTypes.js
+10
-6
@@ -201,9 +201,11 @@ function createInstanceTypeChecker(expectedClass) {
201
202
function createEnumTypeChecker(expectedValues) {
203
if (!Array.isArray(expectedValues)) {
204
- return new Error(
205
- `Invalid argument supplied to oneOf, expected an instance of array.`
206
- );
204
+ return createChainableTypeChecker(function() {
205
+ return new Error(
206
+ `Invalid argument supplied to oneOf, expected an instance of array.`
207
+ );
208
+ });
209
}
210
211
function validate(props, propName, componentName, location, propFullName) {
@@ -256,9 +258,11 @@ function createObjectOfTypeChecker(typeChecker) {
258
259
function createUnionTypeChecker(arrayOfTypeCheckers) {
260
if (!Array.isArray(arrayOfTypeCheckers)) {
259
- return new Error(
260
- `Invalid argument supplied to oneOfType, expected an instance of array.`
261
- );
261
+ return createChainableTypeChecker(function() {
262
+ return new Error(
263
+ `Invalid argument supplied to oneOfType, expected an instance of array.`
264
+ );
265
+ });
266
}
267
268
function validate(props, propName, componentName, location, propFullName) {
src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
+10
-8
@@ -521,10 +521,11 @@ describe('ReactPropTypes', function() {
521
522
describe('OneOf Types', function() {
523
it("should fail for invalid argument", function() {
524
- var error = PropTypes.oneOf('red', 'blue');
525
- expect(error instanceof Error).toBe(true);
526
- expect(error.message).toBe('Invalid argument supplied to ' +
527
- 'oneOf, expected an instance of array.');
524
+ typeCheckFail(
525
+ PropTypes.oneOf('red', 'blue'),
526
+ 'red',
527
+ 'Invalid argument supplied to oneOf, expected an instance of array.'
528
+ );
529
});
530
531
it("should warn for invalid strings", function() {
@@ -580,10 +581,11 @@ describe('ReactPropTypes', function() {
581
582
describe('Union Types', function() {
583
it("should fail for invalid argument", function() {
583
- var error = PropTypes.oneOfType('red', 'blue');
584
- expect(error instanceof Error).toBe(true);
585
- expect(error.message).toBe('Invalid argument supplied to ' +
586
- 'oneOfType, expected an instance of array.');
584
+ typeCheckFail(
585
+ PropTypes.oneOfType(PropTypes.string, PropTypes.number),
586
+ 'red',
587
+ 'Invalid argument supplied to oneOfType, expected an instance of array.'
588
+ );
589
});
590
591
it('should warn if none of the types are valid', function() {