Adding validation for arrayOf and objectOf in ReactPropTypes

This commit is contained in:
chico
2015-11-05 00:20:33 +03:00
parent 84af306044
commit 7ae67916cc
2 changed files with 26 additions and 0 deletions
@@ -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(