【发布时间】:2018-09-05 11:24:45
【问题描述】:
我正在使用swagger-express-validator 来验证小型 API 服务器的输入(使用 Swagger 2 格式)
我的path定义如下
/api/v1/users:
post:
produces:
- "application/json"
parameters:
- in: body
name: ids
description: Array of user ids to be processed
required: true
schema:
$ref: "#/definitions/ArrayOfIds"
responses:
200:
description: success
ArrayOfIds定义如下
Id:
type: string
ArrayOfIds:
type: array
items:
$ref: "#/definitions/Id"
向服务器发送post请求如下:
POST /api/v1/users HTTP/1.1
Content-Type: application/json
Accept: application/json
Host: localhost:3000
Connection: close
User-Agent: Paw/3.1.7 (Macintosh; OS X/10.13.6) GCDHTTPRequest
Content-Length: 35
{
"ids": ["abcd12345"]
}
导致错误
Request Invalid: POST /api/v1/users
[ { keyword: 'type',
dataPath: '',
schemaPath: '#/type',
params: { type: 'array' },
message: 'should be array' } ]
但是,我可以在我的 Express 路由控制器代码中访问 req.body.ids,它包含正确的值 ['1234abc']。
您知道验证者为何抱怨该请求吗?我觉得很好。
【问题讨论】:
-
我会说相关而不是欺骗,因为那些指的是响应对象而不是请求参数。
标签: rest validation express swagger swagger-2.0