-
-
Notifications
You must be signed in to change notification settings - Fork 378
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
Validate JSON after decode #623
Validate JSON after decode #623
Conversation
Closes #621 Custom JSON validations are currently run before the value stored in the database is converted to the Python type. This creates an asymmetry in the validation function as it runs against the Python type on create/update, but against the raw JSON string on fetch. This changes the order of operations so the JSON is decoded before validation is run, along with a test for each of the three supported input types for the field.
id = fields.IntField(pk=True) | ||
data = fields.JSONField() | ||
data_null = fields.JSONField(null=True) | ||
data_default = fields.JSONField(default={"a": 1}) | ||
data_validate = fields.JSONField(null=True, validators=[lambda v: JSONFields.dict_or_list(v)]) |
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.
Putting the field on this model means the 'describe' tests need to be updated, but it feels more appropriate than putting it on the validation model as this is testing the JSONField
itself, not packaged validations. I've used a static method on the object rather than a module method as the validator to keep things self-contained, but happy to change if you find it too clumsy.
Looks good, could you update changelog and version? After CI pass, I will merge it. |
Pull Request Test Coverage Report for Build 509686588
💛 - Coveralls |
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.
Good!
Validates JSON retrieved from the database after decode rather than before.
Description
Changes the order of operations so JSON is decoded before validation is run, along with a test for each of the three supported input types for the field.
Motivation and Context
Custom JSON validations are currently run before the value stored in the database is converted to the Python type. This creates an asymmetry in the validation function as it runs against the Python type on create/update, but against the raw JSON string on fetch.
Closes #621
How Has This Been Tested?
All unit tests run.
Checklist: