MCPcopy
hub / github.com/pydantic/pydantic / validate_host

Method validate_host

pydantic/v1/networks.py:342–381  ·  view source on GitHub ↗
(cls, parts: 'Parts')

Source from the content-addressed store, hash-verified

340
341 @classmethod
342 def validate_host(cls, parts: 'Parts') -> Tuple[str, Optional[str], str, bool]:
343 tld, host_type, rebuild = None, None, False
344 for f in ('domain', 'ipv4', 'ipv6'):
345 host = parts[f] # type: ignore[literal-required]
346 if host:
347 host_type = f
348 break
349
350 if host is None:
351 if cls.host_required:
352 raise errors.UrlHostError()
353 elif host_type == 'domain':
354 is_international = False
355 d = ascii_domain_regex().fullmatch(host)
356 if d is None:
357 d = int_domain_regex().fullmatch(host)
358 if d is None:
359 raise errors.UrlHostError()
360 is_international = True
361
362 tld = d.group('tld')
363 if tld is None and not is_international:
364 d = int_domain_regex().fullmatch(host)
365 assert d is not None
366 tld = d.group('tld')
367 is_international = True
368
369 if tld is not None:
370 tld = tld[1:]
371 elif cls.tld_required:
372 raise errors.UrlHostTldError()
373
374 if is_international:
375 host_type = 'int_domain'
376 rebuild = True
377 host = host.encode('idna').decode('ascii')
378 if tld is not None:
379 tld = tld.encode('idna').decode('ascii')
380
381 return host, tld, host_type, rebuild # type: ignore
382
383 @staticmethod
384 def get_default_parts(parts: 'Parts') -> 'Parts':

Callers 2

_build_urlMethod · 0.80
_build_urlMethod · 0.80

Calls 4

ascii_domain_regexFunction · 0.85
int_domain_regexFunction · 0.85
decodeMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected