[英]Does hapi-swagger ui provide onChange event validation for enum?
我有一個API /v1/test/{note?}
我為此添加了以下代碼。
module.exports = [
{
method: 'POST',
path: '/v1/test/{note?}',
config: {
description: 'Greet user',
notes: ['Use to greet a user'],
tags: ['api'],
handler: defaultHandler,
timeout:{
server:3000
},
validate: {
params: {
note: Joi.string()
.required()
.valid(['param1', 'param2', 'param3'])
.description('Notes')
a: Joi.string().required().description('for param2')
b: Joi.string().required().description('for param3')
},
headers: {
name: Joi.string().required()
},options: {
allowUnknown: true
}
}
}
}
];
在Swagger UI中,如果我從下拉列表中選擇param2,那么ui應該顯示a ;如果我選擇param3,ui應該顯示b;對於param1應該顯示name(標題參數)
您可以使用Joi驗證定義條件https://github.com/hapijs/joi/blob/v13.1.2/API.md#anywhencondition-options
{
note: Joi.string()
.required()
.valid(['param1', 'param2', 'param3'])
.description('Notes'),
a: Joi.string()
.description('for param2')
.when('note', { is: 'param2', then: Joi.required() }),
b: Joi.string()
.description('for param3')
.when('note', { is: 'param3', then: Joi.required() }),
}
但是狂喜的人並沒有完全支持這一點 。 when
實際上是引擎蓋下的alternatives
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.