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

Conditional rule based on value of another key #194

Closed
hueniverse opened this issue Mar 9, 2014 · 9 comments
Closed

Conditional rule based on value of another key #194

hueniverse opened this issue Mar 9, 2014 · 9 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@hueniverse
Copy link
Contributor

No description provided.

@hueniverse hueniverse added this to the 2.8.0 milestone Mar 9, 2014
@hueniverse hueniverse self-assigned this Mar 9, 2014
@hueniverse hueniverse removed this from the 2.8.0 milestone Mar 12, 2014
@danielb2
Copy link
Contributor

I propose the following syntax:

Example
Requires the presence of another key whenever the value of a key matches the given value.

var schema = {
    a: Joi.string(),
    b: Joi.any().when('a', Joi.string().regex(/^value$/), Joi.required())
};

If the value of a is "value" then b is required. When can take an array of triplets.

@phated
Copy link
Contributor

phated commented Mar 19, 2014

👍 I really would love this for schema "tables" /cc @nvcexploder

phated added a commit to iceddev/joi that referenced this issue Mar 20, 2014
@hueniverse
Copy link
Contributor Author

When do you evaluate 'a'? Before or after 'a' itself is evaluated? What if 'a' is '5' and you want it to be a number? should conversion rules apply? This is hard.

@danielb2
Copy link
Contributor

danielb2 commented Apr 2, 2014

My thought was to evaluate 'a' twice. In the schema itself a: Joi.string() and also in the when case. In the case of when you'd create another schema, i.e.

var schema = {
    a: Joi.string().regex(/^value$/)
};

If that does not return null, then add Joi.required() to b.

@hueniverse
Copy link
Contributor Author

Add it to b means cloning the entire object at validation time. Very expensive.

@hueniverse
Copy link
Contributor Author

Partially covered by #260

@hueniverse hueniverse reopened this Apr 22, 2014
@hueniverse hueniverse added this to the 4.0.0 milestone Apr 22, 2014
@phated
Copy link
Contributor

phated commented Apr 22, 2014

Woot! Thanks!

@danielb2
Copy link
Contributor

Reopening this since the fix didn't address neither #166 nor #188

var Joi = require('joi');

var schema = {
    a: Joi.any().when('b', { is: 5, then: Joi.required(), otherwise: Joi.optional() }),
    b: Joi.any()
};

var thing = {
    b: 5
};
var validate = Joi.validate(thing, schema);

// returns
{
    error: null,
    value: {
        b: 5
    }
}

I expected an error saying that a is required because b is 5

@danielb2 danielb2 reopened this May 20, 2014
@hueniverse
Copy link
Contributor Author

@danielb2 Don't do this. Open a new issue next time. In this case, already reported as #334.

@hapijs hapijs locked as resolved and limited conversation to collaborators Oct 24, 2019
@hapijs hapijs deleted a comment from nirajbisht Oct 24, 2019
@hapijs hapijs deleted a comment from nirajbisht Oct 24, 2019
@hapijs hapijs deleted a comment from Marsup Oct 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

3 participants