Skip to content

Problem with multiple when and conditional forbidden field #822

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

Closed
bitcloud opened this issue Feb 11, 2016 · 4 comments
Closed

Problem with multiple when and conditional forbidden field #822

bitcloud opened this issue Feb 11, 2016 · 4 comments
Assignees
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Milestone

Comments

@bitcloud
Copy link

I currently try to implement a validation where the existence or validation of an attribute in an object is dependent on another attribute.

I tried with a chain of any.when():

const Joi = require('joi');
const schemaWhen = Joi.object().keys({
    a: Joi.string().valid('1','2').required(),
    b: Joi
        .when('a', {is: '1', then: Joi.forbidden() })
        .when('a', {is: '2', then: Joi.number().integer().min(1).max(5).required() })
});

Joi.assert({a: '1'}, schemaWhen); // Pass
Joi.assert({a: '1', b: 50}, schemaWhen); // Fail
Joi.assert({a: '2', b: 5}, schemaWhen); // Pass
Joi.assert({a: '2', b: 50}, schemaWhen); // Pass. But should Fail?

... and with alternative:

const Joi = require('joi');
const schemaAlternative = {
    a: Joi.string().valid('aa','ab', 'ac').required(),
    b: Joi.alternatives()
        .when('a', {is: 'aa', then: Joi.string().valid('ba', 'bb').required()})
        .when('a', {is: 'ab', then: Joi.string().valid('bc').required()})
        .when('a', {is: 'ac', then: Joi.forbidden()})
};


Joi.assert({ a: 'aa', b: 'ba' }, schemaAlternative); // Pass
Joi.assert({ a: 'aa', b: 'bb' }, schemaAlternative); // Pass
Joi.assert({ a: 'ab', b: 'bc' }, schemaAlternative); // Pass
Joi.assert({ a: 'ac' }, schemaAlternative); // Pass

Joi.assert({ a: 'aa', b: 'bc' }, schemaAlternative); // Fail
Joi.assert({ a: 'ab', b: 'ba' }, schemaAlternative); // Fail
Joi.assert({ a: 'ac', b: 'ba' }, schemaAlternative); // Fail

Joi.assert({ a: 'aa' }, schemaAlternative); // Pass. But should Fail?
Joi.assert({ a: 'ab' }, schemaAlternative); // Pass. But should Fail?

..., but had no luck so far.

I even think the result should be considered a bug, or am I missing something?

@Marsup Marsup added bug Bug or defect breaking changes Change that can breaking existing code labels Feb 11, 2016
@Marsup Marsup self-assigned this Feb 11, 2016
@Marsup Marsup added this to the 8.0.0 milestone Feb 11, 2016
@Marsup
Copy link
Collaborator

Marsup commented Feb 11, 2016

You stumbled upon a bug that is going to be fixed in the next major but that's a minor breaking change in the behavior of alternatives. FWIW the 1st version is correct. No ETA on the release but it should be soon-ish.

@Marsup Marsup closed this as completed Feb 12, 2016
@bitcloud
Copy link
Author

so this is already fixed in the v8 branch? Then I will just move over to that one and start playing with it.

@Marsup
Copy link
Collaborator

Marsup commented Feb 12, 2016

This has been for quite a while yes.

@bitcloud
Copy link
Author

Great, thanks!

@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants