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

add URL validator implementation & tests #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
tox==4.13.0
validators==0.34.0
Copy link

@mildebrandt mildebrandt Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I have a few concerns about using the validators package:

  1. It's still pre 1.0, so things can break between minor versions. Why isn't http://localhost:PORT a valid URL? python-validators/validators#285 (comment)
  2. It doesn't support http://localhost by default. url validator rejects some local URLs as invalid even though they should be valid python-validators/validators#392
  3. It doesn't support query strings without values. [Question]: Are URL query strings containing keys without values invalid on purpose? python-validators/validators#363
  4. The supported schemes are hard coded. https://github.com/python-validators/validators/blob/c9585e91f8b409029b059df148da4dc52bd951e3/src/validators/url.py#L42

And that's what I found in just 10 minutes. I'm sure this could work for a subset of users....but it's not a generic solution for all valid URLs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Thanks for this feedback. I'll find something more universal.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. FYI, currently Yamale has only one dependency....pyyaml. Anything you add will double the number of dependencies. I don't know how the current maintainers feel about that, but just something to keep in mind.

A couple other things:

  1. Don't update the version of Yamale in your PR. That's done during the release process.
  2. Add any new dependencies in the setup.py file.

Good luck!

1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ deps =
ruamel.yaml<1
pytest<7
pytest-cov<4
validators>0.33.0
2 changes: 1 addition & 1 deletion yamale/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.2.1
5.2.2
7 changes: 7 additions & 0 deletions yamale/tests/fixtures/urls_bad.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
urls:
- ""
- "-11.0.1"
- "http://256.256.256.256"
- "x://example-bad-protocol.com"
- "https://bad-domain"
- "http:///"
7 changes: 7 additions & 0 deletions yamale/tests/fixtures/urls_good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
urls:
- "http://123.123.123.123"
- "https://123.123.123.123/"
- "http://example.com"
- "https://example.com/"
- "https://example.com/path1/subpath1"
- "https://example.com/path2/subpath2?arg2=2#section"
1 change: 1 addition & 0 deletions yamale/tests/fixtures/urls_schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
urls: list(url())
10 changes: 10 additions & 0 deletions yamale/validators/validators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
from datetime import date, datetime
import ipaddress
import validators

from .base import Validator
from . import constraints as con
from .. import util
Expand Down Expand Up @@ -260,6 +262,14 @@ def __init__(self, *args, **kwargs):
re.compile(r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"),
]

class Url(Validator):
"""URL validator"""

tag = "url"

def _is_valid(self, value):
return validators.url(value)


DefaultValidators = {}

Expand Down