Catalog npm
Back to packageRELEASE DETAILS

0.0.16

npm

19json-validator

Simple JSON validator

jsonvalidator
Published10 Aug 2021
Direct dependencies1
Transitive dependenciesNot calculated

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.

Declared dependencies 1

Native requirements
1 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
mocha
Environment
{
  "local_name": "mocha",
  "alias": false,
  "peer_metadata": null
}
^8.3.2development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-dwGoWxykREjJUc5xNBe6GrgnQpzuA8598hzmmXxGbSJwmNMRyo8/sUnw1lVDseBBhB6H8mAHY7wUL350EvaYhQ==",
  "shasum": "bec135e3f7c5cd8e3deddfcda83c186f77153c0a",
  "tarball": "https://registry.npmjs.org/19json-validator/-/19json-validator-0.0.16.tgz",
  "fileCount": 6,
  "unpackedSize": 24902,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhEqR7CRA9TVsSAnZWagAArCkP/i9OWFWNtO/iRfuMG0jW\n19yuq7XImjDwk6Mmb83h5GLlBlsSAZtKJvXleYoT6IMAFlBTv+kzoE9Qmxmi\n+pleKmbCHJmkAgh0x+JpXDwz0QLmrFS7vsZoGRQT9dhjJwjOSJ0zpAG/uwAL\nuoEgA4nkkUGBHi8VI7NP+deepETKrpyktAzIqKNvC+e7xq3KOJMIMhD3Tq2k\nijpkQJ9LTCKhwXbUigO3xPTT5WNMewEn8qXLUaBmRoS+9fYx3kD35VRw5z/B\n0XlYPxozE+/LY99OU8aLqS/TFMcd5IFH59ZrciUFGRHoJN+G9ga/JqCQxfVJ\nE80NCjRAQGPHLtELg4BVsjrBoNNBOCLpfW766cvxT2aHsV0sJMjWANk32uO4\n9Zm3VULq+45OrDtLRJWaTezyRmQF3I7cqBTmmE9fsKO6ZPnVg9Al8wOeFNoL\nUJs4V8nJgSLnXLW4z7Yrd7nEqnZxHO5XhoQ4RGL5SAuBdpdb/tYUrUMGzO3B\nPDhaaPjwa4DUTVdfHSKCQPBwflJpQrXch67I9NZOH2wu66cepWmE/YwWqxN/\neiglZl1o3l3P/AJIKImHUKQAqHDb6Slvzod9nOe9e0bwgYKWgdRZUiy2Coaa\nPHkIYzd/UeU1CvLF6Legwke4ZgqGwwo0T0Ky4LWKmspYfuwgtINDHfT57MbV\ngfch\r\n=W4DC\r\n-----END PGP SIGNATURE-----\r\n",
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEQCIHn0qDFbDRIMNrp8kzGuQVJNkSrB1oASsr+ujYuXCRx/AiApm/D6SzIxhO8a7vkA5/DMqizF4eytIiP2AGO5mVLidQ=="
    }
  ]
}

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_version:1dfe755c600fbbe2017e55a49981dfc439ed96861e4da781a75c87024b287bdb

Other collected versions

0.0.171 Nov0.0.1510 Aug0.0.1417 Jun0.0.1317 Jun0.0.129 Jun0.0.1126 MayView all collected versions →