0.3.0 • Published 4 years ago

gateman v0.3.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Gateman

Simple and easiest JSON validator

Documentation

see docs.md

Installation

npm install gateman

Basic Usage

var gateman=require('gateman');
var validate=gateman({
    name:"string|minlength:5|required",
    age:"number|required",
    dob:"date",
    password:"required|minlength:4",
    confirm_password:"same:password",
    address:{
        country:"string",
        city:"string|required",
    },
    tags:["string|$maxcount:2"]
});
var err=validate({
    name:"Koushik",
    age:28,
    dob:"1990-04-01",
    address:{
        country:"India"
    },
    password:"abcd",
    confirm_password:"abcd",
    tags:["javascript","json"]
});
if(!err) console.log("Valid");
else console.log(err);

Rules

RuleDescriptionParam TypeExample
stringString type check{ name: "string" }
numberNumber type check{ age: "number" }
dateDate or not{ dob: "date" }
emailEmail or not{ email: "email" }
requiredValue given or not{ address: "required" }
requiredifRequired if another field is givenstring{ city: "string", address: "string | requiredif: city" }
minMinimum value checknumber{ price: "min:100" }
maxMaximum value checknumber{ price: "max:1000" }
minlengthMinimum length checknumber{ password: "minlength:5" }
maxlengthMaximum length checknumber{ description: "maxlength:200" }
digitNumber of digit checknumber{ pincode: ["digit:6"] }
mindigitMinimum number of digit checknumber{ amount: ["mindigit:3"] }
maxdigitMaximum number of digit checknumber{ amount: ["maxdigit:6"] }
uppercaseAll characters are uppercase or not{ name: "uppercase" }
lowercaseAll characters are lowercase or not{ name: "lowercase" }
sameValue to be same as other fieldstring{ password: "minlength:5", confirm_password: "same:password" }
acceptedValue to be truthy(eg. true or 1 ){ terms: "accepted" }
rangeValue between 2 numbers(inclusive){ price: "range : 100 : 200" }

Array Rules

Array rules operate on whole array and are prefixed with $ symbol.

RuleDescriptionParam TypeExample
requiredAtleast one array item is required{ address: ["$required"] }
countArray length checknumber{ tags: ["$count:2"]}
mincountMinimum array length checknumber{ tags: ["$mincount:2"] }
maxcountMaximum array length checknumber{ 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 = require('express');
var app = express();

var bodyParser = require('body-parser');
app.use(bodyParser.json());

var gateman = require('gateman');
//the following higher order function creates expressjs middleware
var validate = (schema, customMessages, customValidators)=>{
	var validatorFn = gateman(schema, customMessages, customValidators);
	return (req,res,next)=>{
		req.errors = validatorFn(req.body,{flatten:true});
		next();
	}
}
//use the middleware
app.post('/demo', validate({
    email:"email|required",
    location:"string|required|minlength:10"
}), (req, res) => {
	if(req.errors){
		return res.status(422).json({
			success: false,
			error: req.errors
		});
	}
	res.json({
		success:true,
		data:req.body
	});
});

app.listen(8080, () => {
	console.log(name + ' is live at 8080');
});

WIP

0.3.0

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago