Skip to content

scrollback/valid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

valid

A validator ###Installation

npm install git://github.com/scrollback/valid.git#v0.0.1

##Usage

creating new Object

var valid = require('valid')();

functions

  1. valid.registerType(name, validatorFunction): To add new type to the valid object. E.g:
valid.registerType('id', function(object) {
  return typeof object === 'string' && object.length && object.length === 32;
});
  1. valid.registerGlobalType(name, validatorFunction) : Register new type to global memory of validator.
  2. valid.validate(object, validator): validate object using validator Object.

validation

Creating a validation object. e.g:

var validator = {
        ref: ['string', 'undefined'],
        id: ['undefined', 'string'],
        type: ['nonEmptyString'],
        to: [function(object) {
          return typeof object !== "undefined";
        }],
        time: ['undefined', 'number'],
        session: ['string'],
        resource: ['undefined', 'string'],
        origin: ['strictObject', 'undefined', {
          //anothor validation object(recursive)
        }]
    };

Each key of validation object is an array with a new validation objects, predefined global types, newly added types and functions. To validate an object against validator object.

var result = valid.validate(object, validator); 

It will return an object of type Result:

{ 
  status: true | false,
  info: 'string'
}