2valid
JavaScript simple data validator
v2.3.1
Installation
npm install --save 2valid
Usage ( simple example )
Check for integer
var v = require('2valid');
console.log( v.validate('integer', 111) );
console.log( v.validate('integer', 'aaa') );
Usage ( password validator )
Check for password. Minimum 4 chars, at least one caps and one small letter, digit and special
var v = require('2valid');
console.log( v.validate('password', 'As1eRR') );
console.log( v.validate('password', '!A1e') );
Get examples
var v = require('2valid');
var example = v.getExample('email');
console.log( example );
console.log( v.validate('email', example) );
Available types
type | description |
---|
string | string |
integer | integer number |
float | float number |
boolean | boolean |
array | array type |
date | date methods |
email | simple e-mail |
password | password, minimum 4 chars, at least one caps and one small letter, digit and special |
md5 | MD5 string |
uuid | UUID string |
Results
If validate passed, then result is null
.
Otherwise validate result is object with these keys:
notMatched
[object] - which key does not match which typenotRequired
[array of string] - list of keys are not in modelnotFound
[array of string] - which keys in model are not found in checked objecttext
[string] - simple description of all errors
For example:
{ notMatched: { '.id': 'integer' },
text: 'Field .id not matched with type integer. Field .secondName not required. Field .name not found',
notRequired: [ '.secondName' ],
notFound: [ '.name' ] }
Usage
Simple async mode
Check if integer
var v = require('2valid');
v.validate( 'integer', 111, function(err) {
console.log(err);
});
v.validate( 'integer', '61cecfb4-da43-4b65-aaa0-f1c3be81ec53', function(err) {
console.log(err);
});
With simple model
Check if valid to simple object with integer id
and string name
var vm = require('./index');
var userModel = {
id: {type: 'integer'},
name: {type: 'string', required: true}
};
vm.validate( userModel,
{ id: 123, secondName: 'Max Validator' },
function(err) {
console.log(err);
}
);
Full error stack
Validation failed with all types of errors
var vm = require('./index');
var userModel = {
id: {type: 'integer'},
name: {type: 'string', required: true}
};
vm.validate( userModel,
{ id: 'Max', secondName: 'Validator' },
function(err) {
console.log(err);
}
);
Result
{ notMatched: { '.id': 'integer' },
text: 'Field .id not matched with type integer. Field .secondName not required. Field .name not found',
notRequired: [ '.secondName' ],
notFound: [ '.name' ] }
With register model
Rregister user
model to check validation by model name
var v = require('2valid');
v.registerModel('user', {
id: {type: 'integer'},
name: {type: 'string'}
});
var userObject = {id: 123, name: 'Alex Validates'}
var valid = v.validate('user', userObject);
console.log(valid.text || 'object is valid');
v.validate('user', userObject, function(err) {
console.log(err || 'object is valid');
});
Result
object is valid
Nested object validator
Validate name.first
and name.second
var v = require('2valid');
v.registerModel('user', {
name: {
first : { type: 'string', required: true },
last : { type: 'string' },
}
});
console.log(v.validate('user', {name: {first: 'Alex', last: 'Validator'}}));
console.log(v.validate('user', {name: {first: 'Marry'}}));
console.log(v.validate('user', {id: 123}));
console.log(v.validate('user', {name: {last: 'Alex'}}));
Regex validator
Only 'cyan', 'magenta', 'yellow' or 'key' can passed in cmyk
model to validate
var v = require('2valid');
v.registerModel('cmyk', {name: { type: 'string', match : /^cyan|magenta|yellow|key$/i }});
console.log(v.validate('cmyk', {name: 'Magenta'}));
console.log(v.validate('cmyk', {name: 'black'}));
console.log(v.validate('cmyk', {name: 123}));
Required keys validator
Check if field is required
var v = require('2valid');
v.registerModel('user', {id: { type: 'uuid', required: true }});
console.log(v.validate('user', {id: '61cecfb4-da43-4b65-aaa0-f1c3be81ec53'}));
console.log(v.validate('user', {id: 123}));
console.log(v.validate('user', {name: 'Alex'}));
Length checking validator
Checking for fixed length of the string
var v = require('2valid');
v.registerModel('ISO 3166-2', {name: { type: 'string', min: 2, max: 2 }});
console.log(v.validate('ISO 3166-2', {name: 'US'}));
console.log(v.validate('ISO 3166-2', {name: 123}));
console.log(v.validate('ISO 3166-2', {name: 'USA'}));
console.log(v.validate('ISO 3166-2', {name: 'U'}));
Add type
You can add new type to validate in to types.js.
'check' method is required to check new inserted type.
For example, new type 'password'. It type must contains minimum 4 chars: at least one lower and one upper case, digit and special chars.
Add code below to types.js in list property:
password : {
min : 4,
max : Infinity,
check : function( password ){
if ( ( typeof string === 'string' || string instanceof String )
&& string.length >= this.min
&& string.length <= this.max
&& string.match(/((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).+)/)
)
return true
else
return false;
}
}
Tests
npm test
Module description
Methods and properties of 2valid
- registerModel( modelName, modelObject ) - register model modelName with modelObject to check
- validate( modelName, entity [, callback] ) - validate model modelName with entity. Return empty object if validate is ok. As callback, return error as first argument.
- registeredModels - list of registered models
- showModelsFull() - show full information of registered model
- dispose() - remove all registered modelNames
Register model
For register model you need to use registerModel method.
v.registerModel( 'user', {
id: { type: 'uuid', required: true },
name: { type: 'string', min: 4, max: 128, required: true },
password: { type: 'password', max: 128, required: true },
});
Validate object
v.validate( 'user', { id : '61cecfb4-da33-4b15-aa10-f1c6be81ec53', name : 'Validator', password : 'A1z!' })
Exceptions
myLibrary.registerModel( 'Name', { id: { type: 'uuid', required: true } } );
- Model in 'modelName' is undefined
myLibrary.registerModel( 'modelName', NaN );
- Model 'modelName' is already registered
myLibrary.registerModel( 'modelName', { id: { type: 'uuid', min: 1, max: 5, required: true } } );
myLibrary.registerModel( 'modelName', { id: { type: 'name' } } );
- No field 'name' in key 'name' in model 'modelName'
myLibrary.consoleTrueOrError ( myLibrary.validate( 'modelName', { name : 'Alex Validates' }) );
myLibrary.registerModel( 'name_exception', { date: { parameter: 'date' } } );
myLibrary.registerModel( 'name_exception', { id: { type: 'guid' } } );
Release History
- 0.1.0 Initial release
- 0.2.0 Fix nested required object error
- 0.2.2 Update errors handles
- 0.2.4 Add match ability to string
- 0.2.10 Split number to integer and float, add password type
- 0.2.12 Validate md5 hash
- 0.3.2 Change quotes in messages
- 0.4.1 Replace result with notFound, notMatched and text keys
- 2.0.1 Rename project to 2valid
- 2.2.1 Add boolean and array types
- 2.2.2 Add examples
Created by
Dimitry, 2@ivanoff.org.ua
curl -A cv ivanoff.org.ua