art049 / odmantic

Sync and Async ODM (Object Document Mapper) for MongoDB based on python type hints
http://art049.github.io/odmantic
ISC License
1.07k stars 94 forks source link

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

Open pfrantz opened 10 months ago

pfrantz commented 10 months ago

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

...

Additional context

Add any other context about the problem here.

Lilneo786 commented 10 months ago

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)