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 a DomainStr type to validate domains #211

Closed
2 tasks done
matter1-git opened this issue Sep 5, 2024 · 4 comments
Closed
2 tasks done

Add a DomainStr type to validate domains #211

matter1-git opened this issue Sep 5, 2024 · 4 comments

Comments

@matter1-git
Copy link
Contributor

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

from pydantic import BaseModel, StringConstraints, ValidationError
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-]+)+)$")]


class MyModel(BaseModel):
  url: DomainStr


MyModel(url="google.com")  # Passes
MyModel(url="x.com")  # Passes
MyModel(url="www.a.co")  # Passes
try:
  MyModel(url="https://google.com")  # Raises ValidationError
except ValidationError:
  print("Raised validation error for http/s scheme")
  pass
try:
  MyModel(url="file:///etc/passwd")  # Raises ValidationError
except ValidationError:
  print("Raised validation error for file scheme")
  pass```

### Affected Components

- [ ] [Compatibility between releases](https://docs.pydantic.dev/changelog/)
- [ ] [Data validation/parsing](https://docs.pydantic.dev/concepts/models/#basic-model-usage)
- [ ] [Data serialization](https://docs.pydantic.dev/concepts/serialization/) - `.model_dump()` and `.model_dump_json()`
- [ ] [JSON Schema](https://docs.pydantic.dev/concepts/json_schema/)
- [ ] [Dataclasses](https://docs.pydantic.dev/concepts/dataclasses/)
- [ ] [Model Config](https://docs.pydantic.dev/concepts/config/)
- [X] [Field Types](https://docs.pydantic.dev/api/types/) - adding or changing a particular data type
- [ ] [Function validation decorator](https://docs.pydantic.dev/concepts/validation_decorator/)
- [ ] [Generic Models](https://docs.pydantic.dev/concepts/models/#generic-models)
- [ ] [Other Model behaviour](https://docs.pydantic.dev/concepts/models/) - `model_construct()`, pickling, private attributes, ORM mode
- [ ] [Plugins](https://docs.pydantic.dev/) and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.
@matter1-git
Copy link
Contributor Author

I can open a PR for the DomainStr if everything is okay 😁

@sydney-runkle
Copy link
Contributor

Feel free to open against pydantic-extra-types!

@sydney-runkle sydney-runkle transferred this issue from pydantic/pydantic Sep 5, 2024
@matter1-git
Copy link
Contributor Author

Added PR #212

@yezz123
Copy link
Collaborator

yezz123 commented Sep 6, 2024

Fixed here #212 Thank you @matter1-git

@yezz123 yezz123 closed this as completed Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants