|
| 1 | +<!-- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +--> |
| 19 | + |
| 20 | +[Chinese](request-validation-cn.md) |
| 21 | + |
| 22 | +# Summary |
| 23 | +- [**Name**](#name) |
| 24 | +- [**Attributes**](#attributes) |
| 25 | +- [**How To Enable**](#how-to-enable) |
| 26 | +- [**Test Plugin**](#test-plugin) |
| 27 | +- [**Disable Plugin**](#disable-plugin) |
| 28 | +- [**Examples**](#examples) |
| 29 | + |
| 30 | + |
| 31 | +## Name |
| 32 | + |
| 33 | +`request-validation` plugin validates the requests before forwarding to an upstream service. The validation plugin uses |
| 34 | +json-schema to validate the schema. The plugin can be used to validate the headers and body data. |
| 35 | + |
| 36 | +For more information on schema, refer to [JSON schema](https://github.com/api7/jsonschema) for more information. |
| 37 | + |
| 38 | +## Attributes |
| 39 | + |
| 40 | +|Name |Requirement |Description| |
| 41 | +|--------- |-------- |-----------| |
| 42 | +| header_schema |optional |schema for the header data| |
| 43 | +| body_schema |optional |schema for the body data| |
| 44 | + |
| 45 | +## How To Enable |
| 46 | + |
| 47 | +Create a route and enable the request-validation plugin on the route: |
| 48 | + |
| 49 | +```shell |
| 50 | +curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' |
| 51 | +{ |
| 52 | + "uri": "/get", |
| 53 | + "plugins": { |
| 54 | + "request-validation": { |
| 55 | + "body_schema": { |
| 56 | + "type": "object", |
| 57 | + "required": ["required_payload"], |
| 58 | + "properties": { |
| 59 | + "required_payload": {"type": "string"}, |
| 60 | + "boolean_payload": {"type": "boolean"} |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + }, |
| 65 | + "upstream": { |
| 66 | + "type": "roundrobin", |
| 67 | + "nodes": { |
| 68 | + "127.0.0.1:8080": 1 |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | +
|
| 74 | +## Test Plugin |
| 75 | +
|
| 76 | +```shell |
| 77 | +curl --header "Content-Type: application/json" \ |
| 78 | + --request POST \ |
| 79 | + --data '{"boolean-payload":true,"required_payload":"hello"}' \ |
| 80 | + http://127.0.0.1:9080/get |
| 81 | +``` |
| 82 | +
|
| 83 | +If the schema is violated the plugin will yield a `400` bad request. |
| 84 | +
|
| 85 | +## Disable Plugin |
| 86 | +
|
| 87 | +Remove the corresponding json configuration in the plugin configuration to disable the `request-validation`. |
| 88 | +APISIX plugins are hot-reloaded, therefore no need to restart APISIX. |
| 89 | +
|
| 90 | +```shell |
| 91 | +curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' |
| 92 | +{ |
| 93 | + "uri": "/get", |
| 94 | + "plugins": { |
| 95 | + }, |
| 96 | + "upstream": { |
| 97 | + "type": "roundrobin", |
| 98 | + "nodes": { |
| 99 | + "127.0.0.1:8080": 1 |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +## Examples: |
| 107 | + |
| 108 | +**Using ENUMS:** |
| 109 | + |
| 110 | +```shell |
| 111 | +"body_schema": { |
| 112 | + "type": "object", |
| 113 | + "required": ["required_payload"], |
| 114 | + "properties": { |
| 115 | + "emum_payload": { |
| 116 | + "type": "string", |
| 117 | + enum: ["enum_string_1", "enum_string_2"] |
| 118 | + default = "enum_string_1" |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | + |
| 125 | +**JSON with multiple levels:** |
| 126 | + |
| 127 | +```shell |
| 128 | +"body_schema": { |
| 129 | + "type": "object", |
| 130 | + "required": ["required_payload"], |
| 131 | + "properties": { |
| 132 | + "boolean_payload": {"type": "boolean"}, |
| 133 | + "child_element_name": { |
| 134 | + "type": "object", |
| 135 | + "properties": { |
| 136 | + "http_statuses": { |
| 137 | + "type": "array", |
| 138 | + "minItems": 1, |
| 139 | + "items": { |
| 140 | + "type": "integer", |
| 141 | + "minimum": 200, |
| 142 | + "maximum": 599 |
| 143 | + }, |
| 144 | + "uniqueItems": true, |
| 145 | + "default": [200, 201, 202, 203] |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
0 commit comments