Skip to content

Question: Why not raise ValidationFailure? #139

Description

@BenjamenMeyer

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:

x = validators.domain('/howdy$com')
if x is not True:
   raise MyError("Invalid Domain") from x

instead of the more normal:

try:
    validators.domain('/howdy$com')
except validators.ValidationError as ex:
    raise MyError("InvalidDomain") from ex

I'm primarily curious as to the reasoning behind this as it's simply not expected.

A few suggestions:

  • Instead of True return None so that at the very least the logic above can be simpler:
x = validators.domain('/howdy$com')
if x:
   raise X
  • Raise the exception instead of returning it, or provide a method to configure the library to do so.
>>> validators.raise_exceptions = True
>>> validators.domain('/howdy$com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
validators.utils.ValidationFailure: ValidationFailure(func=domain, args={'value': '/howdy$com'})

or

>>> validators.domain('/howdy$com', raise_exceptions=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
validators.utils.ValidationFailure: ValidationFailure(func=domain, args={'value': '/howdy$com'})

NOTE: I'm suggesting a method of config to maintain backwards compatibility with the uses already out there.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementIssue/PR: A new featureoutdatedIssue/PR: Open for more than 3 monthsquestionIssue: A question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions