Or Assayag
Or Assayag

Reputation: 6336

Joi validate 2 schemas conditionally - Invalid schema content

I'm trying to add conidial schema according of the value of the isCat field in the request body, here is what I have so far: ror [ERR_ASSERTION]: Invalid schema content` error.
What am I missing? Thanks.

Upvotes: 0

Views: 292

Answers (1)

Saurabh Rahate
Saurabh Rahate

Reputation: 91

Try this

Joi.object({
  params: Joi.object({
    id: Joi.number().required().integer().min(1),
  }),
  body: Joi.object({
    code: Joi.string().required(),
  }).when(Joi.ref("$isValid"), {
    is: Joi.equal(true),
    then: Joi.object({
      body: Joi.object({
        ...entity,
      }),
    }),
    otherwise: Joi.object({
      body: Joi.object({
        ...put,
      }),
    }),
  }),
});

Verify the reference given by you is right Joi.ref("$isValid").
If you provide right reference, this schema will work as you expect.

Upvotes: 2

Related Questions