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

regex is removed in pydantic 2.x this causes the Field class to throw an exception if its used. #397

Open
pfrantz opened this issue Dec 24, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@pfrantz
Copy link

pfrantz commented Dec 24, 2023

Bug

if you define a field like

email: str = Field(regex=RE_EMAIL_PATTERN) 

The program when run gives the following exception:-

pydantic.errors.PydanticUserError: `regex` is removed. use `pattern` instead

Current Behavior

just define a Field that has a regex defined

Expected behavior

it would be good to align with Pydantic and support the patten keyword argument

Environment

  • ODMantic version: 1.0.0
  • MongoDB version: 7.0.2
  • Pydantic infos:
    pydantic version: 2.5.2
    pydantic-core version: 2.14.5
    pydantic-core build: profile=release pgo=true
    python version: 3.12.1 (main, Dec 10 2023, 13:40:31) [Clang 15.0.0 (clang-1500.0.40.1)]
    platform: macOS-14.1.2-arm64-arm-64bit
    related packages: fastapi-0.105.0 typing_extensions-4.9.0 pydantic-settings-2.1.0
...
  • Version of additional modules (if relevant):
    • ...

Additional context

Add any other context about the problem here.

@pfrantz pfrantz added the bug Something isn't working label Dec 24, 2023
@Lilneo786
Copy link

In Pydantic 2.x, the regex attribute for specifying regular expressions in the Field class has been removed, and you should use the pattern attribute instead. Here's how you can update your code:

from pydantic import BaseModel, Field, constr

class MyModel(BaseModel):
    email: constr(regex=RE_EMAIL_PATTERN)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants