Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isPresent option to object dependencies #2762

Merged

Conversation

perrin4869
Copy link
Contributor

I needed a feature such as in this StackOverflow question.

When you have a schema such as:

const schema = Joi.object({
  a: Joi.string().default(() => null),
  b: Joi.string().default(() => null)
}).oxor('a', 'b');

You may want schema.validate({}) to pass, however, currently this will fail because the oxor rule will resolve a and b to null, and the current check is against undefined.
This PR adds a new option, isPresent, that allows for something like:

const schema = Joi.object({
  a: Joi.string().default(() => null),
  b: Joi.string().default(() => null)
}).oxor('a', 'b', { isPresent: (resolved) => resolved !== undefined && resolved !== null });

In this case, the oxor (or any of the other dependency options, such as xor or and) will accept undefined and null values as empty.

@Marsup
Copy link
Collaborator

Marsup commented Aug 20, 2022

Hi,

Just wondering, do you actually need a function for that? I mean, it looks more flexible, but it could as well be a flag that also checks for null values. I don't know if presence can or should be defined by any other keyword.

@perrin4869
Copy link
Contributor Author

I prefer the extra flexibility of a function, but i could go either way really... If this ever gets reviewed that is 😅

@Marsup Marsup self-assigned this Oct 22, 2022
@Marsup Marsup added the feature New functionality or improvement label Oct 22, 2022
@Marsup Marsup added this to the 17.7.0 milestone Oct 22, 2022
@Marsup Marsup force-pushed the feature/object-dependencies-is-present branch from b7b79d7 to 1e94298 Compare October 22, 2022 10:46
@Marsup Marsup force-pushed the feature/object-dependencies-is-present branch from 00d0d14 to 1c4a71d Compare October 22, 2022 11:38
@Marsup
Copy link
Collaborator

Marsup commented Oct 22, 2022

I've changed a bit the implementation but it should be good now. Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants