The Field regex attribute was renamed pattern in pydantic v2 #1298
Unanswered
benoit-arcsec
asked this question in
Questions
Replies: 1 comment
-
I ended doing even another way. Based on from typing import Annotated
from pydantic import StringConstraints
from sqlmodel import Field, SQLModel
LicencePlate = Annotated[str, StringConstraints(pattern=r"\w-\w{3}-\w{3}")]
class Car(SQLModel):
license_plate: LicensePlate It seems cleaner like this and I can reuse the type. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
The regex is not validated and not documented in openapi.json.
This is caused because Pydantic V2 does not use the regex attribute anymore but the pattern attribute instead: https://docs.pydantic.dev/latest/concepts/fields/#string-constraints
In this case, it could be solved using Field or constr from Pydantic.
Yet, the fact that the regex is ignored is a footgun. And this footgun could be fixed by renaming the attribute to pattern in
sqlmodel.main.Field
.Operating System
Linux
Operating System Details
Ubuntu 24.04 (WSL2)
SQLModel Version
0.0.22
Python Version
3.12.3
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions