简体   繁体   中英

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

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 not validate

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

When validating against

{a: 'aa', b: ''}

The or condition only tests for the presence of either key a or b , but does test whether the condition for a or b is true. Joi.string() will fail for empty strings.

Here is gist with some test cases to demonstrate

http://requirebin.com/?gist=84c49d8b81025ce68cfb

Code below worked for me. I used alternatives because .or is really testing for the existence of keys and what you really wanted was an alternative where you would allow one key or the other to be empty.

var console = require("consoleit");
var Joi = require('joi');

var schema = Joi.alternatives().try(
  Joi.object().keys({
    a: Joi.string().allow(''),
    b: Joi.string()
    }),
  Joi.object().keys({
    a: Joi.string(),
    b: Joi.string().allow('')
    })
);

var tests = [
  // both empty - should fail
  {a: '', b: ''},
  // one not empty - should pass but is FAILING
  {a: 'aa', b: ''},
  // both not empty - should pass
  {a: 'aa', b: 'bb'},
  // one not empty, other key missing - should pass
  {a: 'aa'}
];

for(var i = 0; i < tests.length; i++) {
  console.log(i, Joi.validate(tests[i], schema)['error']);
}

An alternative way of using Joi.when() that worked for me:

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

.or('a', 'b') prevents a AND b being null (as opposed to '').

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');

我在https://stackoverflow.com/a/61212051/15265372回答中面临的问题是,如果给出了两个字段,您将不会遇到任何错误,如果您想修复该问题,则需要替换or使用xor

&. Simply u can use '.and()'

- it will not pass if either one of the field is empty

var schema = Joi.object().keys({
    a: Joi.string(),
    b: Joi.string()
}).and('a', 'b');

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