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_sanity_checks

v1.0.0

Published

JSON Request body sanity checks

Downloads

3

Readme

json_sanity_checks

The first step involved at backend during a REST API call is the validation of request body. This step of sanity checks involves basically two functions

  • JSON Format Validation of request body
  • JSON Data validation

This project is about the first part.

JSON Format Validation

This step basically consists of validation of two things

  • The parameters satisfy the schema (i.e. string parameters are string, numbers are numbers, arrays are arrays etc.)
  • All the mandatory parameters are present

The Schema

To check whether the request body satisfies the schema, we must, first of all, create a schema. A sample such schema looks like

{
    properties              : [{
        keyName             : 'name',
        keyType             : 'string',
        isMandatory         : true
    },{
        keyName             : 'someBool',
        keyType             : 'boolean',
        isMandatory         : true
    },{
        keyName             : 'age',
        keyType             : 'number',
        isMandatory         : true
    },{
        keyName             : 'day',
        keyType             : 'string',
        isMandatory         : false
    },{
        keyName             : 'sArray',
        keyType             : 'array',
        isMandatory         : true,
        type                : 'boolean',
    },{
        keyName             : 'a',
        keyType             : 'array',
        isMandatory         : true,
        properties          : [{
            keyName         : 'name',
            keyType         : 'string',
            isMandatory     : true
        },{
            keyName         : 'age',
            keyType         : 'number',
            isMandatory     : true
        },{
            keyName         : 'dummy',
            keyType         : 'array',
            isMandatory     : true,
            properties      : [{
                keyName     : 'r',
                keyType     : 'string',
                isMandatory : true
            }]
        }]
    },{
        keyName             : 'ab',
        keyType             : 'object',
        isMandatory         : true,
        properties          : [{
            keyName         : 'name',
            keyType         : 'string',
            isMandatory     : true
        },{
            keyName         : 'age',
            keyType         : 'number',
            isMandatory     : true
        }]
    }]
}

It starts with a json object with key properties which must be an array and consist of objects of key properties at the level, with the key names, key types and their properties.

Each such object must have 2 keys

  • keyType
  • keyName

isMandatory is an optional key.

isMandatory

Set to true if that key at that level must be present. By default it is set to false.

keyName

This refers to the name of the key.

keyType

This refers to the type of the key.The types of keys supported are

string

This is for json keys which are expected to have string values. For example


{
    name : 'Vidur Khanna'
}

boolean

This is for json keys which are expected to have boolean values. For example


{
    isAlive : true
}

number

This is for json keys which are expected to have numerical values. For example


{
    age : 1000
}

array

This is for json keys which are expected to have numerical values. Now arrays could be of three types

  • array with same type of values. For example
{
    ages : [10, 11, 15]
}

To declare that the expected array is not an array of objects we can set the type key to the type of values expected in the array. For example

{
    keyName             : 'sArray',
    keyType             : 'array',
    isMandatory         : true,
    type                : 'boolean',
}
  • array with mixed type of values. For example
{
    test : [01, "hello world!", true]
}
  • array of objects. For example
{
    students   : [{
        name   : 's1',
        rollNo : 666
    }]
}

To declare that the expected array is an array of objects, we can set the properties key at this level with an array of properties. For example

{
    keyName             : 'a',
    keyType             : 'array',
    isMandatory         : true,
    properties          : [{
        keyName         : 'name',
        keyType         : 'string',
        isMandatory     : true
    },{
        keyName         : 'age',
        keyType         : 'number',
        isMandatory     : true
    },{
        keyName         : 'dummy',
        keyType         : 'array',
        isMandatory     : true,
        properties      : [{
            keyName     : 'r',
            keyType     : 'string',
            isMandatory : true
        }]
    }]
}

Here the key a is expected to be an array of object with each object expected to have 3 keys - name of type string, age of type number and dummy of type array of objects with key r of type string. A such valid json is

{
    a           : [{
        name    : 'dasdas',
        age     : 21,
        dummy   : [{
            r   : 'sdagdjafsd'
        }]
    }]
}

object

This is for json keys which are expected to have values which are objects(excluding arrays). For example

{
    ab          : {
        name    : 'Vidur Khanna',
        age     : 123,
    }
}

We can set the properties of this object' children as follows

{
    keyName             : 'ab',
    keyType             : 'object',
    isMandatory         : true,
    properties          : [{
        keyName         : 'name',
        keyType         : 'string',
        isMandatory     : true
    },{
        keyName         : 'age',
        keyType         : 'number',
        isMandatory     : true
    }]
}

Exposed Functions

There are two functions exposed

isValidJsonSchema

This function is to validate your schema. The only parameter it requires is the schema object. It returns true if the schema is valid else it returns false.

validate

This function is to validate the json object against your schema. The parameters it requires are the schema object and the json object to be verified. If the json object satisfies the schema it returns true else it returns an error message like "ab.age was expected to be a number".

Dependencies

Lodash and node version >= 6.2.2.