I've been using Joi for validation, and I've encountered a situation that I'm having trouble with. I have an object that sometimes includes an id field (for editing) and other times it doesn't (for creating).
My goal is to validate that when the "id" field is not present, certain other fields must be included.
Currently, the only solution that works for me is when the id is null but still exists in the object. Is there a way to handle a non-existing key without resorting to setting it to null (which feels like a hack)?
joi.object().keys({
id: joi
.number()
.min(0)
.allow(null),
someOtherField1: joi.when("id", { is: null, then: joi.required() })
});
Any help or advice on this matter would be greatly appreciated!