Joi validation multiple conditions

前端 未结 2 1359
粉色の甜心
粉色の甜心 2020-12-15 05:12

I have the following schema:

var testSchema = Joi.object().keys({
    a: Joi.string(), 
    b: Joi.string(), 
    c: Joi.string().when(\'a\', {\'is\': \'aval         


        
2条回答
  •  借酒劲吻你
    2020-12-15 05:57

    The answer by Gergo Erdosi didn't work with Joi 14.3.0, this resulted in an OR condition:

    a === 'avalue' || b === 'bvalue'

    The following worked for me:

    var schema = {
      a: Joi.string(),
      b: Joi.string(),
      c: Joi.string().when(
        'a', {
          is: 'avalue',
          then: Joi.when(
            'b', {
              is: 'bvalue',
              then: Joi.string().required()
            }
          )
        }
      )
    };
    

    This results in a === 'avalue' && b === 'bvalue'

提交回复
热议问题