https://ajv.js.org/guide/user-keywords.html
ajv关键字,例如

  • type
  • properties
  • minLength

关键字特点:验证数据是否符合某些规则

  1. ajv.addKeyword({
  2. keyword: "range",
  3. type: "number",
  4. schemaType: "array",
  5. implements: "exclusiveRange",
  6. compile: ([min, max], parentSchema) =>
  7. parentSchema.exclusiveRange === true
  8. ? (data) => data > min && data < max
  9. : (data) => data >= min && data <= max,
  10. })
  11. const schema = {range: [2, 4], exclusiveRange: true}
  12. const validate = ajv.compile(schema)
  13. console.log(validate(2.01)) // true
  14. console.log(validate(3.99)) // true
  15. console.log(validate(2)) // false
  16. console.log(validate(4)) // false

Meta-schemas

https://ajv.js.org/options.html#meta
http://json-schema.org/specification.html