Skip to content

.without(), runs before .empty() #947

@anderson-tyler

Description

@anderson-tyler
  • node version: 6.3.0
  • joi version:8.4.2

I am trying to validate two keys that cannot appear together. Both keys are optional, and are set to be empty when they are empty strings. I am currently using .without() to forbid one key when the other is present, but when the reference key is an empty string, the validation fails with the following error:

" "person" conflict with forbidden peer "group" "

It seems as if the .without() check is happening before the .empty(''), and is registering the reference key as an empty string instead of undefined.

Is there a better way to handle this, or do you have any suggestions?

var jsonApiInput = {
    person: '',
    group: 1,
    active: true
};

var schema = Joi.object().keys({
    person: Joi.number().integer().empty(''),
    group: Joi.number().integer().empty(''),
    active: Joi.boolean().empty('')
}).required().without('person', ['group']);

//output: " "person" conflict with forbidden peer "group" "
//expected:  {
                         group: 1,
                         active: true
                      }

schema.validate( jsonApiInput, schema, function (err, result) {

    if (!err) {
        console.log(result);
    } else {
        console.log(err);
    }
});

Metadata

Metadata

Assignees

Labels

bugBug or defectsupportQuestions, discussions, and general support

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions