Elao/validator-framework

Name: validator-framework

Owner: Elao

Description: A javascript objects validators based on rules (support for nested objects, async validation, validations groups, etc...)

Created: 2014-10-14 16:10:42.0

Updated: 2014-10-15 08:44:15.0

Pushed: 2014-11-04 09:08:29.0

Homepage: null

Size: 180

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

TODO

Features:

- Nested Objects validation
- Custom rules
- Custom validator
- Post validation
- Custom errors message
- Async validation
- Use of promises
- Validation groups
- Global validators
- Custom field labels

Description:

validator-magic handle objects validation

Install

npm install validator-magic

Usage:

var validator = require('validator-magic');

var myobject = {
    username: 'Elao',
    email:    'contact@elao.com',
};

var rules = {
    username: {required: true},
    email:    {required: true, isEmail: true},
    website:  {required: true, isUrl: true}
};

validator.ObjectValidator(rules).validate(myobject)
         .then(function() { /** Validation succeed **/ })
         .catch(function(e) {
            /** Validation failed **/
         })

=> Nested objects validation

=> Global validators

=> Post validator

=> Customize field label and errors messages

=> Getting validation errors as a flat object

=> Available Asserts:

required: ...

isEmail: ...

=> Using the validation groups :

Exemple:

var rules = {
    username: {
        isRequired: {
            value: true,
            group: ['create']
        }
    },
    email: {
        isRequired: {
            value: true,
            group: ['create']
        },
        isEmail: true
    }
}

=> Using the custom validator :

Exemple with boolean validation:

    var rules = {
        username: {
            custom: function(fieldValue, ruleValue, data) { return isItValid ? true : false }
        }
    }

Exemple with promise validator:

    var rules = {
        username: {
            custom: function(fieldValue, ruleValue, data) { return new Promise(function(resolve, reject) { return isItValid ? resolve() : reject() }) }
        }
    }

=> Adding custom validator (to reuse validator) - or overwrite existing

var validator = require('validator-magic');

var newValidatorTypes = {
    myNewValidatorType: function(fieldValue, ruleValue, data) {
        return new Promise(function(resolve, reject) {
            if (ruleValue.)
        });
    }
}

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.