Add decimal precision constraints to JSON schema#11016
Add decimal precision constraints to JSON schema#11016KCui0327 wants to merge 4 commits intopydantic:mainfrom
Conversation
CodSpeed Performance ReportMerging #11016 will not alter performanceComparing Summary
|
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
|
please review (thanks!) |
There was a problem hiding this comment.
Thanks for the PR. A couple remarks on the pattern implementation:
- Can't we also set a pattern if only
decimal_placesis set? - I'm wondering if we should special case leading zeros differently. What we could do is:
- in validation, allow arbitrary leading zeros to be specified. Pydantic will not error during validation.
- in serialization, only allow a single 0 or
[1-9]followed by the constrained number of integers.
- Taking into account what was said above, let's define the pattern as a private constant in this module, and explain it with comments (and then compile with the
re.VERBOSEflag), e.g:_DECIMAL_PATTERN = r""" ^-? # Possible minus sign ... """
Hi @Viicos, thank you for your feedback! I have address your feedback in the latest commits, please let me know if there's any other changes I should add. |
Viicos
left a comment
There was a problem hiding this comment.
Thanks. I think this does not yet handle all cases:
- in validation, you can either have:
decimal_placesset,max_digitsnot set. In that case, your current_DECIMAL_JSON_VALIDATION_PATTERNhandles this.decimal_placesset andmax_digitsset. In that case, we need a new regex.decimal_placesnot set andmax_digitsset. In that case, the regex will be limited but you can make use of a positive lookahead, e.g."^(?=.{0,n+1}$){decimal_regex}$", withnbeingmax_digits.
- in serialization, similar logic applies.
| ^ | ||
| -? # Minus sign (optional) | ||
| \d+ # One or more digits for the integer part (allows leading zeros) | ||
| ( # Optional decimal group |
There was a problem hiding this comment.
Can we make it non capturing? (same for the other regex). Or is there a reason not to do so?
| @@ -935,11 +935,41 @@ class Model(BaseModel): | |||
|
|
|||
| def test_decimal_constraints(): | |||
There was a problem hiding this comment.
This test no longer test anything related to JSON Schema. I believe we already have plenty of such tests in test_types.py
|
Hey @KCui0327, Are you still interested in working on this? Thanks for your contributions thus far! |
Hi @sydney-runkle , Apologies for the late response. I will not be able to actively contribute to this PR for the next few months so I want to open it to anyone who wants to take this PR further and address the comments from Viccos. |
|
at pydantify we are looking forward to have this merged as well. Thanks for all the work on this PR |
|
Hi @sydney-runkle and @Viicos I can have a look at finishing this off. I should be able to submit an updated PR later this week. I was reviewing the current PR and noticed the use of a verbose regex pattern makes its way through to the json_schema() output, and it doesn't look like a re.Pattern object provides an easy way to extract the regex without the comments and extra white space.
|
|
Thanks for looking into it. My bad, I thought compiling the verbose pattern and using the Instead, lets use a simple multi-line string and add comments this way: _DECIMAL_JSON_SERIALIZATION_PATTERN = (
r"^-?" # Minus sign (optional)
r"(?:0|[1-9]\d{{0,{integer_places}}})" # Integer part: single zero OR single non-zero digit...
) |
|
Looks like this will be finished up by #11420, thanks for the initial work on this PR! |
Change Summary
This PR added a decimal precision pattern validation to JSON schema generation. The implementation enforces
max_digitsanddecimal_placesconstraints by adding a regex pattern. A corresponding unit test was also added to test the changes.Related issue number
fix #10867
Checklist
Selected Reviewer: @sydney-runkle