add React.PropTypes.any

add the ability for React propTypes to accept an `any` type: `someProp: React.PropTypes.any`.
This is more useful when combined with `.isRequired`, to enforce that _something_ is passed:
`someProp: React.PropTypes.any.isRequired`
This commit is contained in:
Cheng Lou authored and Paul O’Shannessy committed 2014-01-10 21:11:11 -08:00
1 parent d8a8f6a881
commit d14ce00dc3
2 files changed
+39 -1

No files matched your search

+13 -1
View File
@@ -87,12 +87,24 @@ var Props = {
instanceOf: createInstanceTypeChecker,
renderable: createRenderableTypeChecker(),
component: createComponentTypeChecker()
component: createComponentTypeChecker(),
any: createAnyTypeChecker()
};
var ANONYMOUS = '<<anonymous>>';
function createAnyTypeChecker() {
function validateAnyType(
shouldThrow, propValue, propName, componentName, location
) {
// always is valid
return true;
}
return createChainableTypeChecker(validateAnyType);
}
function isRenderable(propValue) {
switch(typeof propValue) {
case 'number':
+26
View File
@@ -347,4 +347,30 @@ describe('Union Types', function() {
});
});
describe('Any type', function() {
it('should should accept any value', function() {
expect(typeCheck(Props.any, 1)).not.toThrow();
expect(typeCheck(Props.any, 'str')).not.toThrow();
expect(typeCheck(Props.any.isRequired, 1)).not.toThrow();
expect(typeCheck(Props.any.isRequired, 'str')).not.toThrow();
expect(typeCheck(Props.any, null)).not.toThrow();
expect(typeCheck(Props.any, undefined)).not.toThrow();
expect(typeCheck(Props.any.isRequired, null)).toThrow(
'Invariant Violation: Required prop `testProp` was not specified in ' +
'`testComponent`.'
);
expect(typeCheck(Props.any.isRequired, undefined)).toThrow(
'Invariant Violation: Required prop `testProp` was not specified in ' +
'`testComponent`.'
);
});
it('should have a weak version that returns true/false', function() {
expect(typeCheck(Props.string.weak, null)()).toEqual(true);
expect(typeCheck(Props.any.weak.isRequired, null)()).toEqual(false);
expect(typeCheck(Props.any.isRequired.weak, null)()).toEqual(false);
});
});
});