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

json-request-validator

v1.0.6

Published

A simple validator for express json requests

Downloads

6

Readme

Introduction

This is a simple validator to validate you req.body in your express application.

Example

const express = require('express');
const ValidateInputs = require('json-request-validator');

const app = express();

const rules = {
    firstName: 'required',
    lastName: 'required|string',
    age: 'required|number',
    hobbies: 'required|array',
    email: 'required|email',
    password: {
        rules: 'required|minlen',
        minlen: 5,
    },
};

app.post('/signup', ValidateInputs(rules), (req, res) => {

});

app.listen(3033);

The above rules defines the following constraints:

firstName and lastName are required. age is required and must be a number. hobbies is required and must be an array. email is required and must be a valid email address. password is required and must contain at least 5 characters.

Usage

Usage is a straight process. First, a set of rules is constructed using the provided constraints:

const rules = {
    name: 'required',
    age: 'required|number',
}

This rule is passed into the Validator as a middleware to the route.

app.post('/signup', ValidateInputs(rules), (req, res) => {

});

If the input is valid, then the next function will be called, otherwise the request is ended as follows (fields depends on the errors):

{
    status: 400,
    error: 'Validation errors.',
    fields: {
        name: 'Name is required',
        age: 'Age must be a number',
    },
}

To manually handle the errors, a second function is passed to ValidateInputs as follows

app.post('/signup', ValidateInputs(rules, false), (req, res) => {
    const { validator } = req;
    if (!validator.isValid) {
        console.log(validator.errors);
    }
});

Rules

  • required - field must be present, Default message {fieldName} is required..
  • array - field must contain array of items, Default message {fieldName} must be an array..
  • number - field must be numeric, Default message {fieldName} must be a number..
  • string - field must be a string, Default message {fieldName} must be a string..
  • email - field must be a valid email, Default message {fieldName} must be a valid email address..
  • object - field must be an object, Default message {fieldName} must be an object..
  • arrayobject - field must be an array of object, Default message {fieldName} must be an array of objects..
  • min - specify minimum length of field, Default message {fieldName} must have minimum length of {value}.
  • max - specify maximum length of field, Default message {fieldName} must have maximum length of {value}.
  • belongsto - specify an array a field must be contained in, Default message {fieldName} must be one of {arrayValue}.
  • eachbelongsto - specify an array each item in an field must be contained in, Default message Each of {fieldName} must be one of {arrayValue}.

Rules that doesn't specify values can be used directly on the field name in the rules object, eg:

const rules = {
    email: 'required|email',
    age: 'number',
}

These rules includes: required, number, email, array, object, arrayobject.

Rules that specify values cannot be used directly on rules field names but as follows:

const rules = {
    gender: {
        rules: 'required|belongsto',
        belongsto: ['male', 'female'],
    }
    age: {
        rules: 'number|min|max',
        min: 13,
        max: 48,
    },
}

These rules includes: min, max, belongsto, eachbelongsto.

The rules object and arrayobject can specify an optional value which validates the fields of the object, eg:

const rules = {
    location: {
        rules: 'object|required',
        object: {
            lat: 'required|number',
            lng: 'required|number',
        },
    },
    experiences: {
        rules: 'required|arrayobject',
        arrayobject: {
            company: 'required',
            years: 'required|number',
        },
    },
};

All the validation rules are valid for validating the object fields.