legit.js 0.1.1
Lightweight objects and strings validation for Node.js.
Usage
Create a schema and use it to validate data.
There are two equivalent usages:
schema.test(data)
legit.mize(schema, data)
These functions return null
if there was no error validating the data.
If the data didn't fit the schema, they return a description of the error (either a String, an Array, or a Map/Object).
var schema = legit; var err1 = schema; // err1 = 'Greater than max' var err2 = schema; // err2 = null // alternative usage: err1 = legit; // err1 = 'Greater than max' err2 = legit; // err2 = null
Note: The keyword "new" should not be used when creating an instance of a schema.
Real world example
Suppose you have some incoming network data and you want to validate it before using/processing it.
// Create the schema var userSchema = legit ; // Validate incoming data var err = userSchema; if err // Data did not fit the schema. // Check 'err' to learn what went wrong. console; else // Data successfully validated! // Now you can use/process it with confidence. ...
Installing and Importing
Using NPM, run the following command in your project's root directory.
$ npm install legitjs
In your Node.js program:
var legit = ;
Types Of Schemas
legit.Any()
Accepts anything.
Example:
var schema = legit; schema // null schema // null
legit.Null()
Accepts only null
.
Example:
var schema = legit; schema // 'Not null' schema // null
legit.Boolean()
Accepts only Booleans.
Modifiers:
none()
: Acceptsnull
andundefined
.
Example:
var schema = legit; schema // 'Not a boolean' schema // 'Boolean is null or undefined' schema // null var schema2 = legit schema2 // null
legit.Number()
Accepts only Numbers.
Modifiers:
none()
: Acceptsnull
andundefined
.min(a)
: Sets minimum allowed value (a
).max(b)
: Sets maximum allowed value (b
).
Example:
var schema = legit; schema; // null schema; // 'Greater than maximum' schema; // 'Not a number' schema; // 'Number is null or undefined'
legit.String()
Accepts only Strings.
Modifiers:
none()
: Acceptsnull
andundefined
.min(a)
: Sets minimum allowed length (a
).max(b)
: Sets maximum allowed length (b
).regex(e)
: Sets a regular expression to use (e
).
Example:
var schema = legit; schema; // 'Regular expression didn't match' schema; // null schema; // 'Greater than maxmimum'
legit.Array()
Accepts only Arrays. Can be used recursively with all other schemas.
You have two options when using legit.Array():
- Set a schema to validate all objects of the array by using
.type(schema)
once. - Set a different schema for each item by using
.item(schema)
once for each item, in the expected order.
Modifiers:
none()
: Acceptsnull
andundefined
.min(a)
: Sets minimum allowed length (a
). Only affects same-type arrays.max(b)
: Sets maximum allowed length (b
). Only affects same-type arrays.type(s)
: Sets a schema (s
) to test all array items. Establishes array as same-type.item(s)
: Sets a schema (s
) to test a single item of the array. Establishes array as different-type.strict()
: Rejects arrays with length greater than expected. Only affects different-type arrays.
Example:
// Same-type array var schema = legit typelegit; schema; // null schema; // 'Greater than maxmimum' schema; // [null, 'Greater than maximum'] // Different-type array var schema = legit ; schema; // null schema; // [ null, null, // 'More items than expected (Array in strict mode)' ] schema; // [ 'Greater than maximum', 'Less than minimum' ]
legit.Map()
Accepts only Maps/Objects. Can be used recursively with all other schemas.
Modifiers:
none()
: Acceptsnull
andundefined
.strict()
: Rejects maps with unexpected keys.key(n, s)
: Sets an expected key-value pair.n
is the expected key ands
is the schema that will be used to validate the value.
Example:
var schema = legit ; schema // null schema // { user: 'Greater than maximum', // age: 'Less than minimum' }
Support/Contact
Feel free to contact me with questions, suggestions, or comments.
I hope you enjoy using legit.js as much as I enjoyed writing it.
If you come across any issues, please report them.