-
Notifications
You must be signed in to change notification settings - Fork 11
Add support for strict validation #52
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lietu
reviewed
Sep 12, 2025
lietu
reviewed
Sep 12, 2025
lietu
reviewed
Sep 12, 2025
lietu
approved these changes
Sep 12, 2025
Contributor
lietu
left a comment
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.
Added a few suggestions that might make code or messages clearer
Co-authored-by: Janne Enberg <janne.enberg@lietu.net>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for optional stricter validation in the generated routes.
If the OpenAPI Spec (
.json) files on the root level contain the keyx-strict-validationand it's set totruethe library will use a stricter mode of validation for the models generated from the file. If the key is not present or set tofalse, the library will default to old, laxer mode.In short this stricter mode consists of:
str,bytes,int,float,boolIt's worth noting that the underlying datamodel-code-generator in version 0.33.0 reverted the behaviour of how datetimes are handled. Based on koxudaxi/datamodel-code-generator#2441 it seems like datamodel-code-generator versions 0.26.2 - 0.32.0 accidentally defaulted to using
datetimefor datetime fields, whereas versions before that and after that default toAwareDatetime. In essence this means that the versions 0.26.2 - 0.32.0 had a laxer validation of datetimes than versions up until 0.26.1 and again starting from 0.33.0. This means also openapi-to-fastapi has had this laxer validation in versions starting from 0.18.0 ->. This PR updates datamode-code-generator to latest 0.33.0 version, but contains such changes that by default the validation will be as lax as in the previous version by defaulting to using thedatetimeclass for those fields. Thus by default the datetime validation will work the same way after this update to openapi-to-fastapi as before it despite datamodel-code-generator being updated to the latest version.The strict mode provided by this PR however takes things one step further. The
AwareDatetimeis somewhat lax; it allows you to send dates in multiple formats that are valid ISO 8601 formats, but aren't following the strict set of RFC 3339, as well as unix timestamps (seconds since 1970). It thus defines it's ownStrictAwareDatetime, which checks the type and format. In essence it will check the separator is a capitalTand that there is either a capitalZor+or-with aHH:MM.The strict mode also ensures that date fields can neither be unix timestamps, rather must be of the YYYY-MM-DD format.