node.js - Swagger Nest.js ValidationPipe ApiBadRequestResponse
问题描述
我刚开始玩 Swagger,一切都很棒。我能够在一个小时左右的时间内记录所有 API,但我在解决最后一个问题时遇到了困难。
如何告诉 swagger 以ApiBadRequestResponse
下面的格式显示?
@ApiOkResponse({
type: User,
})
@ApiBadRequestResponse({
type: {
error: String,
message: ValidationError[]
}
})
@Post('/signup')
signUp(
@Body(ValidationPipe) authCredentialsDto: CredentialsDTO
): Promise<User> {
return this.userService.signUp(authCredentialsDto)
}
据我了解,swagger 不知道如何使用接口,而我必须使用类。有没有一种简单的方法来记录由ValidationPipe
. 这都是本机@nestjs
行为,所以我认为必须有一个简单的解决方案。
这是从 API 实际返回的内容:
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"username": "somename",
"password": "****"
},
"value": "****",
"property": "password",
"children": [], // ValidationError[]
"constraints": {
"matches": "password too weak"
}
}
]
解决方案
推荐阅读
- android - 三星互联网无法使用 App Links?其他浏览器/设备是否也不兼容?
- reactjs - 将组件添加到 React 的 App 组件时出现错误
- python - 如何使用模块提取当前的python文件名?
- javascript - vue.js 中的 v-for 循环
- javascript - JSON Array.Reduce 多维
- excel - 如何在 Excel 中动态计算公式的结果?
- javascript - 带有实际内容的简单 HTML 侧边栏示例
- quarkus - 如何在 Quarkus 中配置 Undertow 的 MAX_PARAMETERS 设置?
- firebase - 如何检查电子邮件已经存在于firebase颤振中的多个表中
- javascript - 为什么我的函数说没有正确处理承诺?