Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field.regex will allow an invalid string to pass validation #1470

Closed
kierandarcy opened this issue May 1, 2020 · 1 comment
Closed

Field.regex will allow an invalid string to pass validation #1470

kierandarcy opened this issue May 1, 2020 · 1 comment
Labels
bug V1 Bug related to Pydantic V1.X duplicate

Comments

@kierandarcy
Copy link
Contributor

kierandarcy commented May 1, 2020

Bug

The regex used to validate a string in pydantic.Field will allow a string that does not match the given pattern to pass without raising a validation error.

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.5.1
            pydantic compiled: True
                 install path: /home/kieran/projects/bimacademy/buildstream/prototypes/backend-admin/.venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.2 (default, Feb 26 2020, 02:56:10)  [GCC 7.4.0]
                     platform: Linux-4.15.0-99-generic-x86_64-with-glibc2.27
     optional deps. installed: []

This simple regex checks for any number of lowercase letters ("a - z") and spaces.

In the example below, the test="this Should Fail" should raise a validation error. It does not. I think this is because it is using re.match instead if re.fullmatch.

import re
import pydantic

class Test(pydantic.BaseModel): 
    test: str = pydantic.Field(regex="[a-z ]+", description="Allow lower case letters and spaces")

try:
    Test(test="this should pass")
except pydantic.ValidationError:
    print("This should have passed!")

try:
    Test(test="this Should Fail")
except pydantic.ValidationError:
    print("This should have failed!")

assert bool(re.fullmatch("[a-z ]+", "this should pass"))
assert not bool(re.fullmatch("[a-z ]+", "this Should Fail"))

Happy to submit a PR! (Looks like a nice easy one)

@kierandarcy kierandarcy added the bug V1 Bug related to Pydantic V1.X label May 1, 2020
@samuelcolvin
Copy link
Member

Not a bug, duplicate of #1396.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X duplicate
Projects
None yet
Development

No branches or pull requests

2 participants