-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75ff7ea
commit af48f0a
Showing
2 changed files
with
32 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
from pydantic import StringConstraints | ||
from typing import Annotated | ||
|
||
DomainStr = Annotated[str, StringConstraints(strip_whitespace=True, to_lower=True, min_length=1, max_length=253, | ||
pattern=r'^([a-z0-9-]+(\.[a-z0-9-]+)+)$')] | ||
from pydantic import StringConstraints | ||
|
||
DomainStr = Annotated[ | ||
str, | ||
StringConstraints( | ||
strip_whitespace=True, to_lower=True, min_length=1, max_length=253, pattern=r'^([a-z0-9-]+(\.[a-z0-9-]+)+)$' | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,40 @@ | ||
from pydantic_extra_types.domain import DomainStr | ||
from pydantic import BaseModel, ValidationError | ||
import pytest | ||
from pydantic import BaseModel, ValidationError | ||
|
||
from pydantic_extra_types.domain import DomainStr | ||
|
||
|
||
class MyModel(BaseModel): | ||
domain: DomainStr | ||
domain: DomainStr | ||
|
||
|
||
very_long_domains_list = [ | ||
'sub1.sub2.sub3.sub4.sub5.sub6.sub7.sub8.sub9.sub10.sub11.sub12.sub13.sub14.sub15.sub16.sub17.sub18.sub19.sub20.sub21.sub22.sub23.sub24.sub25.sub26.sub27.sub28.sub29.sub30.sub31.sub32.sub33.extremely-long-domain-name-example-to-test-the-253-character-limit.com', | ||
'a-very-very-long-subdomain-name-that-continues-forever-and-ever-testing-the-length-limit-of-domains-to-make-sure-it-reaches-beyond-the-maximum-allowed-for-experimentation-and-testing-purposes.extremely-extended-domain-name-example-for-253-characters-limit.net' | ||
'sub1.sub2.sub3.sub4.sub5.sub6.sub7.sub8.sub9.sub10.sub11.sub12.sub13.sub14.sub15.sub16.sub17.sub18.sub19.sub20.sub21.sub22.sub23.sub24.sub25.sub26.sub27.sub28.sub29.sub30.sub31.sub32.sub33.extremely-long-domain-name-example-to-test-the-253-character-limit.com', | ||
'a-very-very-long-subdomain-name-that-continues-forever-and-ever-testing-the-length-limit-of-domains-to-make-sure-it-reaches-beyond-the-maximum-allowed-for-experimentation-and-testing-purposes.extremely-extended-domain-name-example-for-253-characters-limit.net', | ||
] | ||
|
||
|
||
@pytest.mark.parametrize('domain', ['a.com', 'x.com']) | ||
def test_single_letter_domain(domain: str): | ||
MyModel.model_validate({'domain': domain}) | ||
|
||
|
||
@pytest.mark.parametrize('domain', very_long_domains_list) | ||
def test_domains_over_253_characters(domain: str): | ||
assert len(domain) > 253 | ||
try: | ||
MyModel.model_validate({'domain': domain}) | ||
raise Exception('Domain did not throw an error for having over 253 characters') | ||
except ValidationError: | ||
# An error is expected on this test | ||
pass | ||
assert len(domain) > 253 | ||
try: | ||
MyModel.model_validate({'domain': domain}) | ||
raise Exception('Domain did not throw an error for having over 253 characters') | ||
except ValidationError: | ||
# An error is expected on this test | ||
pass | ||
|
||
|
||
@pytest.mark.parametrize('domain', ['']) | ||
def test_domains_over_253_characters(domain: str): | ||
try: | ||
MyModel.model_validate({'domain': domain}) | ||
raise Exception('Domain did not throw an error for having less than 1 character') | ||
except ValidationError: | ||
# An error is expected on this test | ||
pass | ||
def test_domains_having_less_than_one_character(domain: str): | ||
try: | ||
MyModel.model_validate({'domain': domain}) | ||
raise Exception('Domain did not throw an error for having less than 1 character') | ||
except ValidationError: | ||
# An error is expected on this test | ||
pass |