I have the following code to verify my range object is always valid. Namely, if the max is available, then min should be smaller than that.
const rangeSchema = Joi.object().keys({
min: Joi.number().when('max', {
is: Joi.number(),
then: Joi.number().less(Joi.ref('max')),
}),
max: Joi.number(),
});
const rangeObj = {
min: -100,
};
const result1 = Joi.validate(rangeObj, rangeSchema);
console.log(result1);
But I got the following error
ValidationError: child "min" fails because ["min" references "max" which is not a number]
When I add constraints on both max and min
const rangeSchema = Joi.object().keys({
min: Joi.number().when('max', {
is: Joi.number(),
then: Joi.number().less(Joi.ref('max')),
}),
max: Joi.number().when('min', {
is: Joi.number(),
then: Joi.number().greater(Joi.ref('min')),
}),
});
const rangeObj = {
min: -100,
};
const result1 = Joi.validate(rangeObj, rangeSchema);
console.log(result1);
I got the following error Error: item added into group max created a dependencies error.
I am wondering what's the recommended ways to handle this?
What version of Joi are you using?
On Mon, Jul 3, 2017 at 11:13 AM Spin Wang notifications@github.com wrote:
I have the following code to verify my range object is always valid.
Namely, if the max is available, then min should be smaller than that.const rangeSchema = Joi.object().keys({
min: Joi.number().when('max', {
is: Joi.number(),
then: Joi.number().less(Joi.ref('max')),
}),
max: Joi.number(),
});const rangeObj = {
min: -100,
};const result1 = Joi.validate(rangeObj, rangeSchema);console.log(result1);But I got the following error
ValidationError: child "min" fails because ["min" references "max" which is not a number]
When I add constraints on both max and min
const rangeSchema = Joi.object().keys({
min: Joi.number().when('max', {
is: Joi.number(),
then: Joi.number().less(Joi.ref('max')),
}),
max: Joi.number().when('min', {
is: Joi.number(),
then: Joi.number().greater(Joi.ref('min')),
}),
});const rangeObj = {
min: -100,
};const result1 = Joi.validate(rangeObj, rangeSchema);console.log(result1);I got the following error Error: item added into group max created a
dependencies error.I am wondering what's the recommended ways to handle this?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/hapijs/joi/issues/1236, or mute the thread
https://github.com/notifications/unsubscribe-auth/ALl4O3_PB819JkDCYRGV-4LdGnU97Qllks5sKRMUgaJpZM4OMeQ4
.
Wes, it's 10.6.0
You may want to use min/max instead less/greater. Given your current example an object like: { min: 1, max: 1 } will fail validation as well because less/greater are not less-than-or-equal or greater-than-or-equal.
Now, on to your questions (thanks for providing the test case, it was very helpful).
You're code looks good, that's how I would expect it to look given your description of your desired state. After some sleuthing around different options and configurations I found what particular piece is causing this to fail, so then the next question is why.
If we change your example to not use a Joi schema for referencing max, then the example works such as below:
const rangeSchema = Joi.object().keys({
min: Joi.number().when('max', {
is: 5,
then: Joi.number().less(Joi.ref('max')),
otherwise: Joi.number()
}),
max: Joi.number()
});
So what is the functional difference between placing 5 there and placing Joi.number() there instead. So when you pass a non-Joi reference here it is then converted to a Joi schema using our Cast functionality. Specifically in this example casting it to a Joi.number().valid(5) schema. What this means is that values such as null, '', undefined are not valid, only 5 is valid.
This then brings us to the root of what you have encountered. In your particular case you are stating Joi.number() for the is and that is being used to validate the value of max. So by default schemas have an optional (not required) presence meaning that undefined is a valid value and it satisfies the is portion of the Joi.when() which then causes it to make sure that min is less than max. Which then errors because max does not have a value.
To illustrate this, running a test schema such as below (where we haven't specified the presence requirements of keys) will validate successfully because both min and max are optional:
const rangeSchema = Joi.object().keys({
min: Joi.number(),
max: Joi.number()
});
const rangeObj = {
};
const result1 = Joi.validate({ }, rangeSchema);
console.log(result1);
So the next piece, is how would you fix this issue for yourself?
Given your full example @spinwang just setting the presence modifiers for min and max (like I did below) will address the issue for you:
const rangeSchema = Joi.object().keys({
min: Joi.number().when('max', {
is: Joi.number().required(),
then: Joi.number().less(Joi.ref('max')),
}),
max: Joi.number().when('min', {
is: Joi.number().required(),
then: Joi.number().greater(Joi.ref('min')),
}),
});
const rangeObj = {
min: -100,
};
const result1 = Joi.validate(rangeObj, rangeSchema);
console.log(result1);
This will make sure that the is portion of the Joi.when()s don't successfully validate on missing values.
@Marsup @WesTyler Given all of what I mentioned above, it was a pretty obtuse issue that was run into.
I'm not seeing any clear way right now that we could help others to bubble up this issue of presence other than some additions to the docs about it. Do you guys happen to have any ideas on how we might catch/notify this type of thing.
I knew about this one so this doesn't surprise me anymore. Would it deserve a fix ? Not sure. The behavior is consistent with the rest of joi. Is it unexpected ? Maybe... A documentation improvement seems to be the way to go for me.
I agree with the docs fix.
Maybe a note in the .when() documentation? Not sure what that would look like though.
On Tue, Jul 4, 2017 at 2:02 AM Nicolas Morel notifications@github.com
wrote:
I knew about this one so this doesn't surprise me anymore. Would it
deserve a fix ? Not sure. The behavior is consistent with the rest of joi.
Is it unexpected ? Maybe... A documentation improvement seems to be the way
to go for me.—
You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub
https://github.com/hapijs/joi/issues/1236#issuecomment-312797075, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ALl4O89lSUN55KI_M3a5A0vg8AhrUVfnks5sKeNmgaJpZM4OMeQ4
.
thanks @DavidTPate @Marsup and @WesTyler. Here is a PR to improve the API.md
https://github.com/hapijs/joi/pull/1237
@DavidTPate it seems your snippet is not working https://runkit.com/sht/596d287deb92420012e2deff
Or am I missing something?
Most helpful comment
@DavidTPate it seems your snippet is not working https://runkit.com/sht/596d287deb92420012e2deff
Or am I missing something?