• 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官网整理

  • 相关阅读:
    git和github的使用
    C语言 -- 链表操作
    数据结构之表达式求值(C++版)
    数据结构之一元多项式加法(C++版)
    数据结构之邻接矩阵(C++版)
    数据结构之二叉树,赫夫曼树(C++版)
    数据结构之循环队列(C++版)
    数据结构之顺序栈(C++版)
    数据结构之单链表(C++版)
    数据结构之顺序表(C++版)
  • 原文地址:https://www.cnblogs.com/qiyc/p/15200477.html
Copyright © 2020-2023  润新知