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

af-request-validator

v1.0.3

Published

http request payload validator function

Downloads

33

Readme

request-validator - an http request body validator

http request payload validator function. Takes in request payload and request map, and returns a list of field validation results to be used in Node.js apps

Table of Contents

General Info

This is a simple http payload validator function. Function takes in request payload and request map (see below) and outputs an array of validation results for each field present in the map.

Installation

To install package run in terminal:

cd /path/to/project
$ npm install af-request-validator

Use

To use the function in your app.js file

const validate = require('af-request-validator');

const reqMap = {
    "type"          : "Object",
    "needed"        : true,
    "name"          : "Req Body",
    "properties"    : {
        "name": {
            "type"      : "Simple",
            "needed"    : true,
            "express"   : "^[A-z- ]{3,20}$",
            "name"      : "Name"
        },
        "birthDate": {
            "type"      : "Simple",
            "needed"    : true,
            "express"   : "^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
            "name"      : "Date of Birth"
        },
        "friend": {
            "type"          : "Object",
            "needed"        : true,
            "name"          : "Friend",
            "properties"    : {
                "name": {
                    "type"      : "Simple",
                    "needed"    : true,
                    "express"   : "^[A-z- ]{3,20}$",
                    "name"      : "Name"
                },
                "birthDate": {
                    "type"      : "Simple",
                    "needed"    : true,
                    "express"   : "^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
                    "name"      : "Date of Birth"
                }
            }
        },
        "friendArray" : {
            "type"          : "Array",
            "needed"        : true,
            "name"          : "List of Friends",
            "contents"  : {
                "type"          : "Object",
                "needed"        : true,
                "name"          : "Friend",
                "properties"    : {
                    "name": {
                    "type"      : "Simple",
                    "needed"    : true,
                    "express"   : "^[A-z- ]{3,20}$",
                    "name"      : "Name"
                    },
                    "birthDate": {
                        "type"      : "Simple",
                        "needed"    : true,
                        "express"   : "^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
                        "name"      : "Date of Birth"
                    }
                }
            }
        }
    }
}

const requestBody = {
    "name" : "Vik",
    "birthDate" : "1985-02-10",
    "friend" : {
        "name" : "Alex",
        "birthDate" : "1986-01-02"
    },
    "friendArray" : [
        {
        "name" : "Dima",
        "birthDate" : "1987-12-09"
        }
    ] 
}

const validateSummary = validate(requestBody, reqMap);

validateSummary will contain an Array of validation results for each field in Map

Request Map Strucure

Request map is a js object that contains filed descriptions for validator to check against

request map can describe 3 types of fields (Object, Array, and simple);

Simple type field map looks like this:

{
"type"      : "Simple",                 //Type
"needed"    : true,                     //Is this field mandatory?
"express"   : "^[A-z- ]{3,20}$",       //Regexp to validate the field
"name"      : "Name"                    //Field name
}

Object type filed map looks like this:

{
    "type"          : "Object",         //Type
    "needed"        : true,             //Is this field mandatory?
    "name"          : "Req Body",       //Field name
    "properties"    : {                 //Maps for properties the object has
        "name": {
            "type"      : "Simple",
            "needed"    : true,
            "express"   : "^[A-z- ]{3,20}$",
            "name"      : "Name"
        },
        "birthDate": {
            "type"      : "Simple",
            "needed"    : true,
            "express"   : "^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
            "name"      : "Date of Birth"
        }
    }
}

note that object can contain Abject and Array properties, just nest them

Array type field map looks like this:

{
"type"          : "Array",
"needed"        : true,
"name"          : "List of Friends",
"contents"  : {                         //map for contents of array 
    "type"          : "Object",
    "needed"        : true,
    "name"          : "Friend",
    "properties"    : {
        "name": {
        "type"      : "Simple",
        "needed"    : true,
        "express"   : "^[A-z- ]{3,20}$",
        "name"      : "Name"
        },
        "birthDate": {
            "type"      : "Simple",
            "needed"    : true,
            "express"   : "^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
            "name"      : "Date of Birth"
        }
    }
}