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

Validation with multiple fields #281

Open
ramalhoruben opened this issue Apr 18, 2017 · 5 comments
Open

Validation with multiple fields #281

ramalhoruben opened this issue Apr 18, 2017 · 5 comments

Comments

@ramalhoruben
Copy link

Is it possible to validate a field using information from a second field? For example to check if the object doesn't violate a compound key constraint? Here's an example of what I pretend to do:

def ClientExists(v):

    if client_exists(v):
        return v
    else:
        raise Invalid('Client does not exist')

def CodeIsValid(v):
    # Check if (client_id, fraction_code) is unique

sch_addfraction = Schema({
    Required('client_id') : All(Coerce(int), Range(min=1, max=10000), ClientExists),
    Required('fraction_code') : All(str, Length(min=1, max=4), CodeIsValid),
})
@tusharmakkar08
Copy link
Collaborator

Hey @ramalhoruben

I think it's not currently possible.

@alecthomas: What are your views on supporting these kind of constraints?

Thanks.

@alecthomas
Copy link
Owner

I think we should support a way of validating the parent object.

@nbob
Copy link

nbob commented Oct 28, 2017

@alecthomas is it possible to pass to validator function entire data object as a second argument?

@erik-stephens
Copy link

Will partial function application work?

from functools import partial

def CodeIsValid(client_id, v):
    # Check if (client_id, fraction_code) is unique

sch_addfraction = Schema({
    Required('client_id') : All(Coerce(int), Range(min=1, max=10000), ClientExists),
    Required('fraction_code') : All(str, Length(min=1, max=4), partial(CodeIsValid, getClientId())),
})

Might get a bit cumbersome/verbose for more complicated use cases but it helps keep voluptuous simple.

@bruce-szalwinski
Copy link

One solution is given in #124.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants