From 744c1d6481ffa2a5460b928b81db53f7b8581e56 Mon Sep 17 00:00:00 2001 From: Evgeny Blokhin Date: Mon, 13 Jan 2025 17:24:58 +0100 Subject: [PATCH] Add validator script --- scripts/validate.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 scripts/validate.py diff --git a/scripts/validate.py b/scripts/validate.py new file mode 100644 index 0000000..62bb2b9 --- /dev/null +++ b/scripts/validate.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +import sys +import json +import httplib2 +from jsonschema import validate, Draft4Validator +from jsonschema.exceptions import ValidationError + +try: input_json = sys.argv[1] +except IndexError: sys.exit("Usage: %s input_json" % sys.argv[0]) + +network = httplib2.Http() +response, content = network.request("http://developer.mpds.io/mpds.schema.json") +assert response.status == 200 + +schema = json.loads(content) +Draft4Validator.check_schema(schema) + +target = json.loads(open(input_json).read()) + +if not target.get("npages") or not target.get("out") or target.get("error"): + sys.exit("Unexpected API response") + +try: + validate(target["out"], schema) +except ValidationError, e: + raise RuntimeError( + "The item: \r\n\r\n %s \r\n\r\n has an issue: \r\n\r\n %s" % ( + e.instance, e.context + ) + )