odmantic icon indicating copy to clipboard operation
odmantic copied to clipboard

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

Open pfrantz opened this issue 1 year ago • 1 comments

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 avatar Dec 24 '23 03:12 pfrantz

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)

Lilneo786 avatar Dec 24 '23 06:12 Lilneo786