Valerian: JS Validation Library ·
Usage
; // must be done once ;; // 1. Get your dataconst data = name: 'Tony' age: 'Tony' email: 'not an email' ; // 2. Set some rulesconst rules = name: 'string' // rules can use string constructors age: // or class constructors email: 'required' 'string|min:3' 'email'; // 3. Make a validatorconst val = data rules; // 4. Test your data!val; // falseval; // true // 5. Show some error messages valerror // 'The age field must be somewhere between 18 and 100.'valerror // nullvalerrorall'age' // ['The age field must be somewhere between 18 and 100.']valerrorall'name' // nullvalerrorall// {// age: ['The age field must be somewhere between 18 and 100.'],// email: ['The email field must be a valid email address.'],// } valerror // falsevalerror // true // To throw an exception:val; // throws ValidationError - it has all the methods available to val.error (e.first('age'))
Rules
The following rules are available
Numeric
; // Both numeric and number resolve to thisconst rules = number: 'numeric' 'number'; ; ;const rules = number: 'number|min:1'; ;const rules = number: 'number|between:1-5';
Integer
; const rules = number: 'integer' 'int'; ; ;const rules = number: 'integer|min:1'; ;const rules = number: 'integer|between:1-5';
IsString
; const rules = value: 'is_string' 'string' ; ; // defaults to .min(1) ; // set the required string lengthsconst rules = value: 'string|min:10' ; ; // shorthand for bothconst rules = value: 'string|between:10-50' ; ; // sets min length to null
By default, uses the JS regex from http://emailregex.com/
; const rules = value: 'email' ; ; ; // do a weak email check - uses /\S+@\S+/
Url
Must be a valid URL. Very naive regex at the moment.
; const rules = value: 'url' ; ;
Matches
This field must match the field passed in here. For example: name: [new String(), new Matches('name_check')]
- the name
and name_check
fields must match
; const rules = value: 'matches|field_name' ; 'name';
Confirmed
The field must match a field with its own name with _confirmation appended. For example if you used it on, password: [new Confirmed()]
, there must be a password
and password_confirmation
field, and they must match each other.
; const rules = value: 'confirmed' ; ;
TypeOf
Checks against the typeof operator
; const rules = value: 'typeof|string' ; 'string';
InstanceOf
You must provide a class to check against
; const rules = value: 'instance_of|classname' ; Date;
IsDateString
Checks that it is a valid date with Date.parse()
; const rules = value: 'is_date_string' 'date_string' ; ;
OneOf
Must be one of the supplied values
; const rules = value: 'one_of:1,2,3' 'in:3,4,5' ; 'apple' 'banana';
Optional
Marks a field as optional. This gets added automatically if you don't have any "required" rules in a rule set.
; const rules = value: 'optional' ; ;
Required
; const rules = value: 'required' ; ;
RequiredWith
; const rules = value: 'required_with:fieldname' ; 'other_field';
RequiredWithout
; const rules = value: 'required_without' ; 'other_field';
Using your own strings
; ; // This will add any new strings, overriding old ones where necessary.; // This will remove all current strings, and use this as the only strings list;
If a string translation is not found, the key will be displayed instead:
valerror; // 'integer/between'valerror; // 'email'
Custom Rules
Rules are simple JS Classes. The must have two methods as a minimum:
{ // value is the value to validate. Most of the time, you will only need this. // field is the field name that is being validated. // validator is the validator class that contains all the data and rules under validation. return value === 'monkey'; } { // The key used to get the error message. // Note that if the key is not found in the available strings, this key will // be displayed, so if you are not bothered about using the strings functionality, // you can just put your error message here. return 'monkey'; } ; {} monkey: ;
Other
This approach to validation is STRONGLY influenced by Laravel's validation library for PHP.