The Vladiator
Javascript validation and assertion library. Suggestions are welcome.
Includes Typescript definitions.
Installation
You can grab the latest version via NPM or Bower.
npm install --save the-vladiatorbower install --save the-vladiator
Then either use require through NodeJS.
var validate = ;
Or add as a script to your HTML.
Getting Started
Below are some basic examples of usage.
var validate = ; //E-mail address; //returns true if passed validation //Name; //returns true if failed validation //Positive number; //throws if failed validation
Type Checks
You can test whether your input is a certain type.
//Checks for an array or object
Equality Checks
//Checks for equality using === //Check contents using indexOf
Numeric Checks
Length Checks
Optional Checks
//Ensures that value is defined, not null and not NaN //Skips remaining checks if value is undefined, null or NaN //Ensures the value is defined //Skips remaining checks if value is undefined
Object Checks
Nested Checks
//The value can be replaced with a nested object one or more levels deeper //Iterates checking inside arrays or objects, all subsequent checks will be performed on each //Iterates checking through all nested levels inside arrays or objects
Custom Checks
//Validates whether input is pie;
Other Checks
//Checks for MongoDB ObjectIds //Checks whether value is an Enum.//Arguments are a mongoose model or schema and property name string //Checks whether value confirms to semver
Other Stuff
//Replaces the currently checking value //Inverts the following checks result //Changes values case
Advanced Examples
We can compose some pretty lengthy validation rules as demonstrated below though it's recommended to split them into smaller more readable chunks.
Example One
Example Two
var data = usernames: 'User1' 'User2' 'User3' 'User4' 'User5' emails: 'admin1@vmlweb.co.uk' 'admin2@vmlweb.co.uk' 'admin3@vmlweb.co.uk' 'admin4@vmlweb.co.uk' 'admin5@vmlweb.co.uk' ; ;
Example Three
var database = users: metadata: {} ids: '507f1f77bcf86cd799439011' '507f1f77bcf86cd799439012' '507f1f77bcf86cd799439013' '507f1f77bcf86cd799439014' '507f1f77bcf86cd799439015' '507f1f77bcf86cd799439016' '507f1f77bcf86cd799439017' '507f1f77bcf86cd799439018' '507f1f77bcf86cd799439019' '507f1f77bcf86cd799439020' '507f1f77bcf86cd799439021' ; ;