Search code examples
javascriptnode.jsexpressjoi

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.


Solution

  • 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.