Object Shape Validator
Object shape validator inspired by React.js prop-types
Installation
Use the npm to install the module
npm install object-shape-validator
Usage
Creating basic shape and validate it:
; const shape = title: ObjectShapestring tags: ObjectShape published: ObjectShapebool; const object = title: 'Example title' tags: 'news' 'advertisement' published: false; const errors = shape;console;
Validate with static method:
const errors = ObjectShape; console;
Options argument
You can also pass additional parameters as the last argument to validation function
const errors = shape; console;
List of options
- suppressOwnPropertiesValidation: Disable validation for own properties. Useful when is needed to validate just few properties of an object to match the shape
Build-in types
ObjectShape.string
- validates if value is string
const shape = title: ObjectShapestring;
ObjectShape.number
- validates if value is number
const shape = age: ObjectShapenumber;
ObjectShape.func
- validates if value is func
const shape = test: ObjectShapefunc;
ObjectShape.bool
- validates if value is boolean
const shape = published: ObjectShapebool;
ObjectShape.array
- validates if value is array
const shape = items: ObjectShapearray;
ObjectShape.object
- validates if value is object
const shape = user: ObjectShapeobject;
ObjectShape.instanceOf
- validates if value is instanceof class
{ thistype = type; } { super'dog'; } const shape = pet: ObjectShape;
ObjectShape.oneOf
- validates if value is equal to one of the values
const shape = value: ObjectShape;
ObjectShape.oneOfType
- validates if value is one of the types
const shape = value: ObjectShape;
ObjectShape.arrayOf
- validates if value is array of type
const shape = items: ObjectShape;
ObjectShape.objectOf
- validates if value is a valid shape
const shape = user: fname: ObjectShapestring lname: ObjectShapestring age: ObjectShapenumber ;
ObjectShape.custom
- custom validator function
const shape = value: ObjectShape;
or
const shape = value === 'test' || ` is not equals to 'test'`;
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.