Using Joi, require one of two fields to be non empty

后端 未结 3 2001
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 21:28

If I have two fields, I\'d just like to validate when at least one field is a non empty string, but fail when both fields are empty strings.

Something like this does

3条回答
  •  半阙折子戏
    2021-01-07 21:41

    If you want to express the dependency between 2 fields without having to repeat all other parts of the object, you could use when:

    var schema = Joi.object().keys({
      a: Joi.string().allow(''),
      b: Joi.string().allow('').when('a', { is: '', then: Joi.string() })
    }).or('a', 'b');
    

提交回复
热议问题