Skip to content

Commit

Permalink
Merge pull request #3969 from spicyj/pt
Browse files Browse the repository at this point in the history
Fix PropTypes.{oneOf, oneOfType} validation
  • Loading branch information
sophiebits committed May 27, 2015
2 parents f44bf7e + ac349cf commit 34f8a0d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
16 changes: 10 additions & 6 deletions src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 10 additions & 8 deletions src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 34f8a0d

Please sign in to comment.