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
Dependency error #588
Comments
This looks like a circular dependency. To check a we need to check b which needs to check a... |
@Marsup It's actually
I tried to answer a Stack Overflow question when ran into this issue. You can find more details there: http://stackoverflow.com/questions/28864812/using-joi-require-one-of-two-fields-to-be-non-empty |
This works but it's ugly : var schema = Joi.alternatives().try(
Joi.object().keys({
a: Joi.string().min(1),
b: Joi.string().only('')
}),
Joi.object().keys({
a: Joi.string().only(''),
b: Joi.string().min(1)
})
); Maybe we should let joi lazily evaluate the conditions without checking for circular deps (ie. remove Topo). @hueniverse I understand why it's useful in hapi, but what kind of mistake does it prevent in joi's context ? |
You need it because values can be modified so the order in which you check is important. |
This is a pretty unfriendly workaround for the case where one of two fields must be present; it gets even worse if one of three or more fields are required, and exponentially worse if you have multiple sets of fields (e.g. a or b is required and c or d is required). I can see that allowing the values to be modified does make this problematic though :-/ |
You have the |
Thanks @Marsup, I did see Hueniverse pointed out above that the values can be modified and so ordering is important; how is this worked around in |
I think the only practical approach here is to preprocess the data to validate and convert
|
It is exactly what empty does, I don't know what you're asking. |
Perhaps I misunderstood you? I thought you were referring to any.empty, which doesn't do that at all. Was it another empty that you were referring to? Anyway, I didn't mean to turn this closed ticked into a support request. Joi is clearly more targeted at API usage than form validation and so it requires a bit of work to make it behave nicely for it and that's fair enough. |
It doesn't do what ? It converts anything that matches the empty schema to undefined. |
Then I must have been doing something wrong because it didn't work that way
for me.
I'll revisit it, and thanks for your patience.
|
Thanks, I was able to get My mistake was thinking I could apply it to the result of
But this does.
|
There are two keys, if one of them is empty, the other should not be empty. I created this schema:
However it raises an error:
Is this a bug or should I create the schema in a different way?
The text was updated successfully, but these errors were encountered: