Skip to content

Instantly share code, notes, and snippets.

@ivanleoncz
Last active December 5, 2020 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanleoncz/1dcfb03542e7536ae776fe726001b9d6 to your computer and use it in GitHub Desktop.
Save ivanleoncz/1dcfb03542e7536ae776fe726001b9d6 to your computer and use it in GitHub Desktop.
Demonstrating JSON Schema validation, using multiple examples.
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()
{
"hospital": "University of Mariland Medical Center",
"covid_tests": {
"amount": "300",
"positive": 23,
"from": "2020-09-01",
"to": "2020-09-31"
}
}
{
"hospital": "University of Texas MD Anderson Cancer Center",
"covid_tests": {
"amount": 564,
"positive": 68,
"from": "2020-09-01",
"to": "2020-09-31"
}
}
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))
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!")
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!")
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