ppb
ppb

Reputation: 2633

Does hapi-swagger ui provide onChange event validation for enum?

I have an API /v1/test/{note?} I have added below code for this.

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
                }
            }                               
        }
    }
];

In Swagger UI, is there any way if I select param2 from drop down then ui should display a, if I select param3 ui should display b or for param1 should display name (header parameter)

Upvotes: 0

Views: 429

Answers (1)

Max Liashuk
Max Liashuk

Reputation: 1053

You can define condition using Joi validation 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() }),
}

But hapi-swagger didn't fully support that. when is actually alternatives under the hood.

There is a list of features that can't be ported to swagger https://github.com/glennjones/hapi-swagger/blob/master/usageguide.md#features-from-hapi-that-cannot-be-ported-to-swagger

Upvotes: 0

Related Questions