request-validation

    名称

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

    该插件使用 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

    禁用插件

    在路由 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)验证:

    布尔(Boolean)验证:

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

    数字范围(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. "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. "properties": {
    5. "array_payload": {
    6. "type": "array",
    7. "minItems": 1,
    8. "items": {
    9. "type": "integer",
    10. "minimum": 200,
    11. "maximum": 599
    12. },
    13. "uniqueItems": true,
    14. "default": [200, 302]
    15. }
    16. }
    17. }
    18. }

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

    自定义拒绝信息:

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