Proptype
This library is inspired by ReactJS's Prop Validation module. The module serves well enough for ReactJS projects, but along with validations, I needed a little more:
- distinguish between types at runtime, and
- extract information about prop types.
A use case for this library is, if you are working on something such as a UI Builder that requires you to show a form with appropriate fields depending on property types expected by the component. You may find this library useful to defines property types required by your UI components.
Although this library is inspired by ReactJS, it is not restricted to be used with ReactJS in anyway. In fact, property types specific to ReactJS i.e. element, node are not present in this library.
Usage
If you have used ReactJS, the prop type declarations are very similar:
; const objectPropTypes = PropTypesshape optionalAny: PropTypesany // => AnyType instance optionalArray: PropTypesarray // => ArrayType instance optionalBool: PropTypesbool // => BoolType instance optionalFunc: PropTypesfunc // => FuncType instance optionalNumber: PropTypesnumber // => NumberType instance optionalObject: PropTypesobject // => ObjectType instance optionalString: PropTypesstring // => StringType instance optionalSymbol: PropTypessymbol // => SymbolType instance optionalInstanceOf: PropTypes // => InstanceOfType instance // optionalInstanceOf.InstanceOf => SomeClass optionalCustom: PropTypes // => CustomType instance // optionalCustom.validator => myValidator function optionalArrayOfNumbers: PropTypes // ArrayOfType instance // optionalArrayOfNumbers.arrayOf => NumberType instance optionalObjectOfStrings: PropTypes // ObjectOfType instance // optionalObjectOfStrings.objectOf => StringType instance optionalOneOf: PropTypes // => OneOfType instance // => optionalOneOf.oneOf => [1, 'two', 3] optionalOneOfType: PropTypes // => OneOfTypeType instance // optionalOneOfType.oneOfType => [PropTypes.number, PropTypes.string] optionalShape: PropTypesshape oNum: PropTypesnumber rString: PropTypesstringisRequired rShape: PropTypesshape a: PropTypesarray b: PropTypesfuncisRequired isRequired // => ShapeType instance // optionalShape.shape => { // oNum: PropTypes.number, => NumberType instance // rString: PropTypes.string.isRequired, => StringType instance // rShape: PropTypes.shape({ // a: PropTypes.array, => ArrayType instance // b: PropTypes.func.isRequired, => FuncType instance // }).isRequired, => ShapeType instance // } isRequired; objectPropTypes;
You can suffix isRequired
to mark the type as required, which otherwise would accept null
and undefined
values
as valid.
For example:
const optionalType = PropTypesnumber;optionalType === false;let err = optionalType; // => null;err = optionalType; // => nullerr = optionalType; // => Error instance const requiredType = PropTypesnumberisRequired;requiredType === true;let err = requiredType; // => Error instance;err = requiredType; // => nullerr = requiredType; // => Error instance
Supported Type Classes
Following type classes are exported:
- AnyType
- ArrayType
- BoolType
- FuncType
- NumberType
- ObjectType
- StringType
- SymbolType
- ArrayOfType
- CustomType
- InstanceOfType
- ObjectOfType
- OneOfType
- OneOfTypeType
- ShapeType
You can import them as:
;
Type instance to json objects
Each Type except InstanceOfType
and CustomType
supports a toJson() method.
For example:
const myShapeType = optionalNumber: requiredString: true true; // or using PropTypes const anotherShapeType = PropTypesshape optionalNumber: PropTypesnumber requiredString: PropTypesstringisRequiredisRequired; // both these callsmyShapeType;anotherShapeType;// will result =>// {// type: 'shape',// required: true,// shape: {// optionalNumber: {// type: 'number',// required: false// },// requiredString: {// type: 'string',// required: true// }// }// }
Creating Type instances from json objects
You can use a json object to create type instance hierarchy. For example:
const type = proptype; type; // => nulltype; // => Error
Adding Types
You can define you own custom types as:
; static TYPE_NAME = 'myCustomType'; static { // create the type instance from obj return objwhatever objit objrequires objrequired; } { superrequired; // initialise the type } { if value return 'with some message'; // return null for valid values return null; } { return type: MyCustomTypeTYPE_NAME required: this whatever: 'whatever' it: 'it' requires: 'requires' ; } proptype; const myObjProps = PropTypesshape myCustomValue: PropTypesisRequired;
License
Copyright © 2015-2016 Chetan Verma. This source code is licensed under the MIT license found in the LICENSE.txt file. The documentation to the project is licensed under the CC BY-SA 4.0 license.
A note of thanks
Last, but not the least, this library is built using babel-starter-kit Try it out for your next project!