From ac349cfbe520c95bd6ba2d31138569a6750b7ba8 Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Wed, 27 May 2015 15:57:16 -0700 Subject: [PATCH] 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.) --- src/isomorphic/classic/types/ReactPropTypes.js | 16 ++++++++++------ .../types/__tests__/ReactPropTypes-test.js | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/isomorphic/classic/types/ReactPropTypes.js b/src/isomorphic/classic/types/ReactPropTypes.js index bb7933185b..c8425f666a 100644 --- a/src/isomorphic/classic/types/ReactPropTypes.js +++ b/src/isomorphic/classic/types/ReactPropTypes.js @@ -201,9 +201,11 @@ function createInstanceTypeChecker(expectedClass) { function createEnumTypeChecker(expectedValues) { if (!Array.isArray(expectedValues)) { - return new Error( - `Invalid argument supplied to oneOf, expected an instance of array.` - ); + return createChainableTypeChecker(function() { + return new Error( + `Invalid argument supplied to oneOf, expected an instance of array.` + ); + }); } function validate(props, propName, componentName, location, propFullName) { @@ -256,9 +258,11 @@ function createObjectOfTypeChecker(typeChecker) { function createUnionTypeChecker(arrayOfTypeCheckers) { if (!Array.isArray(arrayOfTypeCheckers)) { - return new Error( - `Invalid argument supplied to oneOfType, expected an instance of array.` - ); + return createChainableTypeChecker(function() { + return new Error( + `Invalid argument supplied to oneOfType, expected an instance of array.` + ); + }); } function validate(props, propName, componentName, location, propFullName) { diff --git a/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js b/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js index fbff02c1e3..0992f4f6eb 100644 --- a/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js +++ b/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js @@ -521,10 +521,11 @@ describe('ReactPropTypes', function() { describe('OneOf Types', function() { it("should fail for invalid argument", function() { - var error = PropTypes.oneOf('red', 'blue'); - expect(error instanceof Error).toBe(true); - expect(error.message).toBe('Invalid argument supplied to ' + - 'oneOf, expected an instance of array.'); + typeCheckFail( + PropTypes.oneOf('red', 'blue'), + 'red', + 'Invalid argument supplied to oneOf, expected an instance of array.' + ); }); it("should warn for invalid strings", function() { @@ -580,10 +581,11 @@ describe('ReactPropTypes', function() { describe('Union Types', function() { it("should fail for invalid argument", function() { - var error = PropTypes.oneOfType('red', 'blue'); - expect(error instanceof Error).toBe(true); - expect(error.message).toBe('Invalid argument supplied to ' + - 'oneOfType, expected an instance of array.'); + typeCheckFail( + PropTypes.oneOfType(PropTypes.string, PropTypes.number), + 'red', + 'Invalid argument supplied to oneOfType, expected an instance of array.' + ); }); it('should warn if none of the types are valid', function() {