功能说明

request-validation插件用于提前验证向上游服务转发的请求。该插件使用JSON Schema机制进行数据验证,可以验证请求的body及header数据。

运行属性

插件执行阶段:认证阶段 插件执行优先级:220

配置字段

名称数据类型填写要求默认值描述
header_schemaobject选填-配置用于验证请求header的JSON Schema
body_schemaobject选填-配置用于验证请求body的JSON Schema
rejected_codenumber选填403配置请求被拒绝时返回的HTTP状态码
rejected_msgstring选填-配置请求被拒绝时返回的HTTP应答Body
enable_swaggerbool选填false配置是否开启swagger文档验证
enable_oas3bool选填false配置是否开启OAS3文档验证

校验规则对header和body是一样的,下面以body为例说明

配置示例

枚举(Enum)验证

  1. body_schema:
  2. type: object
  3. required:
  4. - enum_payload
  5. properties:
  6. enum_payload:
  7. type: string
  8. enum:
  9. - enum_string_1
  10. - enum_string_2
  11. default: enum_string_1

布尔(Boolean)验证

  1. body_schema:
  2. type: object
  3. required:
  4. - boolean_payload
  5. properties:
  6. boolean_payload:
  7. type: boolean
  8. default: true

数字范围(Number or Integer)验证

  1. body_schema:
  2. type: object
  3. required:
  4. - integer_payload
  5. properties:
  6. integer_payload:
  7. type: integer
  8. minimum: 1
  9. maximum: 10

字符串长度(String)验证

  1. body_schema:
  2. type: object
  3. required:
  4. - string_payload
  5. properties:
  6. string_payload:
  7. type: string
  8. minLength: 1
  9. maxLength: 10

正则表达式(Regex)验证

  1. bodyschema:
  2. type: object
  3. required:
  4. - regex_payload
  5. properties:
  6. regex_payload:
  7. type: string
  8. minLength: 1
  9. maxLength: 10
  10. pattern: “^[a-zA-Z0-9]+$

数组(Array)验证

  1. body_schema:
  2. type: object
  3. required:
  4. - array_payload
  5. properties:
  6. array_payload:
  7. type: array
  8. minItems: 1
  9. items:
  10. type: integer
  11. minimum: 1
  12. maximum: 10
  13. uniqueItems: true
  14. default: [1, 2, 3]

多字段组合(Combined)验证

  1. bodyschema:
  2. type: object
  3. required:
  4. - boolean_payload
  5. - array_payload
  6. - regex_payload
  7. properties:
  8. boolean_payload:
  9. type: boolean
  10. array_payload:
  11. type: array
  12. minItems: 1
  13. items:
  14. type: integer
  15. minimum: 1
  16. maximum: 10
  17. uniqueItems: true
  18. default: [1, 2, 3]
  19. regex_payload:
  20. type: string
  21. minLength: 1
  22. maxLength: 10
  23. pattern: “^[a-zA-Z0-9]+$

自定义拒绝信息

  1. body_schema:
  2. type: object
  3. required:
  4. - boolean_payload
  5. properties:
  6. boolean_payload:
  7. type: boolean
  8. rejected_code: 403
  9. rejected_msg: “请求被拒绝”