-
Notifications
You must be signed in to change notification settings - Fork 217
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
Adding SomeOf validator #314
Changes from 1 commit
267f197
9a33635
ef97726
5e13321
63eba67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,8 @@ | |
from voluptuous.error import (MultipleInvalid, CoerceInvalid, TrueInvalid, FalseInvalid, BooleanInvalid, Invalid, | ||
AnyInvalid, AllInvalid, MatchInvalid, UrlInvalid, EmailInvalid, FileInvalid, DirInvalid, | ||
RangeInvalid, PathInvalid, ExactSequenceInvalid, LengthInvalid, DatetimeInvalid, | ||
DateInvalid, InInvalid, TypeInvalid, NotInInvalid, ContainsInvalid) | ||
DateInvalid, InInvalid, TypeInvalid, NotInInvalid, ContainsInvalid, NotEnoughValid, | ||
TooManyValid) | ||
|
||
if sys.version_info >= (3,): | ||
import urllib.parse as urlparse | ||
|
@@ -933,3 +934,49 @@ def _get_precision_scale(self, number): | |
raise Invalid(self.msg or 'Value must be a number enclosed with string') | ||
|
||
return (len(decimal_num.as_tuple().digits), -(decimal_num.as_tuple().exponent), decimal_num) | ||
|
||
|
||
class SomeOf(object): | ||
"""Value must pass at least some validations, determined by the given parameter | ||
|
||
The output of each validator is passed as input to the next. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. description will be improved in the next update of this PR (I'm sure there will be so I'm waiting with pushing it 😄 ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha.. Let's not rely on it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will push it asap |
||
|
||
>>> validate = Schema(SomeOf(min_valid=2, validators=[Range(1, 5), Any(float, int), 6.6])) | ||
>>> validate(6.6) | ||
6.6 | ||
>>> validate(3) | ||
3 | ||
>>> with raises(MultipleInvalid, 'value must be at most 5, not a valid value'): | ||
... validate(6.2) | ||
""" | ||
|
||
def __init__(self, min_valid, validators, max_valid=None, **kwargs): | ||
self.min_valid = min_valid or 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Originally I only needed "minimum valid schemas" and just added "max valid" for completeness. So for me, min_valid was always needed. We can put them both as optional, and assert that either of them has a value (otherwise, what's the point of this validator). Sounds good? |
||
self.max_valid = max_valid or len(validators) | ||
self.validators = validators | ||
self.msg = kwargs.pop('msg', None) | ||
self._schemas = [Schema(val, **kwargs) for val in validators] | ||
|
||
def __call__(self, v): | ||
errors = [] | ||
for schema in self._schemas: | ||
try: | ||
v = schema(v) | ||
except Invalid as e: | ||
errors.append(e) | ||
|
||
passed_count = len(self._schemas) - len(errors) | ||
if self.min_valid <= passed_count <= self.max_valid: | ||
return v | ||
|
||
msg = self.msg | ||
if not msg: | ||
msg = ', '.join(map(str, errors)) | ||
|
||
if passed_count > self.max_valid: | ||
raise TooManyValid(msg) | ||
raise NotEnoughValid(msg) | ||
|
||
def __repr__(self): | ||
return 'SomeOf(min_valid=%s, validators=[%s], max_valid=%s, msg=%r)' % ( | ||
self.min_valid, ", ".join(repr(v) for v in self.validators), self.max_valid, self.msg) |
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.
Let's add a test for checking the assert statement too using
AssertionError
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.
Done