property-validation
Chainable validation of object properties for any JS application such as Angular, React and Node applications. Designed to be used as ES6 class. Works with redux-form out of the box.
Features
-
Chainable, e.g.
const validate =// using default error message// using custom error message; -
Supports nested path, e.g.
const validate =; -
Supports array in path, e.g.
const validate =// hobbies is an array; -
Works with redux-form out of the box, example here
-
Can be extended with custom validations
{return this;}const validate =;
Installation
npm install property-validation --save
Test
npm test
Usage
Simple use
;; const values = firstName: 'JohnLongFirstName' lastName: 'Smith'; const errors = values maxLength'firstName' 'firstName is too long' 15 ; todeep; // true
Use with redux-form
{ super; thisrenderHobbies = thisrenderHobbies; } { return <div> fields </div> ; } { return <form onSubmit=thisprops> <Field name="firstName" component="Input" /> <FieldArray name="hobbies" component=thisrenderHobbies/> </form> ; } const validate = values ; form: 'ExampleForm' validate
Existing validations
-
validEmail(fieldName, message) fieldName: required. The key of the property. message: optional. Default message is 'invalid email'.
values -
validInteger(fieldName, message) fieldName: required. The key of the property. message: optional. Default message is 'invalid integer'.
values -
maxLength(fieldName, maxLength, message) fieldName: required. The key of the property. maxLength: required. Maximum length of the string. message: optional. Default message is 'max length exceeded'.
valuesmaxLength'password' 20 'Password is too long' -
containsOnly(fieldList, message) fieldList: required. An array of valid keys of the property. message: optional. Default message is `body should only contain ${fieldList.join(', ')}`.
values -
require(fieldName, message) fieldName: required. The key of the property, or '/' which means the property itself. message: optional. Default message is 'property is required but missing' or 'body is required but missing'.
valuesvalues
Pull Requests are welcomed to add more validations!
Notes
-
For nested properties with arrays, validation message for first element will be undefined if validation passed for the first element but failed for one of the rest. e.g.
const values =persons:firstName: 'John'lastName: 'Smith'hobbies:type: 'sport'name: 'soccer'type: 'sport'name: 'basketball'firstName: 'Alex'lastName: 'Turner'hobbies:name: 'soccer'type: 'sport'name: 'basketball';const errors = values;todeep; // true -
Validations, except for require, do not validate null or undefined values. For example, if
values.firstName
is undefined,new Validation(values).validInteger('firstName')
does not give validation error. In order to validate null or undefined values, 'require' should be added before other validations in the chain. E.g.new Validation(values).require('firstName').validInteger('firstName')
. -
In order to extend
Validation
with custom validations,this.validation()
should be used if it is to validate against a specified path, e.g.{return this;}If it is to validate against the whole object then
this.validation
should not be used. e.g.{if !Arraythiserrorsbody = message || 'body should be an array';return this;}