npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gateman

v0.3.0

Published

Simple and easiest JSON validator

Downloads

15

Readme

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

|Rule|Description|Param Type|Example| |-|-|-|-| |string|String type check| |{ name: "string" }| |number|Number type check| |{ age: "number" }| |date|Date or not| |{ dob: "date" }| |email|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 = 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