Commit b543a461 authored by J. Fernando Sánchez's avatar J. Fernando Sánchez
Browse files

Improved schema validation

* Added debug Dockerfile/Makefile
* Validation of examples in docs
parent bc1f9e4c
from __future__ import print_function
import json
import unittest
import os
from os import path
from fnmatch import fnmatch
import pyld
from jsonschema import validate, RefResolver, Draft4Validator, ValidationError
root_path = path.join(path.dirname(path.realpath(__file__)), '..')
schema_folder = path.join(root_path, 'senpy', 'schemas')
examples_path = path.join(root_path, 'docs', 'examples')
bad_examples_path = path.join(root_path, 'docs', 'bad-examples')
class JSONSchemaTests(unittest.TestCase):
def do_create_(jsfile, success):
def do_expected(self):
with open(jsfile) as f:
js = json.load(f)
assert '@type' in js
schema_name = js['@type']
with open(os.path.join(schema_folder, schema_name+".json")) as file_object:
schema = json.load(file_object)
resolver = RefResolver('file://' + schema_folder + '/', schema)
validator = Draft4Validator(schema, resolver=resolver)
except (AssertionError, ValidationError, KeyError) as ex:
if success:
return do_expected
def add_examples(dirname, success):
for dirpath, dirnames, filenames in os.walk(dirname):
for i in filenames:
if fnmatch(i, '*.json'):
filename = path.join(dirpath, i)
test_method = do_create_(filename, success)
test_method.__name__ = 'test_file_%s_success_%s' % (filename, success)
test_method.__doc__ = '%s should %svalidate' % (filename, '' if success else 'not' )
setattr(JSONSchemaTests, test_method.__name__, test_method)
del test_method
add_examples(examples_path, True)
add_examples(bad_examples_path, False)
if __name__ == '__main__':
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment