Skip to content

xor but none required #1628

@leebickmtu

Description

@leebickmtu

Have a validation case that I'm not sure how to cover.
My existing check is this...

Joi.object().keys({
    businessDivision: Joi.number().integer().positive(),
    masterProgram: Joi.number().integer().positive(),
    profile: Joi.number().integer().positive()
}).xor('businessDivision', 'masterProgram')

Which is fine for ensuring that not both businessDivision and masterProgram are present. But it also has the side effect of requiring at least 1 of them be present.
For my use case both being absent would be just fine.

I can't use .limit(1) because of the unrelated profile key which is optional.

I know I could use .without(...) for a simple case (like above) of just 2 that need to be exclusive but that doesn't expand well to a case where maybe more keys need to be exclusive or but not any required.

Is there a simple way to do this, that also expands to the larger possible key sets as well?

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions