Skip to content

Commit

Permalink
Validate input_example and output_example in api_doc_gen; resolves #34
Browse files Browse the repository at this point in the history
  • Loading branch information
hfaran committed Feb 22, 2014
1 parent 59a5c02 commit fab17f0
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions tornado_json/api_doc_gen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import logging
import json
from jsonschema import validate, ValidationError


def _validate_example(rh, method, example_type):
"""Validates example against schema
:returns: Formatted example if example exists and validates, otherwise None
:raises ValidationError: If example does not validate against the schema
"""
if not rh.apid[method].get(example_type + "_example"):
return None
else:
try:
validate(rh.apid[method][example_type + "_example"], rh.apid[method][example_type + "_schema"])
except ValidationError as e:
raise ValidationError("{}_example for {}.{} could not be validated.\n{}".format(
example_type, rh.__name__, method, str(e)
))

return json.dumps(rh.apid[method][example_type + "_example"], indent=4)


def api_doc_gen(routes):
Expand Down Expand Up @@ -57,15 +77,13 @@ def api_doc_gen(routes):
```json
{}
```
""".format(json.dumps(rh.apid[method]["input_example"], indent=4))
if rh.apid[method].get("input_example") else "",
""".format(_validate_example(rh, method, "input")) if _validate_example(rh, method, "input") else "",
"""
**Output Example**
```json
{}
```
""".format(json.dumps(rh.apid[method]["output_example"], indent=4))
if rh.apid[method].get("output_example") else "",
""".format(_validate_example(rh, method, "output")) if _validate_example(rh, method, "output") else "",
) for method in list(rh.apid.keys())
]
)
Expand Down

0 comments on commit fab17f0

Please sign in to comment.