hapi swagger joi.when issue

110 views Asked by At

I have this joi schema used to validate payload for an api

const schema = Joi.object({ 
  type: Joi.string()
    .valid('user', 'customer')
    .required(),
  user_id: Joi.when('type', {
    is: Joi.string().valid('user'),
    then: Joi.array().items(Joi.string()).min(1).required(),
    otherwise: Joi.forbidden(),
  }),
  customer_id: Joi.when('type', {
    is: Joi.string().valid('customer'),
    then: Joi.string().required(),
    otherwise: Joi.forbidden(),
  }),
}).label('con_model');

I am using hapi-swagger to generate the documentation. it also auto generates yaml from swagger.json file.

the yaml generated for this particular model is:

reports_model:
    type: object
    properties:      
      type:
        $ref: '#/definitions/type'     
      customer_id:
        type: string
      user_id:
        $ref: '#/definitions/user_id'
    required:
      - type
      - customer_id
      - customer_id
      - user_id
      - user_id

in https://editor.swagger.io/ I get error for this model which says:

Structural error at definitions.con_model.required
should NOT have duplicate items (items ## 4 and 3 are identical)
i: 3
j: 4

Any idea why i am getting that duplication and how to remove that? the payload works in such a way that if type is 'user' then only user_id should be given and if the type is 'customer' then only customer_id should be accepted.

0

There are 0 answers