Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5874292

Browse files
refactor: Delete unwanted escaping "\" character from regex.
refactor: Update tests to match the new regex
1 parent e0e7e56 commit 5874292

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

docs/concepts/json_schema.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ print(Model.model_json_schema(mode='validation'))
279279
'anyOf': [
280280
{'type': 'number'},
281281
{
282-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
282+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
283283
'type': 'string',
284284
},
285285
],
@@ -298,7 +298,7 @@ print(Model.model_json_schema(mode='serialization'))
298298
'properties': {
299299
'a': {
300300
'default': '12.34',
301-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
301+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
302302
'title': 'A',
303303
'type': 'string',
304304
}

pydantic/json_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ def get_decimal_pattern(schema: core_schema.DecimalSchema) -> str:
687687
integer_places = 0
688688

689689
pattern = (
690-
r'^(?!^[+-\.]*$)' # check string is not empty and not single or sequence of ".+-" characters.
690+
r'^(?!^[-+.]*$)' # check string is not empty and not single or sequence of ".+-" characters.
691691
r'[+-]?0*' # check "+-" optional characters in the very start and optional zeros.
692692
r'(?:' # open non-capturing group
693693
rf'\d{{0,{integer_places}}}$' # integer case

tests/test_json_schema.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ class Model(BaseModel):
530530
{'type': 'number'},
531531
{
532532
'type': 'string',
533-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
533+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
534534
},
535535
],
536536
'default': '12.34',
@@ -547,7 +547,7 @@ class Model(BaseModel):
547547
'default': '12.34',
548548
'title': 'B',
549549
'type': 'string',
550-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
550+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
551551
},
552552
},
553553
'title': 'Model',
@@ -1075,7 +1075,7 @@ class Model(BaseModel):
10751075
{'type': 'number'},
10761076
{
10771077
'type': 'string',
1078-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
1078+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
10791079
},
10801080
],
10811081
'title': 'A',
@@ -2042,7 +2042,7 @@ class A(BaseModel):
20422042
{'exclusiveMinimum': 2.0, 'type': 'number'},
20432043
{
20442044
'type': 'string',
2045-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2045+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
20462046
},
20472047
]
20482048
},
@@ -2055,7 +2055,7 @@ class A(BaseModel):
20552055
{'type': 'number', 'exclusiveMaximum': 5},
20562056
{
20572057
'type': 'string',
2058-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2058+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
20592059
},
20602060
]
20612061
},
@@ -2068,7 +2068,7 @@ class A(BaseModel):
20682068
{'type': 'number', 'minimum': 2},
20692069
{
20702070
'type': 'string',
2071-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2071+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
20722072
},
20732073
]
20742074
},
@@ -2081,7 +2081,7 @@ class A(BaseModel):
20812081
{'type': 'number', 'maximum': 5},
20822082
{
20832083
'type': 'string',
2084-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2084+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
20852085
},
20862086
]
20872087
},
@@ -2094,7 +2094,7 @@ class A(BaseModel):
20942094
{'type': 'number', 'multipleOf': 5},
20952095
{
20962096
'type': 'string',
2097-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2097+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
20982098
},
20992099
]
21002100
},
@@ -2143,39 +2143,39 @@ class Foo(BaseModel):
21432143
Decimal,
21442144
{
21452145
'type': 'string',
2146-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2146+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
21472147
},
21482148
),
21492149
(
21502150
{'lt': 5},
21512151
Decimal,
21522152
{
21532153
'type': 'string',
2154-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2154+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
21552155
},
21562156
),
21572157
(
21582158
{'ge': 2},
21592159
Decimal,
21602160
{
21612161
'type': 'string',
2162-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2162+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
21632163
},
21642164
),
21652165
(
21662166
{'le': 5},
21672167
Decimal,
21682168
{
21692169
'type': 'string',
2170-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2170+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
21712171
},
21722172
),
21732173
(
21742174
{'multiple_of': 5},
21752175
Decimal,
21762176
{
21772177
'type': 'string',
2178-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
2178+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
21792179
},
21802180
),
21812181
],
@@ -5860,14 +5860,14 @@ class Model(BaseModel):
58605860
{
58615861
('Decimal', 'serialization'): {
58625862
'type': 'string',
5863-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
5863+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
58645864
},
58655865
('Decimal', 'validation'): {
58665866
'anyOf': [
58675867
{'type': 'number'},
58685868
{
58695869
'type': 'string',
5870-
'pattern': '^(?!^[+-\\.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
5870+
'pattern': '^(?!^[-+.]*$)[+-]?0*(?:\\d{0,}$|(?=[\\d\\.]{1,}0*$)\\d{0,}\\.\\d{0,}0*$)',
58715871
},
58725872
]
58735873
},

0 commit comments

Comments
 (0)