-
-
Notifications
You must be signed in to change notification settings - Fork 937
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
Enable jsonschema.validate() to validate responses. #1231
Enable jsonschema.validate() to validate responses. #1231
Conversation
Pass a schema dict to jsonschema.validate() as 'schema_response' and the response data will be validated against it. A falcon.HTTPInternalServerError is raised on schema violations. This is useful to make sure a bug in your code doesn't cause your resource to return an out-of-spec response to users.
Codecov Report
@@ Coverage Diff @@
## master #1231 +/- ##
======================================
Coverage 100% 100%
======================================
Files 38 38
Lines 2484 2491 +7
Branches 364 366 +2
======================================
+ Hits 2484 2491 +7
Continue to review full report at Codecov.
|
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.
Thanks, this looks like a great feature! I can include a note in the changelog about that first kwarg potentially causing a breaking change, but TBH it's a very low risk and this will go into a major new release version, so I don't think it will be a problem.
|
||
assert err.value.description == '\'message\' is a required property' | ||
@skip_py26 |
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.
Once you rebase on master, you can remove these @skip_py26
decorators, as we have removed 2.6 support.
@@ -8,7 +8,7 @@ | |||
pass | |||
|
|||
|
|||
def validate(schema): | |||
def validate(schema_request=None, schema_response=None): |
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.
Nitpick: Would you mind swapping the word order in these arguments to follow the Modifier-Subject style, i.e.: request_schema
and response_schema
or req_schema
and resp_schema
?
I rebased and added a few additional patches on top, resubmitted as #1246 |
With this change, you can pass another schema dict to jsonschema.validate() as 'schema_response' to validate the response data against it.
A falcon.HTTPInternalServerError is raised on schema violations.
This is useful to make sure a bug in your code doesn't somehow accidentally cause your resource to return an out-of-spec response to users.
Let me know if you want me to change the name of the first parameter back. I think it's more specific as 'schema_request', but I understand it's possible some users may have explicitly keyworded it in their apps even though it was the only parameter.
I'm also open to splitting it into it's own 'validate_response' decorator.