when you have host field to validate and host can be:
ipv4 address: 127.0.0.1
ipv6 address: 2001:db8:ff00:42
example.com
int.domain: puny£code.com
11.11.101.11.example.org
Any suggestion, how to validate the field using pydantic?
I'd look up some regexes and use constr(regex=...)
or use some premade types from https://pydantic-docs.helpmanual.io/usage/types/ and https://pydantic-docs.helpmanual.io/usage/types/#urls
https://pydantic-docs.helpmanual.io/usage/types/#arguments-to-constr
then use
from typing import Union
from ipaddress import IPv4Address
from pydantic import BaseModel, HttpUrl, parse_obj_as
class X(BaseModel):
host: Union[HttpUrl,IPv4Address,...]
parsed_host = parse_obj_as(X, your_host)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.