From 3ac6d4606b3ae1016c6ffdad5f79e2721b0d66a1 Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Fri, 15 Nov 2024 17:56:45 +0100 Subject: [PATCH] add URL validator implementation & tests --- requirements.txt | 1 + tox.ini | 1 + yamale/VERSION | 2 +- yamale/tests/fixtures/urls_bad.yaml | 7 +++++++ yamale/tests/fixtures/urls_good.yaml | 7 +++++++ yamale/tests/fixtures/urls_schema.yaml | 1 + yamale/validators/validators.py | 10 ++++++++++ 7 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 yamale/tests/fixtures/urls_bad.yaml create mode 100644 yamale/tests/fixtures/urls_good.yaml create mode 100644 yamale/tests/fixtures/urls_schema.yaml diff --git a/requirements.txt b/requirements.txt index ed71f87..e3ca9d6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ tox==4.13.0 +validators==0.34.0 diff --git a/tox.ini b/tox.ini index fff02c0..51a7768 100644 --- a/tox.ini +++ b/tox.ini @@ -12,3 +12,4 @@ deps = ruamel.yaml<1 pytest<7 pytest-cov<4 + validators>0.33.0 diff --git a/yamale/VERSION b/yamale/VERSION index 26d99a2..ce7f2b4 100644 --- a/yamale/VERSION +++ b/yamale/VERSION @@ -1 +1 @@ -5.2.1 +5.2.2 diff --git a/yamale/tests/fixtures/urls_bad.yaml b/yamale/tests/fixtures/urls_bad.yaml new file mode 100644 index 0000000..d6d8dfd --- /dev/null +++ b/yamale/tests/fixtures/urls_bad.yaml @@ -0,0 +1,7 @@ +urls: + - "" + - "-11.0.1" + - "http://256.256.256.256" + - "x://example-bad-protocol.com" + - "https://bad-domain" + - "http:///" diff --git a/yamale/tests/fixtures/urls_good.yaml b/yamale/tests/fixtures/urls_good.yaml new file mode 100644 index 0000000..8711a18 --- /dev/null +++ b/yamale/tests/fixtures/urls_good.yaml @@ -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" diff --git a/yamale/tests/fixtures/urls_schema.yaml b/yamale/tests/fixtures/urls_schema.yaml new file mode 100644 index 0000000..3ea5a33 --- /dev/null +++ b/yamale/tests/fixtures/urls_schema.yaml @@ -0,0 +1 @@ +urls: list(url()) \ No newline at end of file diff --git a/yamale/validators/validators.py b/yamale/validators/validators.py index 20fbb7a..c38a3b0 100644 --- a/yamale/validators/validators.py +++ b/yamale/validators/validators.py @@ -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 @@ -260,6 +262,14 @@ def __init__(self, *args, **kwargs): re.compile(r"^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?: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[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 = {}