Last active
December 5, 2020 08:00
-
-
Save ivanleoncz/1dcfb03542e7536ae776fe726001b9d6 to your computer and use it in GitHub Desktop.
Demonstrating JSON Schema validation, using multiple examples.
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
from flask import Flask, jsonify, request | |
from jsonschema import Draft7Validator | |
app = Flask(__name__) | |
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = True | |
schema = { | |
"type": "object", | |
"properties": { | |
"hospital": {"type": "string"}, | |
"covid_tests": { | |
"type": "object", | |
"properties": { | |
"amount": {"type": "number"}, | |
"positive": {"type": "number"}, | |
"from": {"type": "string", "format": "date"}, | |
"to": {"type": "string", "format": "date"} | |
} | |
} | |
} | |
} | |
@app.route("/covid_data_validation", methods=["POST"]) | |
def f_index(): | |
validator = Draft7Validator(schema) | |
errors = {"errors": [e.message for e in validator.iter_errors(request.json)]} | |
if errors["errors"]: | |
return jsonify(errors), 400 | |
return "Data sucessfully validated!" | |
if __name__ == "__main__": | |
app.run() |
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
{ | |
"hospital": "University of Mariland Medical Center", | |
"covid_tests": { | |
"amount": "300", | |
"positive": 23, | |
"from": "2020-09-01", | |
"to": "2020-09-31" | |
} | |
} |
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
{ | |
"hospital": "University of Texas MD Anderson Cancer Center", | |
"covid_tests": { | |
"amount": 564, | |
"positive": 68, | |
"from": "2020-09-01", | |
"to": "2020-09-31" | |
} | |
} |
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
from jsonschema import Draft7Validator | |
schema = { | |
"type": "object", | |
"properties": { | |
"hospital": {"type": "string"}, | |
"covid_tests": { | |
"type": "object", | |
"properties": { | |
"amount": {"type": "number"}, | |
"positive": {"type": "number"}, | |
"from": {"type": "string", "format": "date"}, | |
"to": {"type": "string", "format": "date"} | |
} | |
} | |
} | |
} | |
data = { | |
"hospital": "University of Mariland Medical Center", | |
"covid_tests": { | |
"amount": "300", # field expected to be number, will make is_valid() return False | |
"positive": 23, | |
"from": "2020-09-01", | |
"to": "2020-09-31" | |
} | |
} | |
print(Draft7Validator(schema).is_valid(data)) |
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
from jsonschema import Draft7Validator | |
schema = { | |
"type": "object", | |
"properties": { | |
"hospital": {"type": "string"}, | |
"covid_tests": { | |
"type": "object", | |
"properties": { | |
"amount": {"type": "number"}, | |
"positive": {"type": "number"}, | |
"from": {"type": "string", "format": "date"}, | |
"to": {"type": "string", "format": "date"} | |
} | |
} | |
} | |
} | |
data = { | |
"hospital": "University of Mariland Medical Center", | |
"covid_tests": { | |
"amount": "300", # field expected to be number, will generate an error | |
"positive": 23, | |
"from": "2020-09-01", | |
"to": "2020-09-31" | |
} | |
} | |
validator = Draft7Validator(schema) | |
for error in validator.iter_errors(data): | |
print(f"Error: {error.message}") | |
print("Done!") |
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
from jsonschema import Draft7Validator | |
schema = { | |
"type": "object", | |
"properties": { | |
"hospital": {"type": "string"}, | |
"covid_tests": { | |
"type": "object", | |
"properties": { | |
"total_cases": {"type": "number"}, | |
"amount": {"type": "number"}, | |
"positive": {"type": "number"}, | |
"from": {"type": "string", "format": "date"}, | |
"to": {"type": "string", "format": "date"} | |
}, | |
"required": ["amount", "positive", "from", "to"] | |
} | |
} | |
} | |
data = { | |
"hospital": "University of Mariland Medical Center", | |
"covid_tests": { # amount field was removed intentionally | |
"total_cases": 1526, | |
"positive": 23, | |
"from": "2020-09-01", | |
"to": "2020-09-31" | |
} | |
} | |
validator = Draft7Validator(schema) | |
for error in validator.iter_errors(data): | |
print(f"Error: {error.message}") | |
print("Done!") |
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
from jsonschema import validate | |
schema = { | |
"type": "object", | |
"properties": { | |
"hospital": {"type": "string"}, | |
"covid_tests": { | |
"type": "object", | |
"properties": { | |
"amount": {"type": "number"}, | |
"positive": {"type": "number"}, | |
"from": {"type": "string", "format": "date"}, | |
"to": {"type": "string", "format": "date"} | |
} | |
} | |
} | |
} | |
data = { | |
"hospital": "University of Mariland Medical Center", | |
"covid_tests": { | |
"amount": "300", # field expected to be number, will throw Exception | |
"positive": 23, | |
"from": "2020-09-01", | |
"to": "2020-09-31" | |
} | |
} | |
print(validate(instance=data, schema=schema)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment