request-validation

    名称

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

    如何启用

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

    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 bad request 错误。

    禁用插件

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

    枚举(Enums)验证:

    布尔(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. }

    正则表达式(Regex)验证:

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

    数组(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. }

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