request-validation

描述

request-validation 插件用于提前验证请求向上游转发请求,可以验证请求的 bodyheader 数据。

该插件使用 Json Schema 进行数据验证,有关 Json Schema 的更多信息,请参阅 JSON schema

属性

注意,header_schemabody_schema 至少填写其中一个

NameTypeRequirementDefaultValidDescription
header_schemaobject可选header 数据的 schema 数据结构
body_schemaobject可选body 数据的 schema 数据结构
rejected_codeinteger可选400[200,…,599]自定义拒绝状态码
rejected_msgstring可选自定义拒绝信息

如何启用

创建一条路由并在该路由上启用 request-validation 插件:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/get",
  4. "plugins": {
  5. "request-validation": {
  6. "body_schema": {
  7. "type": "object",
  8. "required": ["required_payload"],
  9. "properties": {
  10. "required_payload": {"type": "string"},
  11. "boolean_payload": {"type": "boolean"}
  12. }
  13. }
  14. "rejected_msg": "customize reject message"
  15. }
  16. },
  17. "upstream": {
  18. "type": "roundrobin",
  19. "nodes": {
  20. "127.0.0.1:8080": 1
  21. }
  22. }
  23. }'

测试插件

  1. curl --header "Content-Type: application/json" \
  2. --request POST \
  3. --data '{"boolean-payload":true,"required_payload":"hello"}' \
  4. http://127.0.0.1:9080/get

如果 Schema 验证失败,将返回 400 状态码与相应的拒绝信息。

禁用插件

在路由 plugins 配置块中删除 request-validation 配置,即可禁用该插件。

  1. curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/get",
  4. "plugins": {
  5. },
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:8080": 1
  10. }
  11. }
  12. }'

示例

枚举(Enums)验证:

  1. {
  2. "body_schema": {
  3. "type": "object",
  4. "required": ["enum_payload"],
  5. "properties": {
  6. "enum_payload": {
  7. "type": "string",
  8. "enum": ["enum_string_1", "enum_string_2"],
  9. "default": "enum_string_1"
  10. }
  11. }
  12. }
  13. }

布尔(Boolean)验证:

  1. {
  2. "body_schema": {
  3. "type": "object",
  4. "required": ["bool_payload"],
  5. "properties": {
  6. "bool_payload": {
  7. "type": "boolean",
  8. "default": true
  9. }
  10. }
  11. }
  12. }

数字范围(Number or Integer)验证:

  1. {
  2. "body_schema": {
  3. "type": "object",
  4. "required": ["integer_payload"],
  5. "properties": {
  6. "integer_payload": {
  7. "type": "integer",
  8. "minimum": 1,
  9. "maximum": 65535
  10. }
  11. }
  12. }
  13. }

字符串长度(String)验证:

  1. {
  2. "body_schema": {
  3. "type": "object",
  4. "required": ["string_payload"],
  5. "properties": {
  6. "string_payload": {
  7. "type": "string",
  8. "minLength": 1,
  9. "maxLength": 32
  10. }
  11. }
  12. }
  13. }

正则表达式(Regex)验证:

  1. {
  2. "body_schema": {
  3. "type": "object",
  4. "required": ["regex_payload"],
  5. "properties": {
  6. "regex_payload": {
  7. "type": "string",
  8. "minLength": 1,
  9. "maxLength": 32,
  10. "pattern": "[[^[a-zA-Z0-9_]+$]]"
  11. }
  12. }
  13. }
  14. }

数组(Array)验证:

  1. {
  2. "body_schema": {
  3. "type": "object",
  4. "required": ["array_payload"],
  5. "properties": {
  6. "array_payload": {
  7. "type": "array",
  8. "minItems": 1,
  9. "items": {
  10. "type": "integer",
  11. "minimum": 200,
  12. "maximum": 599
  13. },
  14. "uniqueItems": true,
  15. "default": [200, 302]
  16. }
  17. }
  18. }
  19. }

多字段组合(Multiple Fields)验证:

  1. {
  2. "body_schema": {
  3. "type": "object",
  4. "required": ["boolean_payload", "array_payload", "regex_payload"],
  5. "properties": {
  6. "boolean_payload": {
  7. "type": "boolean"
  8. },
  9. "array_payload": {
  10. "type": "array",
  11. "minItems": 1,
  12. "items": {
  13. "type": "integer",
  14. "minimum": 200,
  15. "maximum": 599
  16. },
  17. "uniqueItems": true,
  18. "default": [200, 302]
  19. },
  20. "regex_payload": {
  21. "type": "string",
  22. "minLength": 1,
  23. "maxLength": 32,
  24. "pattern": "[[^[a-zA-Z0-9_]+$]]"
  25. }
  26. }
  27. }
  28. }

自定义拒绝信息:

  1. {
  2. "uri": "/get",
  3. "plugins": {
  4. "request-validation": {
  5. "body_schema": {
  6. "type": "object",
  7. "required": ["required_payload"],
  8. "properties": {
  9. "required_payload": {"type": "string"},
  10. "boolean_payload": {"type": "boolean"}
  11. }
  12. },
  13. "rejected_msg": "customize reject message"
  14. }
  15. },
  16. "upstream": {
  17. "type": "roundrobin",
  18. "nodes": {
  19. "127.0.0.1:8080": 1
  20. }
  21. }
  22. }