node package manager
We need your input. Help make JavaScript better: Take the 2017 JavaScript Ecosystem survey ยป


Object Validator

Object Validator help you to build validating function to validate a JSON Object easily. based on node-validator

Build Status


Install with npm:

npm install obj-validator --save


var Checker = require('obj-validator').Checker,

validate = Checker({
    'id': Checker.isInt(),
    'username': Checker.isUppercase()

// string message for check
validate = Checker({
    'id': Checker.msg('Id is not int').isInt()

// given message
validate = Checker({
    'foo': Checker.msg({
        isNumeric: 'This is not a number',
        contains: "The value doesn't have a 0 in it"

// nested checker, with nested Checker
validate = Checker({
    'id': Checker.msg('Id is not int').isInt(),
    'profile': Checker({
        'name': Checker.isAlpha(),
        'email': Checker.len(6, 60).isEmail(),
    }).error(function() { throw new Error('Invalid profile'); })

// nested checker, with nested objects
validate = Checker({
    'id': Checker.msg('Id is not int').isInt(),
    'profile': {
        'name': Checker.isAlpha(),
        'email': Checker.len(6, 60).isEmail(),

// custom error handler
validate = Checker({
    'id': Checker.isInt().error(function(msg) { console.log(msg); }), // the same as validator
    'profile': Checker({
    }).onerror(function(/* message */ msg, /* property name */ key, /* original error */original) { throw new Error('Invalid profile'); })
}).onerror(function(msg, key, err) {...});

try {
}catch(e) {

Validation methods

The validation methods is the same as node-validator.

Additional methods

  • isOptional(ignoreFalseValue): which mark this property checking is optional and no needed if the property is not existing in candidate
  • error(fn): add error handler


  • 0.0.6: 'isOptional' method now accepts argument ignoreFalseValue, which indicates whether the checker accept false values like 0, "", null, etc.
  • 0.0.5: add 'isOptional' and make nested object checker better; allow custom error handlers
  • 0.0.4: expose check api in 'validator' for convinient
  • 0.0.3: make internal Rule callable
  • 0.0.2: nothing valuable
  • 0.0.1: start


(The BSD License)

Copyright (c) 2013, Villa.Gao; All rights reserved.