简体   繁体   中英

How to validate a Joi object based on a field type

I have the following Joi object:

const Content = Joi
  .object({
    type: Joi.valid('contentType1', 'contentType2')
    value: 'validate with that certain type'
  })

Is there a way to validate the field value based on the value of the field type

Got it by doing this:

const Content = Joi
  .object({
    type: Joi.valid('contentType1', 'contentType2')
    value: Joi
      .when('type', {
        is: 'contentType1',
        then: Joi.valid('Yey')
      })
      .when('type', {
        is: 'contentType2',
        then: Joi.valid('Yow')
      })
  })

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