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

Domain name string type #212

Merged
merged 9 commits into from
Sep 6, 2024
15 changes: 15 additions & 0 deletions pydantic_extra_types/domain.py
yezz123 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys

if sys.version_info < (3, 9): # pragma: no cover
from typing_extensions import Annotated # pragma: no cover
else:
from typing import Annotated # pragma: no cover

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-]+)+)$'
),
]
40 changes: 40 additions & 0 deletions tests/test_domain.py
yezz123 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
from pydantic import BaseModel, ValidationError

from pydantic_extra_types.domain import DomainStr


class MyModel(BaseModel):
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',
]


@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


@pytest.mark.parametrize('domain', [''])
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
Loading