Legit.js
A simple input validation JavaScript library
Requirements:
- Ensure.js (https://github.com/eduard44/ensure)
On the browser:
On the browser, legit is available as a global object:
1- Install using Bower:
bower install legit
2- Make sure ensure.js is also included in your project (Bower automatically downloads it as a dependency):
3- Include the JS file on your project:
4- Use it!
On node.js:
On node.js, legit is available as a module:
1- Install using npm:
npm install legit.js
2.- Include the library in your project:
var legit = ;
3.- Use it!
Usage example:
Legit works by definining a rule set using a Validator class and then executing the validator on some input object:
// Aliases for conveniencevar Validator = legitValidator MinMaxLengthRule = legitMinMaxLengthRule InArrayRule = legitInArrayRule RequiredRule = legitRequiredRule ValidationError = legitValidationError; // Create a new validator instancevar userValidator = ; userValidator;userValidator;userValidator; // This should not fail:try userValidator; catch error if error instanceof ValidationError console; else console; // This should fail:try userValidator; catch error if error instanceof ValidationError console; else console;
It is also possible to define rules in the constructor of the validator for a shorter definition block. A field can contain one rule or an array of rules:
var userValidator = first_name: 1 20 last_name: 1 20 type: 'admin' 'customer';
Included rules:
Legit.js comes with some built-in rules:
- MinMaxLengthRule(min, max): Expects that the length of the field value is within a certain range
- MinMaxRule(min, max): Expects that the field value is within a certain range
- InArrayRule(allowedElements): Expects that the field value is contained in the allowedElements array
- RequiredRule(): Requires a field to be defined in the input object
- EqualsFieldRule(fieldName): Expects that one field equals another field
- TypeRule(ensureType): Expects that the field is of a certain ensure.js type
- RegexMatchRule(regex): Expects that the field matches a certain JS regular expression
Extending:
It is possible to extend the default rule set by creating rule objects that have ValidationRule
as their prototype. The main function to implement is execute(value, fields, property), which returns true if the input is valid or false if its not.
The example below is a rule that checks if the input is equal to hello
or hello world
(if true
is provided in the constructor):
var { // Call parent constructor legitValidationRule // Save parameters thischeckWorld = checkWorld;} HelloRuleprototype = ; // The execute method performs the validation check.// It is expected that it returns true if the field is valid. False, otherwise.HelloRuleprototype { if thischeckWorld return inputValue === 'hello world'; return inputValue === 'hello';}; // (Optional) Get a custom error message for this ruleHelloRuleprototype { if thischeckWorld return fieldName + ' should be equal to "hello world"'; return fieldName + ' should be equal to "hello"';};