New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(schema): set minItems
and maxItems
in JSON schema for tuples
#2497
Conversation
minItems
and maxItems
in JSON schema for tuples
Codecov Report
@@ Coverage Diff @@
## master #2497 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 25 25
Lines 5109 5113 +4
Branches 1050 1051 +1
=========================================
+ Hits 5109 5113 +4
Continue to review full report at Codecov.
|
), | ||
(Tuple[str], {'type': 'string'}), | ||
(Tuple[str], {'items': {'type': 'string'}, 'minItems': 1, 'maxItems': 1}), | ||
(Tuple[()], {'maxItems': 0, 'minItems': 0}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good but it will conflict with #2375 which I know @maximberg has been working on for a long time.
If someone can explain to my small brain what #2375 does, I think it would be fair to merge that first.
pydantic/schema.py
Outdated
|
||
f_schema = {'type': 'array', 'minItems': tuple_len, 'maxItems': tuple_len} | ||
if tuple_len == 1: | ||
f_schema['items'] = sub_schema[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks weird, are you sure it's correct as per JSONSchema.
Surely even if there's only one item, items
should be an array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both are valid but I agree it's easier to just do like this!
please update |
I'll wait for #2375 before updating this PR. If I should update it before please tell me |
9439e26
to
5f5b71c
Compare
please review 🙏 |
Thank you. |
…pydantic#2497) * feat(schema): enforce length in generated JSON schema for tuple type * docs: add change file * docs: update documentation * simplify a bit * always set array
Change Summary
We didn't enforce the length of the tuple in the JSON schema. We could hence set smaller or longer tuples.
Also handles case of empty tuple by not setting
items
at all to fix #2496Related issue number
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)