Skip to content
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

Simplify implementations of UUID and URL validators. #101

Closed
jenstroeger opened this issue Dec 3, 2018 · 1 comment · Fixed by #236
Closed

Simplify implementations of UUID and URL validators. #101

jenstroeger opened this issue Dec 3, 2018 · 1 comment · Fixed by #236
Labels
maintenance PR: Alters existing source code

Comments

@jenstroeger
Copy link

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)
@kvesteri
Copy link
Collaborator

kvesteri commented Dec 4, 2018

Thanks for suggestions!

  1. PRs welcome for the UUID
  2. rfc3987 package is licensed under GPLv3. Hence according to almight google I can't use that package here:
    "The MIT license is also compatible with many copyleft licenses, such as the GNU General Public License (GPL); MIT licensed software can be integrated into GPL software, but not the other way around."
  3. I wouldn't use decorator like that. Catching generic Exception like that leads to all kinds of weird situations and might catch exceptions that the user wouldn't want to.

@yozachar yozachar added the maintenance PR: Alters existing source code label Mar 7, 2023
yozachar added a commit to yozachar/pyvalidators that referenced this issue Mar 7, 2023
- reformats `mac_address`, `slug` and `uuid` modules
- removes less relevant `truthy` module
- uses type hints, adds doc refs
- updates related tests

**Related items**

*Issues*

- Closes python-validators#101
- Closes python-validators#109
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance PR: Alters existing source code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants