PHP前端开发

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

百变鹏仔 1天前 #Python
文章标签 字符串
问题内容

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

from pydantic import basemodel, fieldclass 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"^[^а-яА-ЯёЁ]*$")

正确答案


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

&gt;&gt;&gt; mymodel(content_en="has wrong content 'йцукен'")mymodel(content_en="has wrong content 'йцукен'")&gt;&gt;&gt; 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 mymodelcontent_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</module></stdin>

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

&gt;&gt;&gt; class MyModel(BaseModel):...     content_en: str = Field(pattern=r"^[^а-яА-Я]*$")...&gt;&gt;&gt; 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 MyModelcontent_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</module></stdin>