-
Notifications
You must be signed in to change notification settings - Fork 163
Closed
Labels
maintenancePR: Alters existing source codePR: Alters existing source code
Description
A few thoughts that I think would help improve flexibility of your code:
For the UUID validator
Instead of using a pattern matcher which doesn’t accept valid strings without hyphens (e.g. "1219ffcf45c78964b04a3290cf84183a"
), why not just use Python’s own uuid
module?
@validator
def uuid(value):
try:
return uuid.UUID(value) is not None
except ValueError:
return False
For the URL validator
Again an impressive funk of patterns 😉Why not use the rfc3987 module like so:
@validator
def url(value, public=False):
try:
result = rfc3987.parse(value)
# Do `public` stuff here.
except ValueError:
return False
Note that this module also gives you a whole heap of regexes which you could use here instead of duplicating code.
And…
To simplify the above code, you’d also move the exception handling into the decorator:
def validator(func, *args, **kwargs):
def wrapper(func, *args, **kwargs):
try:
value = func(*args, **kwargs)
if not value:
return ValidationFailure(
func, func_args_as_dict(func, args, kwargs)
)
return True
except Exception: # In the above two cases, ValueError.
return ValidationFailure(func, func_args_as_dict(func, args, kwargs))
return decorator(wrapper, func)
Metadata
Metadata
Assignees
Labels
maintenancePR: Alters existing source codePR: Alters existing source code