Skip to content
Validate JSON objects against a schema written in JSON
Find file
New pull request
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


  • Small, light, zero dependencies.
  • Can be used in Node, browsers, and Mongo/Couch Map/Reduce functions.
  • Recursively tests JSON data structures.
  • Easy to extend with your own validations.
  • Good test coverage.


  • test? valid? check? Could these names be more generic?


var inRange = Valid.number().min(4).max(9)
inRange.verify(12)    // throws an error
var stringInRange = inRange.typeof('string').errorHandler(Valid.TrueFalse)
if(stringInRange.validate("not in range")) { /\* not executed \*/ }


var check = Valid.checker(TrueFalse);  // uses Throw by default
if(check(12).inRange(4,9)) { /\* not executed \*/ }

what about going insane?

Object.prototype.assert = Valid.checker().errorHandler(Valid.Throw);
12.assert().min(4).max(9);    // throws an error

Handling Validation Errors

By default the validator raises an error when it finds invalid data (using the Valid.Throw handler). To make it return a boolean result:





ErrorObject (included with json-valid)

errors: an error object
  .add([field], msg)  -- adds a message for the field.  A field of "." refers to the entire object.
  .clear()            -- clears all error messages
  .count([field])     -- tells how many total errors or just for the given field
  .messages([field])  -- returns an array of all error messages concated with field name

Built-In Validations

Don't include validations by default? Syntax is very personal. Do you prefer in, contains, isIn, etc? Don't do isBlah. Just blah. Find the BARE MINIMUM non-composed validators. Users can compose their own APIs.

in(1,2) equivalent: in([1,2]) in({1:ignored,2:ignored}) in is a keyword... use synonym isIn if this is a problem. have a synonym module? Valid.isOneOf =

match(regex) length(min, [max]) applies to strings, arrays, or any object that has a length field.

and(Validation, Validation, ...) though why would you do this since Validation.validation().validation() is the same. or(Validation, Validation, ...) not(Validation) Make any a synonym for or and every a synonym or and? What about not(and) and not(or)? optional(Validation, Validation, ...) min -- uses JS's < and > operators so can be used on strings too. max -- also gt, ge, lt, le, eq, ne?

isDate isBefore isAfter

isEmail() isIP() isUrl() isUUID()

json(template) -- checks an entire json structure

We don't offer inverses because not() is usually clearer. If you'd rather write Valid.notIn(arr) instead of Valid.not(, just do this:

Valid.notIn = function(arr) { return Valid.not( }
Valid.notIn = Valid.not(    // could this work?

Extending Valid



  • The syntax is pretty sweet: check('abc').len(6,12).isEmail();
  • Only supports immediate use, can't declare validations.
  • Only meant to work inside node. Can't recurse over JSON.

  • Validates JSON using a declared schema.
  • Only supports static use, can't do immediate validations.
  • Produces unsatisfying error messages on deeply nested schemas.


Pain-free MIT.

Something went wrong with that request. Please try again.