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-field-validator

v0.0.2

Published

Um pacote para verificar se um json contem todos os campos desejados

Downloads

11

Readme

json-field-validator

This is a npm package to make validation of jsons more easy.

Motivation

For developers who use jsons as request body or query it can become cumbersome to check every field. This package exists for you, the developr, to create a json template and leave to the program to identify any problems with it.

Installation

This is a npm package to be used with node.

To install use:

npm i json-field-validator

How to use

To use this package you'll need to create a new validator using a template as follows:

const jsonVal=require('json-field-validator')

//create a template with data types

let data={
    Name:"string",
    zip:"any",
    address:{
        street:'string',
        number:'number'
    }
}

let chk=new jsonVal(data);

Any datatype currently supported by JS is supported here, if you are not sure what data type to use just create a template as following:

let data={
    Name:typeof "hello",
    zip:"any",
    address:{
        street:'string',
        number:typeof 42
    }
}

You can use ''any'' to skip the type validation for the field.

Methods included

validateJSON

To validade a single JSON

jsonValidator.validateJSON(object: object, options?: {
    throwError: boolean;
}): [string]

validateJSONArray

To validade an array of JSONs

jsonValidator.validateJSONArray(array: [object], options?: {
    throwError: boolean;
}): [string]

Both methods will validade one json and return an array containing all errors found as strings, if the throwError in the options is set to true the package will raise an error with all errors found.

obs: for the array method the object position will also be included in the error

Output examples

Single object

All single objects examples were made using the following object:

let data2={
    Name:"string",
    //zip:400,
    address:{
        street:'string',
        number:400
    }
}
Single object fail - error thrown
let errors=chk.validateJSON(data2,{throwError:true})

console.log(JSON.stringify(errors));

Will output to:

image

Single object fail - no error thrown
let errors=chk.validateJSON(data2)

console.log(JSON.stringify(errors));

Will output to:

image


Array of objects

All array objects examples were made using the following array:

let data2={
    Name:"string",
    zip:400,
    address:{
        street:'string',
        number:"400"
    }
}
let data3={
    Name:"string",
    zip:"440",
    address:{
        number:45
    }
}
let data4={
    Name:"string",
    zip:400,
    address:{
    }
}
let datas=[
    data2,
    data3,
    data4
]
Array object fail - error thrown
let errors=chk.validateJSONArray(datas,{throwError:true})

console.log(JSON.stringify(errors));

Will output to:

image

Array object fail - no error thrown
let errors=chk.validateJSONArray(datas)

console.log(JSON.stringify(errors));

Will output to:

image

Contribute

If you find this package useful and found a bug or have some ideia for it just head over to the repo and let me know!

Credits

Renato fernandes (Me!)