Skip to content

Commit f45a40f

Browse files
committed
Fix failing test
1 parent 926bd78 commit f45a40f

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

sqlmodel/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,13 @@ def Field(
395395
current_schema_extra = schema_extra or {}
396396

397397
if IS_PYDANTIC_V2:
398-
current_schema_extra.update(pattern=pattern or regex)
398+
current_schema_extra.update(
399+
pattern=pattern or regex or current_schema_extra.get("pattern")
400+
)
399401
else:
400-
current_schema_extra.update(regex=regex or pattern)
402+
current_schema_extra.update(
403+
regex=regex or pattern or current_schema_extra.get("pattern")
404+
)
401405

402406
field_info = FieldInfo(
403407
default,

tests/test_pydantic/test_field.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ class Model(SQLModel):
6161
def test_field_regex_param(param: str):
6262
class DateModel(SQLModel):
6363
date_1: str = Field(**{param: r"^\d{2}-\d{2}-\d{4}$"})
64-
date_2: str = Field(schema_extra={param: r"^\d{2}-\d{2}-\d{4}$"})
6564

66-
DateModel(date_1="12-31-2024", date_2="12-31-2024")
67-
# Validates correctly
65+
DateModel(date_1="12-31-2024") # Validates correctly
6866

69-
with pytest.raises(ValidationError):
70-
DateModel(date_1="incorrect", date_2="12-31-2024") # date_1 pattern mismatch
67+
68+
def test_field_pattern_via_schema_extra():
69+
class DateModel(SQLModel):
70+
date_1: str = Field(schema_extra={"pattern": r"^\d{2}-\d{2}-\d{4}$"})
7171

7272
with pytest.raises(ValidationError):
73-
DateModel(date_1="12-31-2024", date_2="incorrect") # date_2 pattern mismatch
73+
DateModel(date_1="incorrect")

0 commit comments

Comments
 (0)