MCPcopy
hub / github.com/pydantic/pydantic / UrlConstraints

Class UrlConstraints

pydantic/networks.py:71–123  ·  view source on GitHub ↗

Url constraints. Attributes: max_length: The maximum length of the url. Defaults to `None`. allowed_schemes: The allowed schemes. Defaults to `None`. host_required: Whether the host is required. Defaults to `None`. default_host: The default host. Defaults to `Non

Source from the content-addressed store, hash-verified

69
70@_dataclasses.dataclass
71class UrlConstraints:
72 """Url constraints.
73
74 Attributes:
75 max_length: The maximum length of the url. Defaults to `None`.
76 allowed_schemes: The allowed schemes. Defaults to `None`.
77 host_required: Whether the host is required. Defaults to `None`.
78 default_host: The default host. Defaults to `None`.
79 default_port: The default port. Defaults to `None`.
80 default_path: The default path. Defaults to `None`.
81 preserve_empty_path: Whether to preserve empty URL paths. Defaults to `None`.
82 """
83
84 max_length: int | None = None
85 allowed_schemes: list[str] | None = None
86 host_required: bool | None = None
87 default_host: str | None = None
88 default_port: int | None = None
89 default_path: str | None = None
90 preserve_empty_path: bool | None = None
91
92 def __hash__(self) -> int:
93 return hash(
94 (
95 self.max_length,
96 tuple(self.allowed_schemes) if self.allowed_schemes is not None else None,
97 self.host_required,
98 self.default_host,
99 self.default_port,
100 self.default_path,
101 self.preserve_empty_path,
102 )
103 )
104
105 @property
106 def defined_constraints(self) -> dict[str, Any]:
107 """Fetch a key / value mapping of constraints to values that are not None. Used for core schema updates."""
108 return {field.name: value for field in fields(self) if (value := getattr(self, field.name)) is not None}
109
110 def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
111 schema = handler(source)
112
113 # for function-wrap schemas, url constraints is applied to the inner schema
114 # because when we generate schemas for urls, we wrap a core_schema.url_schema() with a function-wrap schema
115 # that helps with validation on initialization, see _BaseUrl and _BaseMultiHostUrl below.
116 schema_to_mutate = schema['schema'] if schema['type'] == 'function-wrap' else schema
117 if (annotated_type := schema_to_mutate['type']) not in ('url', 'multi-host-url'):
118 raise PydanticUserError(
119 f"'UrlConstraints' cannot annotate '{annotated_type}'.", code='invalid-annotated-type'
120 )
121 for constraint_key, constraint_value in self.defined_constraints.items():
122 schema_to_mutate[constraint_key] = constraint_value
123 return schema
124
125
126class _BaseUrl:

Callers 15

ModelClass · 0.90
Model2Class · 0.90
test_custom_constraintsFunction · 0.90
test_after_validatorFunction · 0.90
_BaseUrlClass · 0.85
_BaseMultiHostUrlClass · 0.85
AnyHttpUrlClass · 0.85
HttpUrlClass · 0.85

Calls

no outgoing calls