mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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.)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user