feat: add HttpUrlStr shared URL validator

This commit is contained in:
CREDO23 2026-07-23 18:52:32 +02:00
parent ca4f231577
commit 797dd2df47
3 changed files with 56 additions and 0 deletions

View file

@ -0,0 +1,24 @@
"""Shared Pydantic field types for capability I/O schemas."""
from __future__ import annotations
from typing import Annotated
from urllib.parse import urlsplit
import validators
from pydantic import AfterValidator
from pydantic_core import PydanticCustomError
_HTTP_SCHEMES = frozenset({"http", "https"})
def _validate_http_url(value: str) -> str:
"""Accept only well-formed http(s) URLs, returned trimmed and unchanged."""
url = value.strip()
if not validators.url(url) or urlsplit(url).scheme.lower() not in _HTTP_SCHEMES:
raise PydanticCustomError("http_url", "must be a valid http(s) URL")
return url
HttpUrlStr = Annotated[str, AfterValidator(_validate_http_url)]
"""A request URL validated as http(s) and kept as ``str`` (no normalization)."""