-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Nested whens don't work #632
Comments
I think you're just using this incorrectly. If you look at any.when() you'll notice that for So what your schema is saying is that the setting value when |
Ahh thanks for clarifying the docs. So I tried using var schema = Joi.object().keys({
setting: Joi.alternatives().when('$auth.credentials.cond1', {
is: true,
then: Joi.alternatives().when('$auth.credentials.cond2', {
is: true,
then: Joi.number().integer().invalid(100, 101).required(),
otherwise: Joi.number().interger().required()
}),
otherwise: Joi.forbidden()
})
}); I tried to fix this below, but I get the error var schema = Joi.object().keys({
setting: Joi.when('$auth.credentials.cond1', {
is: true,
then: Joi.when('$auth.credentials.cond2', {
is: true,
then: Joi.number().integer().invalid(100, 101).required(),
otherwise: Joi.number().interger().required()
}),
otherwise: Joi.forbidden()
})
}); Any advice on how I should get the functionallity I want? |
Also, regarding your comment above about var schema = Joi.object().keys({
setting: Joi.number().integer().when('$auth.credentials.cond1', {
is: true,
then: Joi.required(),
otherwise: Joi.forbidden()
})
}); |
There might be a problem in the concat. Using this : var Joi = require('joi');
var schema = Joi.object().keys({
setting: Joi.when('$auth.credentials.cond1', {
is: true,
then: Joi.when('$auth.credentials.cond2', {
is: true,
then: Joi.number().integer().invalid(100, 101).required(),
otherwise: Joi.number().integer().required()
}),
otherwise: Joi.forbidden()
})
});
var input = { setting: <whatever> };
Joi.validate(input, schema, { context: { auth: { credentials: { cond1: true, cond2: true }}}}, function(err, value) {
console.log('err:', err);
console.log('value:', value);
}); with the latest master works. Can you check if it covers your needs ? |
@Marsup you're right - it works in master, but fails with the below error in the v6.2.0 release. Is there any timeline for cutting a new version with the fix?
|
Can be tonight. Does that address your PR too ? |
Awesome! 👏 Thanks for the help. I'm not sure if this fix will provide a way for me to rename keys after validation like my PR does (or do something which is semantically equal). I'll look into that particular case and report back soon. This fix will definitely be fixing a specific case for me though, so thanks. |
Fixed by 7867225. |
Published. I'm still not convinced by the necessity of your PR, so don't expect this one anytime soon. |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
Why doesn't this work in my schema:
When
$auth.credentials.cond1
is true, andsetting
is not provided in the input object, there is no validation error when there should be because it is required.The text was updated successfully, but these errors were encountered: