You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an interesting little library and very useful (Thanks!). However, the validators.utils.validator decorator ends up making a weird little API since it returns the Exception instead of raising it which is further complicated by the fact that on success True is returned. Thus the code reads as:
I'm fine with having a configuration option for that. The reason for having an API like it currently has is that many times I found it cumbersome of catching exceptions. Let's say I only want to check that given value is a valid email, otherwise I'll ignore it. I can just write:
I was thinking more about this, and I think you can do it easily as a parameter:
try:
validators.email(value, raise_on_error=True)
exceptvalidators.ValidationError:
log.exception('error in validation')
# do something about itelse:
# continue onpass
From your example, I'm thinking the above might be your only good solution to maintaining backwards compatibility which will be important; however, do realize that your logic may fail unless you return a False-y value in some form (None, False, 0, etc). At it is you're always returning the equivalent of True even when validation fails.
That is:
x = validators.email(...)
if x:
print('Validation Succeeded!')
the above will always print out "Validation Succeeded!" whether ValidationError is returned or True is returned as they're both equivalent to True. If you returned None instead of True, then you could do:
x = validators.email(...)
if not x:
print("Validation Succeeded!")
this would also remove the need to definitively tests using isinstance() in your unit tests.
Activity
kvesteri commentedon Feb 14, 2020
I'm fine with having a configuration option for that. The reason for having an API like it currently has is that many times I found it cumbersome of catching exceptions. Let's say I only want to check that given value is a valid email, otherwise I'll ignore it. I can just write:
instead of
BenjamenMeyer commentedon Feb 18, 2020
I was thinking more about this, and I think you can do it easily as a parameter:
This would just require the https://github.com/kvesteri/validators/blob/master/validators/utils.py#L82 to check for the kwarg and remove it from the dict passed onto the wrapped function; something like:
From your example, I'm thinking the above might be your only good solution to maintaining backwards compatibility which will be important; however, do realize that your logic may fail unless you return a False-y value in some form (None, False, 0, etc). At it is you're always returning the equivalent of
Trueeven when validation fails.That is:
the above will always print out "Validation Succeeded!" whether ValidationError is returned or
Trueis returned as they're both equivalent to True. If you returned None instead of True, then you could do:this would also remove the need to definitively tests using
isinstance()in your unit tests.InnovativeInventor commentedon Feb 19, 2020
As someone who loves to use this library, I like the
raise_on_errorflag idea!ogencoglu commentedon Sep 4, 2023
A configuration would be great. Most times, people need True or False.