Factor out common code

pull/977/head
Kitsune Ral 6 years ago
parent 1b374eafbc
commit 31687608dc

@ -43,22 +43,25 @@ except ImportError as e:
raise raise
def check_schema(filepath, example, schema):
# Setting the 'id' tells jsonschema where the file is so that it
# can correctly resolve relative $ref references in the schema
schema['id'] = "file://" + os.path.abspath(filepath)
example = resolve_references(filepath, example)
resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_file})
jsonschema.validate(example, schema, resolver=resolver)
def check_parameter(filepath, request, parameter): def check_parameter(filepath, request, parameter):
schema = parameter.get("schema") schema = parameter.get("schema")
example = schema.get('example') example = schema.get('example')
fileurl = "file://" + os.path.abspath(filepath)
if example and schema: if example and schema:
try: try:
print ("Checking request schema for: %r %r" % ( print ("Checking request schema for: %r %r" % (
filepath, request filepath, request
)) ))
# Setting the 'id' tells jsonschema where the file is so that it check_schema(filepath, example, schema)
# can correctly resolve relative $ref references in the schema
schema['id'] = fileurl
example = resolve_references(filepath, example)
resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_file})
jsonschema.validate(example, schema, resolver=resolver)
except Exception as e: except Exception as e:
raise ValueError("Error validating JSON schema for %r" % ( raise ValueError("Error validating JSON schema for %r" % (
request request
@ -68,18 +71,12 @@ def check_parameter(filepath, request, parameter):
def check_response(filepath, request, code, response): def check_response(filepath, request, code, response):
example = response.get('examples', {}).get('application/json') example = response.get('examples', {}).get('application/json')
schema = response.get('schema') schema = response.get('schema')
fileurl = "file://" + os.path.abspath(filepath)
if example and schema: if example and schema:
try: try:
print ("Checking response schema for: %r %r %r" % ( print ("Checking response schema for: %r %r %r" % (
filepath, request, code filepath, request, code
)) ))
# Setting the 'id' tells jsonschema where the file is so that it check_schema(filepath, example, schema)
# can correctly resolve relative $ref references in the schema
schema['id'] = fileurl
example = resolve_references(filepath, example)
resolver = jsonschema.RefResolver(filepath, schema, handlers={"file": load_file})
jsonschema.validate(example, schema, resolver=resolver)
except Exception as e: except Exception as e:
raise ValueError("Error validating JSON schema for %r %r" % ( raise ValueError("Error validating JSON schema for %r %r" % (
request, code request, code
@ -127,30 +124,18 @@ def resolve_references(path, schema):
return schema return schema
def load_yaml(path): def load_file(path):
if not path.startswith("file:///"): print("Loading reference: %s" % path)
raise Exception("Bad ref: %s" % (path,))
path = path[len("file://"):]
with open(path, "r") as f:
return yaml.load(f)
def load_json(path):
if not path.startswith("file:///"): if not path.startswith("file:///"):
raise Exception("Bad ref: %s" % (path,)) raise Exception("Bad ref: %s" % (path,))
path = path[len("file://"):] path = path[len("file://"):]
with open(path, "r") as f: with open(path, "r") as f:
return json.load(f) if path.endswith(".json"):
return json.load(f)
else:
def load_file(path): # We have to assume it's YAML because some of the YAML examples
print("Loading reference: %s" % path) # do not have file extensions.
if path.endswith(".json"): return yaml.load(f)
return load_json(path)
else:
# We have to assume it's YAML because some of the YAML examples
# do not have file extensions.
return load_yaml(path)
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save