object-validators

0.1.1 • Public • Published

object-validators

description

installation

build status

Build Status

usage

Set up a validator configuration

var   ObjectValidators = require('object-validatorss')
    , validators
    , validator;

// create a new validator configuration that
// can be extended with custom validators
// and custom translations
validators = new ObjectValidators();


// create a validator for an object
validators.createValidator({
    name: {
          type: validator.STRING
        , min: 10               // numbers only
        , max: 3000             // numbers only
        , value: 89999          // must have the type defined by the type filed
        , length: 10            // only for strings, buffers
        , minLength: 1          // only for strings, buffers
        , maxLength: 20         // only for strings, buffers
        , required: false       
        , pattern: /hui/gi      // only for strings
    }
    , related: validators.createValidator({});
});

or use the default configuration

 var  Validator = require('object-validatorss').Validator
    , validator;

validator = new Validator{
    name: {
          type: validator.STRING
        , min: 10               // numbers only
        , max: 3000             // numbers only
        , value: 89999          // must have the type defined by the type filed
        , length: 10            // only for strings, buffers
        , minLength: 1          // only for strings, buffers
        , maxLength: 20         // only for strings, buffers
        , required: false       
        , pattern: /hui/gi      // only for strings
    }
    , related: new Validator({});
});

the validator validates either the object or all objects inside an array passed to it. each object may contain key pointing to other validator

validation is async

// using promises
validator.validate(input).then(function() {
    // is valid
}.bind(this)).catch(function(message) {

}.bind(this));


// using a callback
validator.validate(input, function(isValid, message) {
    // isnt valid
});

Package Sidebar

Install

npm i object-validators

Weekly Downloads

1

Version

0.1.1

License

none

Last publish

Collaborators

  • ee