validio

0.1.6 • Public • Published

Npm version Magnum-CI

Validio

A Node.js type less schema validation for model, objects and data formats. Also, Validio can run as Synchronous and as Promise implementation too, see more at Validio Npm.

How to install

npm install --save validio

How to use it

First we need to generate the schema to validate an object where name is a string which max length is 20, and field age is an int which max value is 100:

var validio = require('validio')
validio.parse('req name 20, lastName 20, age int 100');

It will generate the next schema

{
    name: {
        validators: {
            isString: {},
            isLength: {
                args: {length: 20}
            }
        },
        required: true,
        length: 20,
        type: 'string'
    },
    lastName: {
        validators: {
            isString: {},
            isLength: {
                args: {length: 20}
            }
        },
        length: 20,
        type: 'string'
    },
    age: {
        validators: {
            isInt: {
                args: {max: 100}
            }
        },
        type: 'int'
    }
}

In this case we will use the Person class, and will validate object instances using the schema:

function Person(name, email, age, phone, birth, height) {
    this.name = name;
    this.email = email;
    this.age = age;
    this.phone = phone;
    this.birth = birth;
    this.height = height;
};

Then we can validate the object using the schema, in this case it will validate the name and age because those are the only fields that appears on the schema.

var person = new Person('Joe', 'joe@joe.com', 45);
 
var result = validio.validate(schema, person);
 
if(!result.isValid) {
    console.log(result.errors.length == 1 ? 'There is an error:' : 'There are many errors:');
    for(error in result.errors)
        console.log(error);
}
else
    console.log('Validation succeeded');

Validio as Promise

We can use Validio as Promise implementation, in the next sample we will use the same schema we build before.

var validio = require('validio').aspromise;
 
var schema = validio.parse('name 20, age int 100');
var person = new Person('Joe', 'joe@joe.com', 45);
 
 
validio.validate(schema, person)
.then(function(result) {
    console.log('Validation succeeded');
})
.catch(function(result) {
    for(error of result.errors)
        console.log(error);
});

New Features

  • Adding support for ArrayTypes
  • Fully supporting Basic ArrayType
  • Fully supporting Schema Relations with ArrayType

Disclaimer

Strictly supporting packages up to next versions:

Dependencies (7)

Dev Dependencies (3)

Package Sidebar

Install

npm i validio

Weekly Downloads

3

Version

0.1.6

License

none

Last publish

Collaborators

  • cecheverria