ajv参数验证
1.验证枚举类型 var schema = { "properties": { "data": { "type": "object", "required": ["code", "status", "message", "data", "token"], "properties": { "code": { "type": "number" }, "status": { "type": "number", "enum": [0, 1] //枚举 }, "message": { "type": "string" }, "data": { "type": "array", //数组 items:{ type:"number" } }, "token": { "type": "string" } } } } }; 2.验证email(format、minLength、minimum、default) const Ajv = require('ajv'); let schema = { type: 'object', required: ['username', 'email', 'password'], properties: { username: { type: 'string', minLength: 4 }, email: { type: 'string', format: 'email' }, password: { type: 'string', minLength: 6 }, age: { type: 'integer', minimum: 0 }, sex: { enum: ['boy', 'girl', 'secret'], default: 'secret' }, } }; let ajv = new Ajv(); let validate = ajv.compile(schema); let valid = validate(data); if (!valid) console.log(validate.errors); 3.if/then/else { type: "object", if: {properties: {foo: {minimum: 10}}}, then: {required: ["bar"]}, else: {required: ["baz"]} } { type: "integer", minimum: 1, maximum: 1000, if: {minimum: 100}, then: {multipleOf: 100}, else: { if: {minimum: 10}, then": {multipleOf: 10} } } 4.regex: const schema = { type: "object", properties: { foo: {type: "string", regexp: "/foo/i"}, bar: {type: "string", regexp: {pattern: "bar", flags: "i"}}, }, } 5. Keywords for arrays const schema = { type: "array", uniqueItemProperties: ["id", "name"], } const validData = [{id: 1}, {id: 2}, {id: 3}] const invalidData1 = [ {id: 1}, {id: 1}, // duplicate "id" {id: 3}, ] const invalidData2 = [ {id: 1, name: "taco"}, {id: 2, name: "taco"}, // duplicate "name" {id: 3, name: "salsa"}, ] 6.验证日期格式 onst schema = { type: "object", dynamicDefaults: { ts: "datetime", r: {func: "randomint", args: {max: 100}}, id: {func: "seq", args: {name: "id"}}, }, properties: { ts: { type: "string", format: "date-time", }, r: { type: "integer", minimum: 0, exclusiveMaximum: 100, }, id: { type: "integer", minimum: 0, }, }, } const data = {} ajv.validate(data) // true 7. JSON data type ==Type can be: number, integer, string, boolean, array, object or null== 1.复合类型: {type: ["number", "string"]} 2. nullable:This keyword can be used to allow null value in addition to the defined type. { "type": "string", "nullable": true }, { "type": ["string", "null"] } 3.Keywords for numbers: maximum / minimum and exclusiveMaximum / exclusiveMinimum schema: {type: "number", not: {minimum: 3}} 4.Keywords for strings maxLength / minLength schema: {type: "string", maxLength: 5} {type: "string", minLength: 2} 3.pattern: 内容为“正则” schema: {type: "string", pattern: "[abc]+", enum: ["foo", "bar"]} 5.format The value of the keyword should be a string. The data to be valid should match the format with this name. Ajv does not include any formats, they can be added with ajv-formats (opens new window) plugin. Example schema: {type: "string", format: "ipv4"} 6.验证array 1. { "type": "array", items:{ type:'number' }, "minItems": 3, "maxItems": 5, "uniqueItems": true } 2.{ type: "array", items: [{type: "integer"}, {type: "string"}] }
本文来自ajv官网整理