mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Adding validation for arrayOf and objectOf in ReactPropTypes
This commit is contained in:
@@ -144,6 +144,11 @@ function createAnyTypeChecker() {
|
||||
|
||||
function createArrayOfTypeChecker(typeChecker) {
|
||||
function validate(props, propName, componentName, location, propFullName) {
|
||||
if (typeof typeChecker !== 'function') {
|
||||
return new Error(
|
||||
`Invalid argument \`${propFullName}\` supplied to \`${componentName}\`, expected valid PropType.`
|
||||
);
|
||||
}
|
||||
var propValue = props[propName];
|
||||
if (!Array.isArray(propValue)) {
|
||||
var locationName = ReactPropTypeLocationNames[location];
|
||||
@@ -230,6 +235,11 @@ function createEnumTypeChecker(expectedValues) {
|
||||
|
||||
function createObjectOfTypeChecker(typeChecker) {
|
||||
function validate(props, propName, componentName, location, propFullName) {
|
||||
if (typeof typeChecker !== 'function') {
|
||||
return new Error(
|
||||
`Invalid argument \`${propFullName}\` supplied to \`${componentName}\`, expected valid PropType.`
|
||||
);
|
||||
}
|
||||
var propValue = props[propName];
|
||||
var propType = getPropType(propValue);
|
||||
if (propType !== 'object') {
|
||||
|
||||
@@ -138,6 +138,14 @@ describe('ReactPropTypes', function() {
|
||||
});
|
||||
|
||||
describe('ArrayOf Type', function() {
|
||||
it('should fail for invalid argument', function() {
|
||||
typeCheckFail(
|
||||
PropTypes.arrayOf({ foo: PropTypes.string }),
|
||||
{ foo: 'bar' },
|
||||
'Invalid argument `testProp` supplied to `testComponent`, expected valid PropType.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should support the arrayOf propTypes', function() {
|
||||
typeCheckPass(PropTypes.arrayOf(PropTypes.number), [1, 2, 3]);
|
||||
typeCheckPass(PropTypes.arrayOf(PropTypes.string), ['a', 'b', 'c']);
|
||||
@@ -461,6 +469,14 @@ describe('ReactPropTypes', function() {
|
||||
});
|
||||
|
||||
describe('ObjectOf Type', function() {
|
||||
it('should fail for invalid argument', function() {
|
||||
typeCheckFail(
|
||||
PropTypes.objectOf({ foo: PropTypes.string }),
|
||||
{ foo: 'bar' },
|
||||
'Invalid argument `testProp` supplied to `testComponent`, expected valid PropType.'
|
||||
);
|
||||
});
|
||||
|
||||
it('should support the objectOf propTypes', function() {
|
||||
typeCheckPass(PropTypes.objectOf(PropTypes.number), {a: 1, b: 2, c: 3});
|
||||
typeCheckPass(
|
||||
|
||||
Reference in New Issue
Block a user