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官網整理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章