mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Adding validation for array in ReactPropTypes.
This commit is contained in:
@@ -200,6 +200,12 @@ function createInstanceTypeChecker(expectedClass) {
|
||||
}
|
||||
|
||||
function createEnumTypeChecker(expectedValues) {
|
||||
if (!Array.isArray(expectedValues)) {
|
||||
return new Error(
|
||||
`Invalid argument supplied to oneOf, expected an instance of array.`
|
||||
);
|
||||
}
|
||||
|
||||
function validate(props, propName, componentName, location, propFullName) {
|
||||
var propValue = props[propName];
|
||||
for (var i = 0; i < expectedValues.length; i++) {
|
||||
@@ -249,6 +255,12 @@ function createObjectOfTypeChecker(typeChecker) {
|
||||
}
|
||||
|
||||
function createUnionTypeChecker(arrayOfTypeCheckers) {
|
||||
if (!Array.isArray(arrayOfTypeCheckers)) {
|
||||
return new Error(
|
||||
`Invalid argument supplied to oneOfType, expected an instance of array.`
|
||||
);
|
||||
}
|
||||
|
||||
function validate(props, propName, componentName, location, propFullName) {
|
||||
for (var i = 0; i < arrayOfTypeCheckers.length; i++) {
|
||||
var checker = arrayOfTypeCheckers[i];
|
||||
|
||||
@@ -520,6 +520,13 @@ 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.');
|
||||
});
|
||||
|
||||
it("should warn for invalid strings", function() {
|
||||
typeCheckFail(
|
||||
PropTypes.oneOf(['red', 'blue']),
|
||||
@@ -572,6 +579,13 @@ 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.');
|
||||
});
|
||||
|
||||
it('should warn if none of the types are valid', function() {
|
||||
typeCheckFail(
|
||||
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
||||
|
||||
Reference in New Issue
Block a user