我正在为请求使用操作处理程序。在oas方案中,我将create user定义为:
/users:
post:
description: |
creates a user
operationId: createUser
x-eov-operation-handler: controllers/user.controller
tags:
- Users
requestBody:
description: User to create
required: true
content:
application/json:
schema:
properties:
email:
type: string
description: The email of the user
example: example@gmail.com
pattern: ^[w-.]+@([w-]+.)+[w-]{2,5}$
password:
type: string
description: The password of the user
example: password1234
minLength: 8
maxLength: 16
required:
- email
- password
additionalProperties: false
responses:
'201':
description: User was created
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'400':
description: Bad request
content:
application/json:
schema:
properties:
code:
description: Http status code
type: number
example: 400
message:
description: The message of the response
type: string
example: Bad request
required:
- code
- message
additionalProperties: false
…等
这些是我的验证器设置:
const openApiValidator = OpenApiValidator.middleware({
apiSpec,
operationHandlers: path.join(__dirname)
})
,但在响应中,当我尝试创建一个用户与错误的电子邮件模式,例如:examplegmail.com,而不是得到像:{400,'Bad request'}
我得到一个HTML页面。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Bad Request: request.body.email should match pattern "^[w-.]+@([w-]+.)+[w-]{2,5}$"<br> at Object.POST-/users-application/json (C:UsersmagerDesktopnetflix-api_node_modulesexpress-openapi-validatorsrcmiddlewaresopenapi.request.validator.ts:175:35)<br> at RequestValidator.validate (C:UsersmagerDesktopnetflix-api_node_modulesexpress-openapi-validatorsrcmiddlewaresopenapi.request.validator.ts:76:37)<br> at C:UsersmagerDesktopnetflix-api_node_modulesexpress-openapi-validatorsrcopenapi.validator.ts:282:49<br> at C:UsersmagerDesktopnetflix-api_node_modulesexpress-openapi-validatorsrcopenapi.validator.ts:188:20<br> at processTicksAndRejections (node:internal/process/task_queues:95:5)</pre>
</body>
</html>
我可以改变这种行为还是这是它应该是怎样的?
编辑:我也有一个错误处理后定义的openApiValidator
app.use(openApiValidator);
app.use(errorHandler);
function errorHandler(err: any, req: Request, res: Response) {
res.status(err.status || 500).json({
code: err.status,
message: err.message,
});
}
编辑2:我设法修复它我的错误处理程序缺少下一个参数,我想我可以省略它,因为我没有使用它。
我设法修复了我的错误处理程序缺少下一个参数,我想我可以省略那个,因为我没有使用它。