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

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

WBOY
Release: 2024-02-09 10:06:14
forward
1283 people have browsed it

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

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
...
Copy after login

How to check correctness?

python 3.8

Padantic 2.5

Solution (thanks to @chepner):

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


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
Copy after login

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
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template