-
Notifications
You must be signed in to change notification settings - Fork 218
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
Allow a discrminant field in validators #368
Conversation
@alecthomas did you want to comment on this? |
It's hard to tell as the doctest doesn't actually test it? |
voluptuous/validators.py
Outdated
"""Use the first validated value among those selected by discrminant. | ||
|
||
:param msg: Message to deliver to user if validation fails. | ||
:param discriminant: Function to filter the values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move the signature description into this param comment please.
Thanks! |
HI @alecthomas ! I saw that this repo is contributions only, but will you still make release from master from time to time ? (I'm interested about this feature and was wondering if it has a chance to be released or not) Thanks for the work on this library 👍 |
Yep, definitely. |
Attempts to fix #360
The accepted solution was to add a
Union
type which requires a discriminant. What I have done is check for adiscrminant
argument, and run thevalue
and alternatives through the discriminant function to obtain a filtered list of alternatives. This allows a discriminant field to be included in the existing typesSince the validators are compiled before execution, when using the
_WithSubValidators
class, and thevalue
is available during execution I cannot check if it is accepted by the discriminant or not any earlier than execution. In such a case I'd have to recompile the validators obtained from the discriminant.I have a bunch of
print
statements committed which I will remove once this is approved.