request-validation

note

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

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

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

String validation

  1. {
  2. "body_schema": {
  3. "type": "object",
  4. "properties": {
  5. "string_payload": {
  6. "type": "string",
  7. "minLength": 1,
  8. "maxLength": 32
  9. }
  10. }
  11. }
  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": ["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. "type": "string",
  21. "minLength": 1,
  22. "maxLength": 32,
  23. }
  24. }
  25. }
  26. }

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

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

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