我需要确保该字符串不包含西里尔字符。我这样检查:
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中文网其他相关文章!