Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Validate JSON objects against a schema written in JSON
JavaScript
Tree: 7e94b6e3a1

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
LICENSE
README.md
console.js
kitchen-sink.js
package.json

README.md

jsvalid

  • 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.

TODO

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

Declarative

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 \*/ }

Immediate

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:

Valid.errorHandler(Valid.TrueFalse).validate(false);

Throw

Console

TrueFalse

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 = Valid.in

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(Valid.in(arr)), just do this:

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

Extending Valid

Validators

Alternatives

https://github.com/chriso/node-validator

  • 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.

https://github.com/doffm/Onvalid

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

License

Pain-free MIT.

Something went wrong with that request. Please try again.