Validating models in Node Js using Indicative

Data validation is the key point of writing applications,as it is always important to make sure the data you are collecting is valid upto some extent.

Validating models/data models in nodejs is pretty simple as there are ton of packages which can help in doing same but my favourite is Indicative.

Indicative

Indicative is a beautiful Laravel inspired validation library for Nodejs with powerful promises.

It makes validating data models really simple with set of expressive rules.

Installing

npm install indicative  

Example

Let's start with a simple example to get taste of how indicative works.

rules

 var rules = {
    'name' : 'required|alpha'
  };

Rules are a set of objects defining validation rules seperated with | (pipe).

data

var values = {  
  'name': 'johny123'
};

Above we have some sample data we are planning to validate against our
defined set of rules.

Finally we are going to use indicative to run validation using our dummy data and defined set of rules.

// requiring indicative package
var validator = require('indicative');  
// initializing it
validator.initialize();

validator.validate(rules,values)  
.then(function(success){
    console.log(success);
}).catch(function(err){
    console.log(err);
}).done();

Indicative returns a promise with success or error depending whether
validation has passed or failed.

In our case it would have been failed becuase johny123 is not a valid alpha string.


Try is yourself

http://runnable.com/VI6kmKXMAhk9SQ0d/validating-models-in-nodejs-for-indicative


Documentation

https://www.npmjs.org/package/indicative