简体   繁体   中英

Joi validation schema

I have a schema where I need to validate as follow

const schema = Joi.object({
   a: Joi.string(),
   b: Joi.string(),
   c: Joi.string()
})

If any one of them is present I want to make the remaining two optional. How can I do it?

Take a look at the Joi docs at https://joi.dev/api/?v=17.3.0

Then try something like this:

const schema = Joi
  .alternatives([
    Joi.object({
      a: Joi.string().required(),
      b: Joi.string(),
      c: Joi.string(),
    }),
    Joi.object({
      a: Joi.string(),
      b: Joi.string().required(),
      c: Joi.string(),
    }),
    Joi.object({
      a: Joi.string(),
      b: Joi.string(),
      c: Joi.string().required(),
    }),
  ])
  .match('one')
  ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM