npm

Check out our latest tech talk, "JavaScript Supply Chain Security" presented by VP of Security, Adam Baldwin.Watch it here »

check-json

0.0.7 • Public • Published

check-json - JSON fields checking

Copyright (c) 2014, Alberto Garbui (aka JK) www.ajk.altervista.org

All rights reserved.

Usage

check-json(object-literal)

Returns an chainable object whereon the following functions can be used.

.hasString(key)

.hasString(key)

.hasStringNotEmpty(key)

.hasOptionalString(key)

.hasOptionalStringNotEmpty(key)

.hasObject(key)

.hasNumber(key)

Example:

 
var checkJson = require('check-json');
 
var user = {
    username: 'alberto',
    password: 'MDAxMTIyMzM0NDU1NjY3Nw==',
    gender: 'male'
    activities: {
        sports: 'crossfit',
        hobby: 'electronics stuff'
    }
}
 
checkJson(user)
    .hasStringNotEmpty('username')
    .hasStringNotEmpty('password')
    .optionalString('gender')
    .optionalStringNotEmpty('activities.sports')
    .optionalString('activities.hobby')
    .check(function(err){
        if(err){
            console.log('error: ' + JSON.stringify(err)); 
        }else{
            //no errors found
        }
    };

install

npm i check-json

Downloadsweekly downloads

13

version

0.0.7

license

BSD-3-Clause

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability