README

Uranus is a wrapper validation utility over chriso's awesome validator.js with some extra extension methods.
Installation:
$ npm install --save uranus
Note: 2.x is written in Node 4x so its not compatible with previous versions of Node. For previous versions, install 1.x:
$ npm install --save uranus@1.x
Tests:
To execute tests:
# clone the repo and change directory
$ git clone https://github.com/umayr/uranus.git && cd $_
# install local dependencies
$ npm install
# run tests
$ npm test
Usage:
After installing uranus, you can simply use it as:
const Uranus = require('uranus');
let result = Uranus.validateAll([
{
value: '@foo.com',
rules: {
isEmail: true,
len: [15, 100]
}
},{
value: 'Neptune',
rules: {
isAlpha: true,
isLowercase: true,
notContains: 'Net'
}
}]);
console.log(result.isValid()) // false
There are several ways to apply validations. For bulk validation you can use validateAll which supports both array and object.
const Uranus = require('uranus');
// For Arrays.
let result = Uranus.validateAll([
{
value: 'foo@gmail.com',
rules: {
isEmail: true
}
},{
value: 'Neptune',
rules: {
isAlpha: true,
}
}]);
console.log(result.isValid()) // true
// For objects.
let src = {
name: 'Neptune',
email: 'foo@gmail.com'
};
let rules = {
name: {
isAlpha: true
},
email: {
isEmail: true
}
}
let result = Uranus.validateAll(src, rules);
console.log(result.isValid()) // true
By default Uranus generates subject less error messages itself with the help of Cressida. For e.g:
let rules = {
isEmail: true
};
Uranus.validateOne('foo@..!!.com', rules);
// ['should be a valid email address.']
By default these messages are subjectless. To specify a name, you can do something like this:
// For `validateOne()`:
let rules = {
isEmail: true
};
Uranus.validateOne({value: 'foo@..!!.com', name: 'Foo'}, rules);
// ['Foo should be a valid email address.']
// For `validateAll()` with an array:
let result = Uranus.validateAll([
{
value: 'foo',
name: 'Foo',
rules: {
isEmail: true
}
}
], {
includeName: true
});
// ['Foo should be a valid email address.']
// For `validateAll()` with an object:
let src = {
email: {
name: 'Foo',
value: 'foo@!!!.com'
}
};
let rules = {
email: {
isEmail: true
}
}
Uranus.validateAll(src, rules);
// ['Foo should be a valid email address.']
This feature can be turned off with includeName set to false in options moreover you can set your own error messages.
let result = Uranus.validateAll([
{
value: '@foo.com',
rules: {
isEmail: {
args: true,
msg: 'Boo! email is invalid'
},
len: {
args: [15, 100],
msg: 'You\'re either too large or too small.'
}
}
},{
value: 'Neptune',
rules: {
isAlpha: {
args: true,
msg: 'meh, only letters, k?'
},
isLowercase: {
args: true,
msg: 'only lowercase, babes.'
},
notContains: {
args: 'Net',
msg: 'No fishin\''
}
}
}]);
For validating one single value, you can use validateOne as:
let value = 'foo@email.com';
let rules = {
isEmail: true,
notNull: true
};
Uranus.validateOne(value, rules);
Both validateOne & validateAll methods can also be accessed by creating an instance of Uranus. For example:
const Uranus = require('uranus');
let validator = new Uranus();
// validateAll
let result = validator.validateAll([
{
value: 'foo@gmail.com',
rules: {
isEmail: true
}
},{
value: 'Neptune',
rules: {
isAlpha: true,
}
}]);
console.log(result.isValid()) // true
// validateOne
let value = 'foo@email.com';
let rules = {
isEmail: true,
notNull: true
};
let result = validator.validateOne(value, rules);
console.log(result.isValid()) // true
By default validateAll validates all the rules for all value sets but if you set progressive to true while creating Uranus instance, it will stop iterating through rules when one fails. In that way you can get only one error message for one value instead of getting all, for example:
let validator = new Uranus({ progressive: true });
let result = validator.validateAll([
{
value: '@foo.com',
rules: {
isEmail: {
args: true,
msg: 'Boo! email is invalid'
},
len: {
args: [15, 100],
msg: 'You\'re either too large or too small.'
}
}
}]);
console.log(result.getAllMessages())
// ["Boo! email is invalid"]
Note: In case of static methods, options can be provided as the last argument.
Later you can get all of these messages by getAllMessages() method. For example,
let msgs = result.getAllMessages();
console.log(msgs)
// ["Boo! email is invalid", "You're either too large or too small.", "meh, only letters, k?", "only lowercase, babes.", "No fishin'"]
You can also get message for one specific rule by:
let msg = result.getMessage(0, 'isEmail'); // where 0 is the index of provided array.
console.log(msg) // Boo! email is invalid
In order to get all rules for one value you can use getItem() method, like:
let check = result.getItem(0);
console.log(check.isEmail.isValid()) // false
console.log(check.isEmail.getMessage()) // Boo! email is invalid
console.log(check.len.isValid()) // false
console.log(check.len.getMessage()) // You're either too large or too small.
Note: You can get whole ValidationItem by using getRule().
Supported Rules:
As mentioned above, Uranus acts like a wrapper to validator.js so it supports all validations currently provided by validator.js. In addition to that, there are several extra validations rules that Uranus provides out of the box. Some common validations along with their args are as follows:
is: ["^[a-z]+