Gateman
Simple and easiest JSON validator
Documentation
see docs.md
Installation
npm install gateman
Basic Usage
var gateman=;var validate=;var err=;if!err console;else console;
Rules
Rule | Description | Param Type | Example |
---|---|---|---|
string | String type check | { name: "string" } |
|
number | Number type check | { age: "number" } |
|
date | Date or not | { dob: "date" } |
|
Email or not | { email: "email" } |
||
required | Value given or not | { address: "required" } |
|
requiredif | Required if another field is given | string | { city: "string", address: "string | requiredif: city" } |
min | Minimum value check | number | { price: "min:100" } |
max | Maximum value check | number | { price: "max:1000" } |
minlength | Minimum length check | number | { password: "minlength:5" } |
maxlength | Maximum length check | number | { description: "maxlength:200" } |
digit | Number of digit check | number | { pincode: ["digit:6"] } |
mindigit | Minimum number of digit check | number | { amount: ["mindigit:3"] } |
maxdigit | Maximum number of digit check | number | { amount: ["maxdigit:6"] } |
uppercase | All characters are uppercase or not | { name: "uppercase" } |
|
lowercase | All characters are lowercase or not | { name: "lowercase" } |
|
same | Value to be same as other field | string | { password: "minlength:5", confirm_password: "same:password" } |
accepted | Value to be truthy(eg. true or 1 ) |
{ terms: "accepted" } |
|
range | Value between 2 numbers(inclusive) | { price: "range : 100 : 200" } |
Array Rules
Array rules operate on whole array and are prefixed with $
symbol.
Rule | Description | Param Type | Example |
---|---|---|---|
required | Atleast one array item is required | { address: ["$required"] } |
|
count | Array length check | number | { tags: ["$count:2"]} |
mincount | Minimum array length check | number | { tags: ["$mincount:2"] } |
maxcount | Maximum array length check | number | { tags: ["$maxcount:2"] } |
Using array rules in keys
favourites: "$mincount": 2 "$maxcount": 10 rating:"number | required"
a valid json with respect to the above schema would be
favourites: rating: 44 rating: 22
Using with expressjs
var express = ;var app = ; var bodyParser = ;app; var gateman = ;//the following higher order function creates expressjs middlewarevar { var validatorFn = ; return { reqerrors = ; ; }}//use the middlewareapp; app;