Catalog npm
PACKAGE EXPLORERView in registry

19json-validator

npm

Simple JSON validator

jsonvalidator
Versions collected17
Direct dependencies1From release 0.0.17
Last observed20 Oct, 18:24 UTC

README

Current package documentation

19json-validator

Simple JSON schema validator.

Install

$ npm install 19json-validator

Usage

// import
const Validator = require('19json-validator');

// input
const input = {

    id: 1,

    name: 'tom',

    email: 'tom@example.com',

    description: '1a student',

    emailAgent: 'gmail',

    mother: null,

    father: {

        name: 'father tom',

        age: 48
    },

    skills: [
        
        'read', 
        
        'write', 
        
        'speak'
    ],

    orderBy: '[{"field":"id","direction":"asc"}]'
};

// schema
const schema = {

    // id field
    id: {

        // is optional
        optional: true,

        // allow type
        type: ['number', 'string'],

        // regexp
        regex: /^[1-9][0-9]*$/
    },

    // name field
    name: {

        // optional when id present
        optional: input.hasOwnProperty('id'),

        // type
        type: 'string',

        // not allow empty
        empty: false
    },

    // email field
    email: {

        // optional when id present
        optional: input.hasOwnProperty('id'),

        // is email
        isEmail: true
    },

    // description field
    description: {

        // optional when id present
        optional: input.hasOwnProperty('id'),

        // type
        type: 'string',

        // allow empty
        empty: true
    },

    emailAgent:{

        inValues: [

            'gmail',

            'ymail',

            'hotmail'
        ]
    },

    // mother field
    mother: {

        // optional
        optional: true,

        // nullable
        nullable: true,
        
        // is json
        isJson: true,

        // child schema
        childSchema: {

            // child name field
            name: {

                type: 'string'
            },

            // child age field
            age: {

                // type
                type: ['number', 'string'],

                // regex
                regex: /^[1-9][0-9]*$/
            }
        }
    },

    // father field
    father: {

        // optional
        optional: true,

        // nullable
        nullable: true,
        
        // is json
        isJson: true,

        // child schema
        childSchema: {

            // child name field
            name: {

                type: 'string'
            },

            // child age field
            age: {

                // type
                type: ['number', 'string'],

                // regex
                regex: /^[1-9][0-9]*$/
            }
        }
    },

    // skills field
    skills: {

        // optional
        optional: true,
        
        // is array
        isArray: true,

        // check for each children element
        childrenSchema: {

            // check directly input value
            direct: true,

            // type
            type: 'string'
        }
    },

    // orderBy field
    orderBy: {

        // optional
        optional: true,
        
        // is array
        isArray: true,        

        // check for each children element
        childrenSchema: {

            // elements field
            field: {

                type: 'string'
            },

            // elements direction
            direction: {

                // type
                type: 'string',

                // regexp
                regex: /^(a|de)sc$/
            }
        }
    }
};

try {

    // do validation
    Validator.validate(input, schema);

} catch(err) {

    // log error
    console.error(err);
}

Test

$ npm test

Usage

validate(input, schema, options)

Schema config

The following config options can help you to build your own json check schema for validation.

optional (boolean, default: false)

true = allow target key missing, false = must contain key.

nullable (boolean)

true = allow target value is (null) object or 'null' string, false = target value must not equal to (null) object or 'null' string.

type (string | array<string>)

restrict target value of key should equal to given types, use underlying typeof to perform type checking.

regex (object instanceof RegExp)

restrict target value of key should match to given regular expression, this checking will add a empty string to the end of the value for non-string value.

empty (boolean, default: true)

true = accept empty value, false = reject empty value.

isArray (boolean, default: false)

true = value must be a valid array object or a valid json array string.

isJson (boolean, default: false)

true = value must be a valid object or a valid json object string.

isEmail (boolean, default: false)

true = input value must be in valid email format.

childSchema (object)

check input value with provided child schema, use for nested json object structure.

childrenSchema (object)

check input (array) values with provided child schema, use for json array checking.

direct (boolean, default: false)

true = use schema to check the input value directly, use for non-object input checking.

options

allowExtraProperties (boolean, default: true)

true = allow properties that not defined in schema, false = do not allow properties that not defined in schema

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 17

Collected releases
VersionPublishedDirect dependenciesOpen version
0.0.17Registry latest1 Nov 20211
0.0.1610 Aug 20211
0.0.1510 Aug 20211
0.0.1417 Jun 20211
0.0.1317 Jun 20211
0.0.129 Jun 20211
0.0.1126 May 20211
0.0.1026 May 20211
0.0.930 Apr 20211
0.0.830 Apr 20211
0.0.729 Apr 20211
0.0.629 Apr 202176
0.0.529 Apr 20211
0.0.429 Apr 20211
0.0.329 Apr 20211
0.0.229 Apr 20210
0.0.129 Apr 20210

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
19site/19json-validator

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 18:24 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:e89ab35e1263d9cbd90af50fd9c99b1bee489aa4594be35d6d1b3d3640e43f1f