19json-validator 中文文档教程

发布于 3年前 浏览 22 项目主页 更新于 3年前

19json-validator

简单的 JSON 模式验证器。

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

以下配置选项可以帮助您构建自己的 json 检查模式以进行验证。

optional (boolean, default: false)

true = 允许目标键丢失,false = 必须包含键。

nullable (boolean)

true = 允许目标值为(null)对象或“null”字符串,false = 目标值不得等于(null)对象或“null”字符串。

type (string | array<string>)

限制键的目标值应该等于给定的类型,使用底层 typeof 来执行类型检查。

regex (object instanceof RegExp)

限制键的目标值应匹配给定的正则表达式,此检查将在非字符串值的值末尾添加一个空字符串。

empty (boolean, default: true)

true = 接受空值,false = 拒绝空值。

isArray (boolean, default: false)

true = 值必须是有效的数组对象或有效的 json 数组字符串。

isJson (boolean, default: false)

true = 值必须是有效对象或有效的 json 对象字符串。

isEmail (boolean, default: false)

true = 输入值必须采用有效的电子邮件格式。

childSchema (object)

使用提供的子模式检查输入值,用于嵌套的 json 对象结构。

childrenSchema (object)

使用提供的子模式检查输入(数组)值,用于 json 数组检查。

direct (boolean, default: false)

true = 使用模式直接检查输入值,用于非对象输入检查。

options

allowExtraProperties (boolean, default: true)

true = 允许模式中未定义的属性,false = 不允许模式中未定义的属性

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

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文