> >content_en="haswrongcontent'йцукен'""/> > >content_en="haswrongcontent'йцукен'"">

Home  >  Article  >  Backend Development  >  Pydantic verification. Check if a string does not contain certain characters

Pydantic verification. Check if a string does not contain certain characters

WBOY
WBOYforward
2024-02-09 10:06:141238browse

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

Question content

I need to make sure that the string does not contain Cyrillic characters. I check like this:

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 'йцукен'"

But when I pass a string containing Cyrillic letters to the content_en field, no error is thrown.

Estimated:

pydantic_core._pydantic_core.validationerror: 1 validation error for mymodel
...

How to check correctness?

python 3.8

Padantic 2.5

Solution (thanks to @chepner):

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

Correct answer


Your pattern matches any string that contains at least one non-Cyrillic character, not a string consisting only of non-Cyrillic characters .

>>> 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

The correct pattern is ^[^а-яА-Я]*$:

>>> 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

The above is the detailed content of Pydantic verification. Check if a string does not contain certain characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete