Skip to content

Commit

Permalink
Accept empty string on construction
Browse files Browse the repository at this point in the history
The empty string is already treated as invalid by the supplied
validators. It was incorrect to raise ValueError on construction since
this requires users to pre-validate strings before passing to a
validation library.

Fixes ypcrts#45
  • Loading branch information
Shane Kearns committed Mar 4, 2024
1 parent 8429006 commit 44bc6d1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fqdn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, fqdn, *nothing, **kwargs):
if unknown_kwargs:
raise ValueError("got extra kwargs: {}".format(unknown_kwargs))

if not (fqdn and isinstance(fqdn, str)):
if not isinstance(fqdn, str):
raise ValueError("fqdn must be str")
self._fqdn = fqdn.lower()
self._allow_underscores = kwargs.get("allow_underscores", False)
Expand Down
8 changes: 6 additions & 2 deletions tests/test_fqdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def test_str(self, a_u):
f = FQDN(d, allow_underscores=a_u)
assert f.absolute == str(f)

def test_empty(self, a_u):
d = ""
f = FQDN(d, allow_underscores=a_u)
assert not f.is_valid

def test_rfc_1035_s_2_3_4__label_max_length(self, a_u):
assert FQDN(
"www.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk.com",
Expand Down Expand Up @@ -159,8 +164,7 @@ def test_min_labels_defaults_to_require_2(self):
assert not dn.is_valid

def test_min_labels_valid_set_to_1(self):
with pytest.raises(ValueError):
FQDN("", min_labels=1).is_valid
assert not FQDN("", min_labels=1).is_valid
assert FQDN("label", min_labels=1).is_valid
assert not FQDN(".label", min_labels=1).is_valid
assert FQDN("label.babel", min_labels=1).is_valid
Expand Down

0 comments on commit 44bc6d1

Please sign in to comment.