You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Currently, Pydantic's UrlConstraints allows specifying allowed_schemes via the allowed_schemes parameter. However, there is no built-in mechanism to restrict Url validation to specific hosts.
I propose enhancing the UrlConstraints class in Pydantic by adding an allowed_hosts attribute. This attribute would allow users to specify a list of valid hosts for Url validation. The addition of the allowed_hosts attribute would provide users with more granular control over Url validation.
A demo of how code might look when setting the allowed_hosts parameter in UrlConstraints 👇
from typing import Annotated
from pydantic import BaseModel, HttpUrl, UrlConstraints
class Model(BaseModel):
url: Annotated[HttpUrl, UrlConstraints(allowed_hosts=["www.github.com", "google.com"])]
Seems like a reasonable request. I believe we might be moving all of the annotated url types to concrete subclasses, so maybe we could add more support when we make that change, see #7353.
Initial Checks
Description
Currently, Pydantic's UrlConstraints allows specifying allowed_schemes via the allowed_schemes parameter. However, there is no built-in mechanism to restrict Url validation to specific hosts.
I propose enhancing the UrlConstraints class in Pydantic by adding an allowed_hosts attribute. This attribute would allow users to specify a list of valid hosts for Url validation. The addition of the allowed_hosts attribute would provide users with more granular control over Url validation.
A demo of how code might look when setting the allowed_hosts parameter in UrlConstraints 👇
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: