[英]How to validate a Host field in python pydantic?
when you have host field to validate and host can be:当您有要验证的主机字段时,主机可以是:
ipv4 address: 127.0.0.1 IPv4地址:127.0.0.1
ipv6 address: 2001:db8:ff00:42 ipv6 地址:2001:db8:ff00:42
example.com例子.com
int.domain: puny£code.com int.domain: puny£code.com
11.11.101.11.example.org 11.11.101.11.example.org
Any suggestion, how to validate the field using pydantic?任何建议,如何使用 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我会查找一些正则表达式并使用
constr(regex=...)
或使用https://pydantic-docs.helpmanual.io/usage/types/和https://pydantic-docs.helpmanual 中的一些预制类型。 io/用法/类型/#urls
https://pydantic-docs.helpmanual.io/usage/types/#arguments-to-constr 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.