Factor out common code
This commit is contained in:
parent
1b374eafbc
commit
31687608dc
1 changed files with 21 additions and 36 deletions
|
@ -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):
|
|
||||||
if not path.startswith("file:///"):
|
|
||||||
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:///"):
|
|
||||||
raise Exception("Bad ref: %s" % (path,))
|
|
||||||
path = path[len("file://"):]
|
|
||||||
with open(path, "r") as f:
|
|
||||||
return json.load(f)
|
|
||||||
|
|
||||||
|
|
||||||
def load_file(path):
|
def load_file(path):
|
||||||
print("Loading reference: %s" % path)
|
print("Loading reference: %s" % path)
|
||||||
if path.endswith(".json"):
|
if not path.startswith("file:///"):
|
||||||
return load_json(path)
|
raise Exception("Bad ref: %s" % (path,))
|
||||||
else:
|
path = path[len("file://"):]
|
||||||
# We have to assume it's YAML because some of the YAML examples
|
with open(path, "r") as f:
|
||||||
# do not have file extensions.
|
if path.endswith(".json"):
|
||||||
return load_yaml(path)
|
return json.load(f)
|
||||||
|
else:
|
||||||
|
# We have to assume it's YAML because some of the YAML examples
|
||||||
|
# do not have file extensions.
|
||||||
|
return yaml.load(f)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue