networks.py 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. """The networks module contains types for common network-related fields."""
  2. from __future__ import annotations as _annotations
  3. import dataclasses as _dataclasses
  4. import re
  5. from dataclasses import fields
  6. from functools import lru_cache
  7. from importlib.metadata import version
  8. from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
  9. from typing import TYPE_CHECKING, Any, ClassVar
  10. from pydantic_core import MultiHostHost, PydanticCustomError, SchemaSerializer, core_schema
  11. from pydantic_core import MultiHostUrl as _CoreMultiHostUrl
  12. from pydantic_core import Url as _CoreUrl
  13. from typing_extensions import Annotated, Self, TypeAlias
  14. from pydantic.errors import PydanticUserError
  15. from ._internal import _repr, _schema_generation_shared
  16. from ._migration import getattr_migration
  17. from .annotated_handlers import GetCoreSchemaHandler
  18. from .json_schema import JsonSchemaValue
  19. from .type_adapter import TypeAdapter
  20. if TYPE_CHECKING:
  21. import email_validator
  22. NetworkType: TypeAlias = 'str | bytes | int | tuple[str | bytes | int, str | int]'
  23. else:
  24. email_validator = None
  25. __all__ = [
  26. 'AnyUrl',
  27. 'AnyHttpUrl',
  28. 'FileUrl',
  29. 'FtpUrl',
  30. 'HttpUrl',
  31. 'WebsocketUrl',
  32. 'AnyWebsocketUrl',
  33. 'UrlConstraints',
  34. 'EmailStr',
  35. 'NameEmail',
  36. 'IPvAnyAddress',
  37. 'IPvAnyInterface',
  38. 'IPvAnyNetwork',
  39. 'PostgresDsn',
  40. 'CockroachDsn',
  41. 'AmqpDsn',
  42. 'RedisDsn',
  43. 'MongoDsn',
  44. 'KafkaDsn',
  45. 'NatsDsn',
  46. 'validate_email',
  47. 'MySQLDsn',
  48. 'MariaDBDsn',
  49. 'ClickHouseDsn',
  50. 'SnowflakeDsn',
  51. ]
  52. @_dataclasses.dataclass
  53. class UrlConstraints:
  54. """Url constraints.
  55. Attributes:
  56. max_length: The maximum length of the url. Defaults to `None`.
  57. allowed_schemes: The allowed schemes. Defaults to `None`.
  58. host_required: Whether the host is required. Defaults to `None`.
  59. default_host: The default host. Defaults to `None`.
  60. default_port: The default port. Defaults to `None`.
  61. default_path: The default path. Defaults to `None`.
  62. """
  63. max_length: int | None = None
  64. allowed_schemes: list[str] | None = None
  65. host_required: bool | None = None
  66. default_host: str | None = None
  67. default_port: int | None = None
  68. default_path: str | None = None
  69. def __hash__(self) -> int:
  70. return hash(
  71. (
  72. self.max_length,
  73. tuple(self.allowed_schemes) if self.allowed_schemes is not None else None,
  74. self.host_required,
  75. self.default_host,
  76. self.default_port,
  77. self.default_path,
  78. )
  79. )
  80. @property
  81. def defined_constraints(self) -> dict[str, Any]:
  82. """Fetch a key / value mapping of constraints to values that are not None. Used for core schema updates."""
  83. return {field.name: value for field in fields(self) if (value := getattr(self, field.name)) is not None}
  84. def __get_pydantic_core_schema__(self, source: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  85. schema = handler(source)
  86. # for function-wrap schemas, url constraints is applied to the inner schema
  87. # because when we generate schemas for urls, we wrap a core_schema.url_schema() with a function-wrap schema
  88. # that helps with validation on initialization, see _BaseUrl and _BaseMultiHostUrl below.
  89. schema_to_mutate = schema['schema'] if schema['type'] == 'function-wrap' else schema
  90. if annotated_type := schema_to_mutate['type'] not in ('url', 'multi-host-url'):
  91. raise PydanticUserError(
  92. f"'UrlConstraints' cannot annotate '{annotated_type}'.", code='invalid-annotated-type'
  93. )
  94. for constraint_key, constraint_value in self.defined_constraints.items():
  95. schema_to_mutate[constraint_key] = constraint_value
  96. return schema
  97. class _BaseUrl:
  98. _constraints: ClassVar[UrlConstraints] = UrlConstraints()
  99. _url: _CoreUrl
  100. def __init__(self, url: str | _CoreUrl | _BaseUrl) -> None:
  101. self._url = _build_type_adapter(self.__class__).validate_python(url)._url
  102. @property
  103. def scheme(self) -> str:
  104. """The scheme part of the URL.
  105. e.g. `https` in `https://user:pass@host:port/path?query#fragment`
  106. """
  107. return self._url.scheme
  108. @property
  109. def username(self) -> str | None:
  110. """The username part of the URL, or `None`.
  111. e.g. `user` in `https://user:pass@host:port/path?query#fragment`
  112. """
  113. return self._url.username
  114. @property
  115. def password(self) -> str | None:
  116. """The password part of the URL, or `None`.
  117. e.g. `pass` in `https://user:pass@host:port/path?query#fragment`
  118. """
  119. return self._url.password
  120. @property
  121. def host(self) -> str | None:
  122. """The host part of the URL, or `None`.
  123. If the URL must be punycode encoded, this is the encoded host, e.g if the input URL is `https://£££.com`,
  124. `host` will be `xn--9aaa.com`
  125. """
  126. return self._url.host
  127. def unicode_host(self) -> str | None:
  128. """The host part of the URL as a unicode string, or `None`.
  129. e.g. `host` in `https://user:pass@host:port/path?query#fragment`
  130. If the URL must be punycode encoded, this is the decoded host, e.g if the input URL is `https://£££.com`,
  131. `unicode_host()` will be `£££.com`
  132. """
  133. return self._url.unicode_host()
  134. @property
  135. def port(self) -> int | None:
  136. """The port part of the URL, or `None`.
  137. e.g. `port` in `https://user:pass@host:port/path?query#fragment`
  138. """
  139. return self._url.port
  140. @property
  141. def path(self) -> str | None:
  142. """The path part of the URL, or `None`.
  143. e.g. `/path` in `https://user:pass@host:port/path?query#fragment`
  144. """
  145. return self._url.path
  146. @property
  147. def query(self) -> str | None:
  148. """The query part of the URL, or `None`.
  149. e.g. `query` in `https://user:pass@host:port/path?query#fragment`
  150. """
  151. return self._url.query
  152. def query_params(self) -> list[tuple[str, str]]:
  153. """The query part of the URL as a list of key-value pairs.
  154. e.g. `[('foo', 'bar')]` in `https://user:pass@host:port/path?foo=bar#fragment`
  155. """
  156. return self._url.query_params()
  157. @property
  158. def fragment(self) -> str | None:
  159. """The fragment part of the URL, or `None`.
  160. e.g. `fragment` in `https://user:pass@host:port/path?query#fragment`
  161. """
  162. return self._url.fragment
  163. def unicode_string(self) -> str:
  164. """The URL as a unicode string, unlike `__str__()` this will not punycode encode the host.
  165. If the URL must be punycode encoded, this is the decoded string, e.g if the input URL is `https://£££.com`,
  166. `unicode_string()` will be `https://£££.com`
  167. """
  168. return self._url.unicode_string()
  169. def __str__(self) -> str:
  170. """The URL as a string, this will punycode encode the host if required."""
  171. return str(self._url)
  172. def __repr__(self) -> str:
  173. return f'{self.__class__.__name__}({str(self._url)!r})'
  174. def __deepcopy__(self, memo: dict) -> Self:
  175. return self.__class__(self._url)
  176. def __eq__(self, other: Any) -> bool:
  177. return self.__class__ is other.__class__ and self._url == other._url
  178. def __hash__(self) -> int:
  179. return hash(self._url)
  180. @classmethod
  181. def build(
  182. cls,
  183. *,
  184. scheme: str,
  185. username: str | None = None,
  186. password: str | None = None,
  187. host: str,
  188. port: int | None = None,
  189. path: str | None = None,
  190. query: str | None = None,
  191. fragment: str | None = None,
  192. ) -> Self:
  193. """Build a new `Url` instance from its component parts.
  194. Args:
  195. scheme: The scheme part of the URL.
  196. username: The username part of the URL, or omit for no username.
  197. password: The password part of the URL, or omit for no password.
  198. host: The host part of the URL.
  199. port: The port part of the URL, or omit for no port.
  200. path: The path part of the URL, or omit for no path.
  201. query: The query part of the URL, or omit for no query.
  202. fragment: The fragment part of the URL, or omit for no fragment.
  203. Returns:
  204. An instance of URL
  205. """
  206. return cls(
  207. _CoreUrl.build(
  208. scheme=scheme,
  209. username=username,
  210. password=password,
  211. host=host,
  212. port=port,
  213. path=path,
  214. query=query,
  215. fragment=fragment,
  216. )
  217. )
  218. @classmethod
  219. def __get_pydantic_core_schema__(
  220. cls, source: type[_BaseUrl], handler: GetCoreSchemaHandler
  221. ) -> core_schema.CoreSchema:
  222. def wrap_val(v, h):
  223. if isinstance(v, source):
  224. return v
  225. if isinstance(v, _BaseUrl):
  226. v = str(v)
  227. core_url = h(v)
  228. instance = source.__new__(source)
  229. instance._url = core_url
  230. return instance
  231. return core_schema.no_info_wrap_validator_function(
  232. wrap_val,
  233. schema=core_schema.url_schema(**cls._constraints.defined_constraints),
  234. serialization=core_schema.to_string_ser_schema(),
  235. )
  236. @classmethod
  237. def __get_pydantic_json_schema__(
  238. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  239. ) -> JsonSchemaValue:
  240. # we use the url schema for json schema generation, but we might have to extract it from
  241. # the function-wrap schema we use as a tool for validation on initialization
  242. inner_schema = core_schema['schema'] if core_schema['type'] == 'function-wrap' else core_schema
  243. return handler(inner_schema)
  244. __pydantic_serializer__ = SchemaSerializer(core_schema.any_schema(serialization=core_schema.to_string_ser_schema()))
  245. class _BaseMultiHostUrl:
  246. _constraints: ClassVar[UrlConstraints] = UrlConstraints()
  247. _url: _CoreMultiHostUrl
  248. def __init__(self, url: str | _CoreMultiHostUrl | _BaseMultiHostUrl) -> None:
  249. self._url = _build_type_adapter(self.__class__).validate_python(url)._url
  250. @property
  251. def scheme(self) -> str:
  252. """The scheme part of the URL.
  253. e.g. `https` in `https://foo.com,bar.com/path?query#fragment`
  254. """
  255. return self._url.scheme
  256. @property
  257. def path(self) -> str | None:
  258. """The path part of the URL, or `None`.
  259. e.g. `/path` in `https://foo.com,bar.com/path?query#fragment`
  260. """
  261. return self._url.path
  262. @property
  263. def query(self) -> str | None:
  264. """The query part of the URL, or `None`.
  265. e.g. `query` in `https://foo.com,bar.com/path?query#fragment`
  266. """
  267. return self._url.query
  268. def query_params(self) -> list[tuple[str, str]]:
  269. """The query part of the URL as a list of key-value pairs.
  270. e.g. `[('foo', 'bar')]` in `https://foo.com,bar.com/path?query#fragment`
  271. """
  272. return self._url.query_params()
  273. @property
  274. def fragment(self) -> str | None:
  275. """The fragment part of the URL, or `None`.
  276. e.g. `fragment` in `https://foo.com,bar.com/path?query#fragment`
  277. """
  278. return self._url.fragment
  279. def hosts(self) -> list[MultiHostHost]:
  280. '''The hosts of the `MultiHostUrl` as [`MultiHostHost`][pydantic_core.MultiHostHost] typed dicts.
  281. ```python
  282. from pydantic_core import MultiHostUrl
  283. mhu = MultiHostUrl('https://foo.com:123,foo:bar@bar.com/path')
  284. print(mhu.hosts())
  285. """
  286. [
  287. {'username': None, 'password': None, 'host': 'foo.com', 'port': 123},
  288. {'username': 'foo', 'password': 'bar', 'host': 'bar.com', 'port': 443}
  289. ]
  290. ```
  291. Returns:
  292. A list of dicts, each representing a host.
  293. '''
  294. return self._url.hosts()
  295. def unicode_string(self) -> str:
  296. """The URL as a unicode string, unlike `__str__()` this will not punycode encode the hosts."""
  297. return self._url.unicode_string()
  298. def __str__(self) -> str:
  299. """The URL as a string, this will punycode encode the host if required."""
  300. return str(self._url)
  301. def __repr__(self) -> str:
  302. return f'{self.__class__.__name__}({str(self._url)!r})'
  303. def __deepcopy__(self, memo: dict) -> Self:
  304. return self.__class__(self._url)
  305. def __eq__(self, other: Any) -> bool:
  306. return self.__class__ is other.__class__ and self._url == other._url
  307. def __hash__(self) -> int:
  308. return hash(self._url)
  309. @classmethod
  310. def build(
  311. cls,
  312. *,
  313. scheme: str,
  314. hosts: list[MultiHostHost] | None = None,
  315. username: str | None = None,
  316. password: str | None = None,
  317. host: str | None = None,
  318. port: int | None = None,
  319. path: str | None = None,
  320. query: str | None = None,
  321. fragment: str | None = None,
  322. ) -> Self:
  323. """Build a new `MultiHostUrl` instance from its component parts.
  324. This method takes either `hosts` - a list of `MultiHostHost` typed dicts, or the individual components
  325. `username`, `password`, `host` and `port`.
  326. Args:
  327. scheme: The scheme part of the URL.
  328. hosts: Multiple hosts to build the URL from.
  329. username: The username part of the URL.
  330. password: The password part of the URL.
  331. host: The host part of the URL.
  332. port: The port part of the URL.
  333. path: The path part of the URL.
  334. query: The query part of the URL, or omit for no query.
  335. fragment: The fragment part of the URL, or omit for no fragment.
  336. Returns:
  337. An instance of `MultiHostUrl`
  338. """
  339. return cls(
  340. _CoreMultiHostUrl.build(
  341. scheme=scheme,
  342. hosts=hosts,
  343. username=username,
  344. password=password,
  345. host=host,
  346. port=port,
  347. path=path,
  348. query=query,
  349. fragment=fragment,
  350. )
  351. )
  352. @classmethod
  353. def __get_pydantic_core_schema__(
  354. cls, source: type[_BaseMultiHostUrl], handler: GetCoreSchemaHandler
  355. ) -> core_schema.CoreSchema:
  356. def wrap_val(v, h):
  357. if isinstance(v, source):
  358. return v
  359. if isinstance(v, _BaseMultiHostUrl):
  360. v = str(v)
  361. core_url = h(v)
  362. instance = source.__new__(source)
  363. instance._url = core_url
  364. return instance
  365. return core_schema.no_info_wrap_validator_function(
  366. wrap_val,
  367. schema=core_schema.multi_host_url_schema(**cls._constraints.defined_constraints),
  368. serialization=core_schema.to_string_ser_schema(),
  369. )
  370. @classmethod
  371. def __get_pydantic_json_schema__(
  372. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  373. ) -> JsonSchemaValue:
  374. # we use the url schema for json schema generation, but we might have to extract it from
  375. # the function-wrap schema we use as a tool for validation on initialization
  376. inner_schema = core_schema['schema'] if core_schema['type'] == 'function-wrap' else core_schema
  377. return handler(inner_schema)
  378. __pydantic_serializer__ = SchemaSerializer(core_schema.any_schema(serialization=core_schema.to_string_ser_schema()))
  379. @lru_cache
  380. def _build_type_adapter(cls: type[_BaseUrl | _BaseMultiHostUrl]) -> TypeAdapter:
  381. return TypeAdapter(cls)
  382. class AnyUrl(_BaseUrl):
  383. """Base type for all URLs.
  384. * Any scheme allowed
  385. * Top-level domain (TLD) not required
  386. * Host not required
  387. Assuming an input URL of `http://samuel:pass@example.com:8000/the/path/?query=here#fragment=is;this=bit`,
  388. the types export the following properties:
  389. - `scheme`: the URL scheme (`http`), always set.
  390. - `host`: the URL host (`example.com`).
  391. - `username`: optional username if included (`samuel`).
  392. - `password`: optional password if included (`pass`).
  393. - `port`: optional port (`8000`).
  394. - `path`: optional path (`/the/path/`).
  395. - `query`: optional URL query (for example, `GET` arguments or "search string", such as `query=here`).
  396. - `fragment`: optional fragment (`fragment=is;this=bit`).
  397. """
  398. # Note: all single host urls inherit from `AnyUrl` to preserve compatibility with pre-v2.10 code
  399. # Where urls were annotated variants of `AnyUrl`, which was an alias to `pydantic_core.Url`
  400. class AnyHttpUrl(AnyUrl):
  401. """A type that will accept any http or https URL.
  402. * TLD not required
  403. * Host not required
  404. """
  405. _constraints = UrlConstraints(allowed_schemes=['http', 'https'])
  406. class HttpUrl(AnyUrl):
  407. """A type that will accept any http or https URL.
  408. * TLD not required
  409. * Host not required
  410. * Max length 2083
  411. ```python
  412. from pydantic import BaseModel, HttpUrl, ValidationError
  413. class MyModel(BaseModel):
  414. url: HttpUrl
  415. m = MyModel(url='http://www.example.com') # (1)!
  416. print(m.url)
  417. #> http://www.example.com/
  418. try:
  419. MyModel(url='ftp://invalid.url')
  420. except ValidationError as e:
  421. print(e)
  422. '''
  423. 1 validation error for MyModel
  424. url
  425. URL scheme should be 'http' or 'https' [type=url_scheme, input_value='ftp://invalid.url', input_type=str]
  426. '''
  427. try:
  428. MyModel(url='not a url')
  429. except ValidationError as e:
  430. print(e)
  431. '''
  432. 1 validation error for MyModel
  433. url
  434. Input should be a valid URL, relative URL without a base [type=url_parsing, input_value='not a url', input_type=str]
  435. '''
  436. ```
  437. 1. Note: mypy would prefer `m = MyModel(url=HttpUrl('http://www.example.com'))`, but Pydantic will convert the string to an HttpUrl instance anyway.
  438. "International domains" (e.g. a URL where the host or TLD includes non-ascii characters) will be encoded via
  439. [punycode](https://en.wikipedia.org/wiki/Punycode) (see
  440. [this article](https://www.xudongz.com/blog/2017/idn-phishing/) for a good description of why this is important):
  441. ```python
  442. from pydantic import BaseModel, HttpUrl
  443. class MyModel(BaseModel):
  444. url: HttpUrl
  445. m1 = MyModel(url='http://puny£code.com')
  446. print(m1.url)
  447. #> http://xn--punycode-eja.com/
  448. m2 = MyModel(url='https://www.аррӏе.com/')
  449. print(m2.url)
  450. #> https://www.xn--80ak6aa92e.com/
  451. m3 = MyModel(url='https://www.example.珠宝/')
  452. print(m3.url)
  453. #> https://www.example.xn--pbt977c/
  454. ```
  455. !!! warning "Underscores in Hostnames"
  456. In Pydantic, underscores are allowed in all parts of a domain except the TLD.
  457. Technically this might be wrong - in theory the hostname cannot have underscores, but subdomains can.
  458. To explain this; consider the following two cases:
  459. - `exam_ple.co.uk`: the hostname is `exam_ple`, which should not be allowed since it contains an underscore.
  460. - `foo_bar.example.com` the hostname is `example`, which should be allowed since the underscore is in the subdomain.
  461. Without having an exhaustive list of TLDs, it would be impossible to differentiate between these two. Therefore
  462. underscores are allowed, but you can always do further validation in a validator if desired.
  463. Also, Chrome, Firefox, and Safari all currently accept `http://exam_ple.com` as a URL, so we're in good
  464. (or at least big) company.
  465. """
  466. _constraints = UrlConstraints(max_length=2083, allowed_schemes=['http', 'https'])
  467. class AnyWebsocketUrl(AnyUrl):
  468. """A type that will accept any ws or wss URL.
  469. * TLD not required
  470. * Host not required
  471. """
  472. _constraints = UrlConstraints(allowed_schemes=['ws', 'wss'])
  473. class WebsocketUrl(AnyUrl):
  474. """A type that will accept any ws or wss URL.
  475. * TLD not required
  476. * Host not required
  477. * Max length 2083
  478. """
  479. _constraints = UrlConstraints(max_length=2083, allowed_schemes=['ws', 'wss'])
  480. class FileUrl(AnyUrl):
  481. """A type that will accept any file URL.
  482. * Host not required
  483. """
  484. _constraints = UrlConstraints(allowed_schemes=['file'])
  485. class FtpUrl(AnyUrl):
  486. """A type that will accept ftp URL.
  487. * TLD not required
  488. * Host not required
  489. """
  490. _constraints = UrlConstraints(allowed_schemes=['ftp'])
  491. class PostgresDsn(_BaseMultiHostUrl):
  492. """A type that will accept any Postgres DSN.
  493. * User info required
  494. * TLD not required
  495. * Host required
  496. * Supports multiple hosts
  497. If further validation is required, these properties can be used by validators to enforce specific behaviour:
  498. ```python
  499. from pydantic import (
  500. BaseModel,
  501. HttpUrl,
  502. PostgresDsn,
  503. ValidationError,
  504. field_validator,
  505. )
  506. class MyModel(BaseModel):
  507. url: HttpUrl
  508. m = MyModel(url='http://www.example.com')
  509. # the repr() method for a url will display all properties of the url
  510. print(repr(m.url))
  511. #> HttpUrl('http://www.example.com/')
  512. print(m.url.scheme)
  513. #> http
  514. print(m.url.host)
  515. #> www.example.com
  516. print(m.url.port)
  517. #> 80
  518. class MyDatabaseModel(BaseModel):
  519. db: PostgresDsn
  520. @field_validator('db')
  521. def check_db_name(cls, v):
  522. assert v.path and len(v.path) > 1, 'database must be provided'
  523. return v
  524. m = MyDatabaseModel(db='postgres://user:pass@localhost:5432/foobar')
  525. print(m.db)
  526. #> postgres://user:pass@localhost:5432/foobar
  527. try:
  528. MyDatabaseModel(db='postgres://user:pass@localhost:5432')
  529. except ValidationError as e:
  530. print(e)
  531. '''
  532. 1 validation error for MyDatabaseModel
  533. db
  534. Assertion failed, database must be provided
  535. assert (None)
  536. + where None = PostgresDsn('postgres://user:pass@localhost:5432').path [type=assertion_error, input_value='postgres://user:pass@localhost:5432', input_type=str]
  537. '''
  538. ```
  539. """
  540. _constraints = UrlConstraints(
  541. host_required=True,
  542. allowed_schemes=[
  543. 'postgres',
  544. 'postgresql',
  545. 'postgresql+asyncpg',
  546. 'postgresql+pg8000',
  547. 'postgresql+psycopg',
  548. 'postgresql+psycopg2',
  549. 'postgresql+psycopg2cffi',
  550. 'postgresql+py-postgresql',
  551. 'postgresql+pygresql',
  552. ],
  553. )
  554. @property
  555. def host(self) -> str:
  556. """The required URL host."""
  557. return self._url.host # pyright: ignore[reportAttributeAccessIssue]
  558. class CockroachDsn(AnyUrl):
  559. """A type that will accept any Cockroach DSN.
  560. * User info required
  561. * TLD not required
  562. * Host required
  563. """
  564. _constraints = UrlConstraints(
  565. host_required=True,
  566. allowed_schemes=[
  567. 'cockroachdb',
  568. 'cockroachdb+psycopg2',
  569. 'cockroachdb+asyncpg',
  570. ],
  571. )
  572. @property
  573. def host(self) -> str:
  574. """The required URL host."""
  575. return self._url.host # pyright: ignore[reportReturnType]
  576. class AmqpDsn(AnyUrl):
  577. """A type that will accept any AMQP DSN.
  578. * User info required
  579. * TLD not required
  580. * Host not required
  581. """
  582. _constraints = UrlConstraints(allowed_schemes=['amqp', 'amqps'])
  583. class RedisDsn(AnyUrl):
  584. """A type that will accept any Redis DSN.
  585. * User info required
  586. * TLD not required
  587. * Host required (e.g., `rediss://:pass@localhost`)
  588. """
  589. _constraints = UrlConstraints(
  590. allowed_schemes=['redis', 'rediss'],
  591. default_host='localhost',
  592. default_port=6379,
  593. default_path='/0',
  594. host_required=True,
  595. )
  596. @property
  597. def host(self) -> str:
  598. """The required URL host."""
  599. return self._url.host # pyright: ignore[reportReturnType]
  600. class MongoDsn(_BaseMultiHostUrl):
  601. """A type that will accept any MongoDB DSN.
  602. * User info not required
  603. * Database name not required
  604. * Port not required
  605. * User info may be passed without user part (e.g., `mongodb://mongodb0.example.com:27017`).
  606. """
  607. _constraints = UrlConstraints(allowed_schemes=['mongodb', 'mongodb+srv'], default_port=27017)
  608. class KafkaDsn(AnyUrl):
  609. """A type that will accept any Kafka DSN.
  610. * User info required
  611. * TLD not required
  612. * Host not required
  613. """
  614. _constraints = UrlConstraints(allowed_schemes=['kafka'], default_host='localhost', default_port=9092)
  615. class NatsDsn(_BaseMultiHostUrl):
  616. """A type that will accept any NATS DSN.
  617. NATS is a connective technology built for the ever increasingly hyper-connected world.
  618. It is a single technology that enables applications to securely communicate across
  619. any combination of cloud vendors, on-premise, edge, web and mobile, and devices.
  620. More: https://nats.io
  621. """
  622. _constraints = UrlConstraints(
  623. allowed_schemes=['nats', 'tls', 'ws', 'wss'], default_host='localhost', default_port=4222
  624. )
  625. class MySQLDsn(AnyUrl):
  626. """A type that will accept any MySQL DSN.
  627. * User info required
  628. * TLD not required
  629. * Host not required
  630. """
  631. _constraints = UrlConstraints(
  632. allowed_schemes=[
  633. 'mysql',
  634. 'mysql+mysqlconnector',
  635. 'mysql+aiomysql',
  636. 'mysql+asyncmy',
  637. 'mysql+mysqldb',
  638. 'mysql+pymysql',
  639. 'mysql+cymysql',
  640. 'mysql+pyodbc',
  641. ],
  642. default_port=3306,
  643. host_required=True,
  644. )
  645. class MariaDBDsn(AnyUrl):
  646. """A type that will accept any MariaDB DSN.
  647. * User info required
  648. * TLD not required
  649. * Host not required
  650. """
  651. _constraints = UrlConstraints(
  652. allowed_schemes=['mariadb', 'mariadb+mariadbconnector', 'mariadb+pymysql'],
  653. default_port=3306,
  654. )
  655. class ClickHouseDsn(AnyUrl):
  656. """A type that will accept any ClickHouse DSN.
  657. * User info required
  658. * TLD not required
  659. * Host not required
  660. """
  661. _constraints = UrlConstraints(
  662. allowed_schemes=['clickhouse+native', 'clickhouse+asynch'],
  663. default_host='localhost',
  664. default_port=9000,
  665. )
  666. class SnowflakeDsn(AnyUrl):
  667. """A type that will accept any Snowflake DSN.
  668. * User info required
  669. * TLD not required
  670. * Host required
  671. """
  672. _constraints = UrlConstraints(
  673. allowed_schemes=['snowflake'],
  674. host_required=True,
  675. )
  676. @property
  677. def host(self) -> str:
  678. """The required URL host."""
  679. return self._url.host # pyright: ignore[reportReturnType]
  680. def import_email_validator() -> None:
  681. global email_validator
  682. try:
  683. import email_validator
  684. except ImportError as e:
  685. raise ImportError('email-validator is not installed, run `pip install pydantic[email]`') from e
  686. if not version('email-validator').partition('.')[0] == '2':
  687. raise ImportError('email-validator version >= 2.0 required, run pip install -U email-validator')
  688. if TYPE_CHECKING:
  689. EmailStr = Annotated[str, ...]
  690. else:
  691. class EmailStr:
  692. """
  693. Info:
  694. To use this type, you need to install the optional
  695. [`email-validator`](https://github.com/JoshData/python-email-validator) package:
  696. ```bash
  697. pip install email-validator
  698. ```
  699. Validate email addresses.
  700. ```python
  701. from pydantic import BaseModel, EmailStr
  702. class Model(BaseModel):
  703. email: EmailStr
  704. print(Model(email='contact@mail.com'))
  705. #> email='contact@mail.com'
  706. ```
  707. """ # noqa: D212
  708. @classmethod
  709. def __get_pydantic_core_schema__(
  710. cls,
  711. _source: type[Any],
  712. _handler: GetCoreSchemaHandler,
  713. ) -> core_schema.CoreSchema:
  714. import_email_validator()
  715. return core_schema.no_info_after_validator_function(cls._validate, core_schema.str_schema())
  716. @classmethod
  717. def __get_pydantic_json_schema__(
  718. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  719. ) -> JsonSchemaValue:
  720. field_schema = handler(core_schema)
  721. field_schema.update(type='string', format='email')
  722. return field_schema
  723. @classmethod
  724. def _validate(cls, input_value: str, /) -> str:
  725. return validate_email(input_value)[1]
  726. class NameEmail(_repr.Representation):
  727. """
  728. Info:
  729. To use this type, you need to install the optional
  730. [`email-validator`](https://github.com/JoshData/python-email-validator) package:
  731. ```bash
  732. pip install email-validator
  733. ```
  734. Validate a name and email address combination, as specified by
  735. [RFC 5322](https://datatracker.ietf.org/doc/html/rfc5322#section-3.4).
  736. The `NameEmail` has two properties: `name` and `email`.
  737. In case the `name` is not provided, it's inferred from the email address.
  738. ```python
  739. from pydantic import BaseModel, NameEmail
  740. class User(BaseModel):
  741. email: NameEmail
  742. user = User(email='Fred Bloggs <fred.bloggs@example.com>')
  743. print(user.email)
  744. #> Fred Bloggs <fred.bloggs@example.com>
  745. print(user.email.name)
  746. #> Fred Bloggs
  747. user = User(email='fred.bloggs@example.com')
  748. print(user.email)
  749. #> fred.bloggs <fred.bloggs@example.com>
  750. print(user.email.name)
  751. #> fred.bloggs
  752. ```
  753. """ # noqa: D212
  754. __slots__ = 'name', 'email'
  755. def __init__(self, name: str, email: str):
  756. self.name = name
  757. self.email = email
  758. def __eq__(self, other: Any) -> bool:
  759. return isinstance(other, NameEmail) and (self.name, self.email) == (other.name, other.email)
  760. @classmethod
  761. def __get_pydantic_json_schema__(
  762. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  763. ) -> JsonSchemaValue:
  764. field_schema = handler(core_schema)
  765. field_schema.update(type='string', format='name-email')
  766. return field_schema
  767. @classmethod
  768. def __get_pydantic_core_schema__(
  769. cls,
  770. _source: type[Any],
  771. _handler: GetCoreSchemaHandler,
  772. ) -> core_schema.CoreSchema:
  773. import_email_validator()
  774. return core_schema.no_info_after_validator_function(
  775. cls._validate,
  776. core_schema.json_or_python_schema(
  777. json_schema=core_schema.str_schema(),
  778. python_schema=core_schema.union_schema(
  779. [core_schema.is_instance_schema(cls), core_schema.str_schema()],
  780. custom_error_type='name_email_type',
  781. custom_error_message='Input is not a valid NameEmail',
  782. ),
  783. serialization=core_schema.to_string_ser_schema(),
  784. ),
  785. )
  786. @classmethod
  787. def _validate(cls, input_value: Self | str, /) -> Self:
  788. if isinstance(input_value, str):
  789. name, email = validate_email(input_value)
  790. return cls(name, email)
  791. else:
  792. return input_value
  793. def __str__(self) -> str:
  794. if '@' in self.name:
  795. return f'"{self.name}" <{self.email}>'
  796. return f'{self.name} <{self.email}>'
  797. IPvAnyAddressType: TypeAlias = 'IPv4Address | IPv6Address'
  798. IPvAnyInterfaceType: TypeAlias = 'IPv4Interface | IPv6Interface'
  799. IPvAnyNetworkType: TypeAlias = 'IPv4Network | IPv6Network'
  800. if TYPE_CHECKING:
  801. IPvAnyAddress = IPvAnyAddressType
  802. IPvAnyInterface = IPvAnyInterfaceType
  803. IPvAnyNetwork = IPvAnyNetworkType
  804. else:
  805. class IPvAnyAddress:
  806. """Validate an IPv4 or IPv6 address.
  807. ```python
  808. from pydantic import BaseModel
  809. from pydantic.networks import IPvAnyAddress
  810. class IpModel(BaseModel):
  811. ip: IPvAnyAddress
  812. print(IpModel(ip='127.0.0.1'))
  813. #> ip=IPv4Address('127.0.0.1')
  814. try:
  815. IpModel(ip='http://www.example.com')
  816. except ValueError as e:
  817. print(e.errors())
  818. '''
  819. [
  820. {
  821. 'type': 'ip_any_address',
  822. 'loc': ('ip',),
  823. 'msg': 'value is not a valid IPv4 or IPv6 address',
  824. 'input': 'http://www.example.com',
  825. }
  826. ]
  827. '''
  828. ```
  829. """
  830. __slots__ = ()
  831. def __new__(cls, value: Any) -> IPvAnyAddressType:
  832. """Validate an IPv4 or IPv6 address."""
  833. try:
  834. return IPv4Address(value)
  835. except ValueError:
  836. pass
  837. try:
  838. return IPv6Address(value)
  839. except ValueError:
  840. raise PydanticCustomError('ip_any_address', 'value is not a valid IPv4 or IPv6 address')
  841. @classmethod
  842. def __get_pydantic_json_schema__(
  843. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  844. ) -> JsonSchemaValue:
  845. field_schema = {}
  846. field_schema.update(type='string', format='ipvanyaddress')
  847. return field_schema
  848. @classmethod
  849. def __get_pydantic_core_schema__(
  850. cls,
  851. _source: type[Any],
  852. _handler: GetCoreSchemaHandler,
  853. ) -> core_schema.CoreSchema:
  854. return core_schema.no_info_plain_validator_function(
  855. cls._validate, serialization=core_schema.to_string_ser_schema()
  856. )
  857. @classmethod
  858. def _validate(cls, input_value: Any, /) -> IPvAnyAddressType:
  859. return cls(input_value) # type: ignore[return-value]
  860. class IPvAnyInterface:
  861. """Validate an IPv4 or IPv6 interface."""
  862. __slots__ = ()
  863. def __new__(cls, value: NetworkType) -> IPvAnyInterfaceType:
  864. """Validate an IPv4 or IPv6 interface."""
  865. try:
  866. return IPv4Interface(value)
  867. except ValueError:
  868. pass
  869. try:
  870. return IPv6Interface(value)
  871. except ValueError:
  872. raise PydanticCustomError('ip_any_interface', 'value is not a valid IPv4 or IPv6 interface')
  873. @classmethod
  874. def __get_pydantic_json_schema__(
  875. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  876. ) -> JsonSchemaValue:
  877. field_schema = {}
  878. field_schema.update(type='string', format='ipvanyinterface')
  879. return field_schema
  880. @classmethod
  881. def __get_pydantic_core_schema__(
  882. cls,
  883. _source: type[Any],
  884. _handler: GetCoreSchemaHandler,
  885. ) -> core_schema.CoreSchema:
  886. return core_schema.no_info_plain_validator_function(
  887. cls._validate, serialization=core_schema.to_string_ser_schema()
  888. )
  889. @classmethod
  890. def _validate(cls, input_value: NetworkType, /) -> IPvAnyInterfaceType:
  891. return cls(input_value) # type: ignore[return-value]
  892. class IPvAnyNetwork:
  893. """Validate an IPv4 or IPv6 network."""
  894. __slots__ = ()
  895. def __new__(cls, value: NetworkType) -> IPvAnyNetworkType:
  896. """Validate an IPv4 or IPv6 network."""
  897. # Assume IP Network is defined with a default value for `strict` argument.
  898. # Define your own class if you want to specify network address check strictness.
  899. try:
  900. return IPv4Network(value)
  901. except ValueError:
  902. pass
  903. try:
  904. return IPv6Network(value)
  905. except ValueError:
  906. raise PydanticCustomError('ip_any_network', 'value is not a valid IPv4 or IPv6 network')
  907. @classmethod
  908. def __get_pydantic_json_schema__(
  909. cls, core_schema: core_schema.CoreSchema, handler: _schema_generation_shared.GetJsonSchemaHandler
  910. ) -> JsonSchemaValue:
  911. field_schema = {}
  912. field_schema.update(type='string', format='ipvanynetwork')
  913. return field_schema
  914. @classmethod
  915. def __get_pydantic_core_schema__(
  916. cls,
  917. _source: type[Any],
  918. _handler: GetCoreSchemaHandler,
  919. ) -> core_schema.CoreSchema:
  920. return core_schema.no_info_plain_validator_function(
  921. cls._validate, serialization=core_schema.to_string_ser_schema()
  922. )
  923. @classmethod
  924. def _validate(cls, input_value: NetworkType, /) -> IPvAnyNetworkType:
  925. return cls(input_value) # type: ignore[return-value]
  926. def _build_pretty_email_regex() -> re.Pattern[str]:
  927. name_chars = r'[\w!#$%&\'*+\-/=?^_`{|}~]'
  928. unquoted_name_group = rf'((?:{name_chars}+\s+)*{name_chars}+)'
  929. quoted_name_group = r'"((?:[^"]|\")+)"'
  930. email_group = r'<(.+)>'
  931. return re.compile(rf'\s*(?:{unquoted_name_group}|{quoted_name_group})?\s*{email_group}\s*')
  932. pretty_email_regex = _build_pretty_email_regex()
  933. MAX_EMAIL_LENGTH = 2048
  934. """Maximum length for an email.
  935. A somewhat arbitrary but very generous number compared to what is allowed by most implementations.
  936. """
  937. def validate_email(value: str) -> tuple[str, str]:
  938. """Email address validation using [email-validator](https://pypi.org/project/email-validator/).
  939. Returns:
  940. A tuple containing the local part of the email (or the name for "pretty" email addresses)
  941. and the normalized email.
  942. Raises:
  943. PydanticCustomError: If the email is invalid.
  944. Note:
  945. Note that:
  946. * Raw IP address (literal) domain parts are not allowed.
  947. * `"John Doe <local_part@domain.com>"` style "pretty" email addresses are processed.
  948. * Spaces are striped from the beginning and end of addresses, but no error is raised.
  949. """
  950. if email_validator is None:
  951. import_email_validator()
  952. if len(value) > MAX_EMAIL_LENGTH:
  953. raise PydanticCustomError(
  954. 'value_error',
  955. 'value is not a valid email address: {reason}',
  956. {'reason': f'Length must not exceed {MAX_EMAIL_LENGTH} characters'},
  957. )
  958. m = pretty_email_regex.fullmatch(value)
  959. name: str | None = None
  960. if m:
  961. unquoted_name, quoted_name, value = m.groups()
  962. name = unquoted_name or quoted_name
  963. email = value.strip()
  964. try:
  965. parts = email_validator.validate_email(email, check_deliverability=False)
  966. except email_validator.EmailNotValidError as e:
  967. raise PydanticCustomError(
  968. 'value_error', 'value is not a valid email address: {reason}', {'reason': str(e.args[0])}
  969. ) from e
  970. email = parts.normalized
  971. assert email is not None
  972. name = name or parts.local_part
  973. return name, email
  974. __getattr__ = getattr_migration(__name__)