bailer
A lightweight validation library. Inspired by leonardoborges's bouncer for Clojure.
Setup
npm install bailer
Usage
Built-in validators are:
bailer.validations.required
Validates when notnull
orundefined
bailer.validations.email
Check email format via a regexpbailer.validations.number
Validates whentypeof
returns"number"
bailer.validations.string
Validates whentypeof
returns"string"
bailer.validations.object
Validates whentypeof
returns"object"
. Arrays are objects.
You can create your own (see here).
var b = v = bvalidations; // Define a dummy object.var person = name: "John" age: 20 corporation: '' email: "john.doe@example.org"; // Check if it matches the schema.b; // Every validations passed.// Result:null
Errors
b; // Uh-oh. The person object object doesn't have a truthy corporation// (it is empty) or firstname (it is undefined).// Result:corporation: "corporation must be present" firstname: "firstname must be present"
You can use a custom message.
b;// which results in:corporation: "What! You don't work for a corporation?!"
Custom validators
A custom validator is a function
with this signature:
{ // Do your stuff here // ... var correctResult = objattributeNamelength > 3; // If the provided value is valid, return null. // Otherwise, return a description. return correctResult ? null : "";}