Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add request-validation plugin chinese document. #1932

Merged
merged 5 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/plugins/request-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
-->

[Chinese](request-validation-cn.md)
[Chinese](../zh-cn/plugins/request-validation.md)

# Summary
- [**Name**](#name)
Expand Down
151 changes: 151 additions & 0 deletions doc/zh-cn/plugins/request-validation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<!--
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
-->

[English](../../plugins/request-validation.md)

# 目录
- [**名字**](#名字)
- [**属性**](#属性)
- [**如何启用**](#如何启用)
- [**测试插件**](#测试插件)
- [**禁用插件**](#禁用插件)
- [**示例**](#示例)

## 名字

`request-validation` 插件用于提前验证请求向上游转发请求,可以验证请求的 `body` 及 `header` 数据。

该插件使用 `Json Schema` 进行数据验证,有关 `Json Schema` 的更多信息,请参阅 [JSON schema](https://github.com/api7/jsonschema) 。


## 属性

|名称 |必选项 |描述|
|--------- |-------- |-----------|
| header_schema |可选 |`header` 数据的 `schema` 数据结构|
| body_schema |可选 |`body` 数据的 `schema` 数据结构|


## 如何启用

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

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/get",
"plugins": {
"request-validation": {
"body_schema": {
"type": "object",
"required": ["required_payload"],
"properties": {
"required_payload": {"type": "string"},
"boolean_payload": {"type": "boolean"}
}
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8080": 1
}
}
}
```


## 测试插件

```shell
curl --header "Content-Type: application/json" \
--request POST \
--data '{"boolean-payload":true,"required_payload":"hello"}' \
http://127.0.0.1:9080/get
```

如果 `Schema` 验证失败,将返回 `400 bad request` 错误。


## 禁用插件

在路由 `plugins` 配置块中删除 `request-validation` 配置,即可禁用该插件。

```shell
curl http://127.0.0.1:9080/apisix/admin/routes/5 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/get",
"plugins": {
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8080": 1
}
}
}
```


## 示例

**验证 `Enums`:**

```shell
"body_schema": {
"type": "object",
"required": ["required_payload"],
"properties": {
"emum_payload": {
"type": "string",
enum: ["enum_string_1", "enum_string_2"]
default = "enum_string_1"
}
}
}
```

**验证 多级`Json`:**

```shell
"body_schema": {
shuaijinchao marked this conversation as resolved.
Show resolved Hide resolved
"type": "object",
"required": ["required_payload"],
"properties": {
"boolean_payload": {"type": "boolean"},
"child_element_name": {
"type": "object",
"properties": {
"http_statuses": {
"type": "array",
"minItems": 1,
"items": {
"type": "integer",
"minimum": 200,
"maximum": 599
},
"uniqueItems": true,
"default": [200, 201, 202, 203]
}
}
}
}
}
```