Pydantic 验证。检查字符串是否不包含某些字符

WBOY
发布: 2024-02-09 10:06:14
转载
1238 人浏览过

Pydantic 验证。检查字符串是否不包含某些字符

问题内容

我需要确保该字符串不包含西里尔字符。我这样检查:

from pydantic import basemodel, field

class mymodel(basemodel):
    content_en: str = field(pattern=r"[^а-яА-Я]")


data = mymodel(content_en="has wrong content 'йцукен'")
print(data)
>>> content_en="has wrong content 'йцукен'"
登录后复制

但是当我将包含西里尔字母的字符串传递到 content_en 字段时,不会引发错误。

预计:

pydantic_core._pydantic_core.validationerror: 1 validation error for mymodel
...
登录后复制

如何检查正确?

python 3.8

派丹蒂克2.5

解决方案(感谢@chepner):

class MyModel(BaseModel):
    content_en: str = Field(pattern=r"^[^а-яА-ЯёЁ]*$")
登录后复制


正确答案


您的模式与包含至少一个非西里尔字符的任何字符串匹配,而不是仅由非西里尔字符组成的字符串。

>>> mymodel(content_en="has wrong content 'йцукен'")
mymodel(content_en="has wrong content 'йцукен'")
>>> mymodel(content_en="йцукен")
traceback (most recent call last):
  file "<stdin>", line 1, in <module>
  file "/users/chepner/py311/lib/python3.11/site-packages/pydantic/main.py", line 164, in __init__
    __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.validationerror: 1 validation error for mymodel
content_en
  string should match pattern '[^а-яА-Я]' [type=string_pattern_mismatch, input_value='йцукен', input_type=str]
    for further information visit https://errors.pydantic.dev/2.5/v/string_pattern_mismatch
登录后复制

正确的模式是 ^[^а-яА-Я]*$:

>>> class MyModel(BaseModel):
...     content_en: str = Field(pattern=r"^[^а-яА-Я]*$")
...
>>> MyModel(content_en="Has wrong content 'йцукен'")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/chepner/py311/lib/python3.11/site-packages/pydantic/main.py", line 164, in __init__
    __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 1 validation error for MyModel
content_en
  String should match pattern '^[^а-яА-Я]*$' [type=string_pattern_mismatch, input_value="Has wrong content 'йцукен'", input_type=str]
    For further information visit https://errors.pydantic.dev/2.5/v/string_pattern_mismatch
登录后复制

以上是Pydantic 验证。检查字符串是否不包含某些字符的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!