Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
first real commit
- Loading branch information
1 parent
bdf00fd
commit b7d91b9
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# `flask_restful_jsonschema` | ||
|
||
Add the `@validate_json` decorator and schema class constant to | ||
`flask_restful.Resource` methods (post, get, etc.) in order to validate | ||
requests meet the `jsonschema`. | ||
|
||
|
||
```python | ||
from flask_restful_jsonschema import validate_json | ||
|
||
|
||
class Users(flask_restful.Resource): | ||
SCHEMA_POST = { | ||
"type": "object", | ||
"properties": { | ||
"email": {"type": "string"}, | ||
"password": {"type": "string"}, | ||
}, | ||
"required": ["email", "password"], | ||
} | ||
SCHEMA_PUT = { | ||
"type": "object", | ||
"properties": { | ||
"email": {"type": "string"}, | ||
"password": {"type": "string"}, | ||
}, | ||
} | ||
SCHEMA_GET = { | ||
"type": "object", | ||
"properties": { | ||
"email": {"type": "string"}, | ||
}, | ||
"required": ["email"], | ||
} | ||
|
||
@validate_json | ||
def post(self, json_request): | ||
pass | ||
|
||
@validate_json | ||
def put(self, json_request): | ||
pass | ||
|
||
@validate_json | ||
def get(self, json_request): | ||
pass | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import jsonschema | ||
import flask | ||
import flask_restful | ||
|
||
|
||
def get_valid_json_or_abort(schema): | ||
"""Either return the request's JSON, which | ||
matches the supplied JSON `schema`. | ||
Arguments: | ||
schema (dict): See jsonschema Python package. | ||
Aborts: | ||
400: The request does not adhere to the schema. | ||
Returns: | ||
JSON: JSON that fits the supplied `schema`. | ||
""" | ||
|
||
json_request = flask.request.get_json(force=True) | ||
|
||
try: | ||
jsonschema.validate(json_request, schema) | ||
except jsonschema.ValidationError as e: | ||
flask_restful.abort(400, message=e.message) | ||
else: | ||
return json_request | ||
|
||
|
||
def validate_json(func): | ||
"""Decorator specifically for wrapping flask_restful | ||
Resource method... methods (get/post/put, etc.). | ||
""" | ||
|
||
# get the function name because we're going to | ||
# see if we need SCHEMA_POST, SCHEMA_GET, etc. | ||
function_name = func.__name__.upper() | ||
|
||
def wrapped_func(self, *args, **kwargs): | ||
respective_schema = getattr(self, 'SCHEMA_' + function_name) | ||
json_request = get_valid_json_or_abort(respective_schema) | ||
return func(self, json_request, *args, **kwargs) | ||
|
||
return wrapped_func |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
jsonschema | ||
flask | ||
flask_restful |