8000 Add regex patterns to JSON schema for `Decimal` type by Dima-Bulavenko · Pull Request #11987 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: Delete unwanted escaping "\" character from regex.
refactor: Update tests to match the new regex
  • Loading branch information
Dima-Bulavenko committed Jul 4, 2025
commit 5874292e75f955fe80334c7c083be0832edcf0df
4 changes: 2 additions & 2 deletions docs/concepts/json_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ print(Model.model_json_schema(mode='validation'))
'anyOf': [
{'type': 'number'},
{
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'type': 'string',
},
],
Expand All @@ -298,7 +298,7 @@ print(Model.model_json_schema(mode='serialization'))
'properties': {
'a': {
'default': '12.34',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'title': 'A',
'type': 'string',
}
Expand Down
2 changes: 1 addition & 1 deletion pydantic/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def get_decimal_pattern(schema: core_schema.DecimalSchema) -> str:
integer_places = 0

pattern = (
r'^(?!^[+-\.]*$)' # check string is not empty and not single or sequence of ".+-" characters.
r'^(?!^[-+.]*$)' # check string is not empty and not single or sequence of ".+-" characters.
r'[+-]?0*' # check "+-" optional characters in the very start and optional zeros.
r'(?:' # open non-capturing group
rf'\d{{0,{integer_places}}}$' # integer case
Expand Down
30 changes: 15 additions & 15 deletions tests/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ class Model(BaseModel):
{'type': 'number'},
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d 8000 {0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
],
'default': '12.34',
Expand All @@ -547,7 +547,7 @@ class Model(BaseModel):
'default': '12.34',
'title': 'B',
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
},
'title': 'Model',
Expand Down Expand Up @@ -1075,7 +1075,7 @@ class Model(BaseModel):
{'type': 'number'},
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
],
'title': 'A',
Expand Down Expand Up @@ -2042,7 +2042,7 @@ class A(BaseModel):
{'exclusiveMinimum': 2.0, 'type': 'number'},
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
]
},
Expand All @@ -2055,7 +2055,7 @@ class A(BaseModel):
{'type': 'number', 'exclusiveMaximum': 5},
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
]
},
Expand All @@ -2068,7 +2068,7 @@ class A(BaseModel):
{'type': 'number', 'minimum': 2},
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
]
},
Expand All @@ -2081,7 +2081,7 @@ class A(BaseModel):
{'type': 'number', 'maximum': 5},
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
]
},
Expand All @@ -2094,7 +2094,7 @@ class A(BaseModel):
{'type': 'number', 'multipleOf': 5},
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
]
},
Expand Down Expand Up @@ -2143,39 +2143,39 @@ class Foo(BaseModel):
Decimal,
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
),
(
{'lt': 5},
Decimal,
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
),
(
{'ge': 2},
Decimal,
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
),
(
{'le': 5},
Decimal,
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
),
(
{'multiple_of': 5},
Decimal,
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
),
],
Expand Down Expand Up @@ -5860,14 +5860,14 @@ class Model(BaseModel):
{
('Decimal', 'serialization'): {
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
('Decimal', 'validation'): {
'anyOf': [
{'type': 'number'},
{
'type': 'string',
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
},
]
},
Expand Down
Loading
0