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

Enhance UrlConstraints with allowed_hosts attribute. #9440

Open
4 of 13 tasks
nimaxin opened this issue May 15, 2024 · 1 comment
Open
4 of 13 tasks

Enhance UrlConstraints with allowed_hosts attribute. #9440

nimaxin opened this issue May 15, 2024 · 1 comment

Comments

@nimaxin
Copy link

nimaxin commented May 15, 2024

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

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"])]

Affected Components

@sydney-runkle
Copy link
Member

@nimaxin,

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants