request-validation

Description

The request-validation Plugin can be used to validate the requests before forwarding them to an Upstream service. This Plugin uses JSON Schema for validation and can be used to validate the headers and body of the request.

Attributes

NameTypeRequiredDefaultValid valuesDescription
header_schemaobjectFalseSchema for the request header data.
body_schemaobjectFalseSchema for the request body data.
rejected_codeintegerFalse400[200,…,599]Status code to show when the request is rejected.
rejected_msgstringFalseMessage to show when the request is rejected.
request-validation - 图1note

At least one of header_schema or body_schema should be filled in.

Enabling the Plugin

You can configure the Plugin on a specific Route as shown below:

  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. "rejected_msg": "customize reject message"
  14. }
  15. }
  16. },
  17. "upstream": {
  18. "type": "roundrobin",
  19. "nodes": {
  20. "127.0.0.1:8080": 1
  21. }
  22. }
  23. }'

The examples below shows how you can configure this Plugin for different validation scenarios:

Enum validation:

  1. {
  2. "body_schema": {
  3. "type": "object",
  4. "required": ["required_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 validation:

  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 validation:

  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 validation:

  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. }

Regular expression validation:

  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 validation:

  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. }

Combined validation:

  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. }

Custom rejection message:

  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. "rejected_msg": "customize reject message"
  13. }
  14. }
  15. },
  16. "upstream": {
  17. "type": "roundrobin",
  18. "nodes": {
  19. "127.0.0.1:8080": 1
  20. }
  21. }
  22. }

Example usage

Once you have configured the Plugin, it will only allow requests that are valid based on the configuration to reach the Upstream service. If not, the requests are rejected with a 400 or a custom status code you configured.

A valid request for the above configuration could look like this:

  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

Disable Plugin

To disable the request-validation Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  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. }'