快乐招摇的 UI 是否为枚举提供 onChange 事件验证



我有一个 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 应该显示名称(标头参数(

您可以使用 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的。

有一个无法移植到招摇的功能列表https://github.com/glennjones/hapi-swagger/blob/master/usageguide.md#features-from-happy-that-cannot-be-ported-to-swagger

最新更新