Skip to content
Snippets Groups Projects
Commit 69125073 authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-860: check if json_doc and schema have matching and

parent c278f434
No related branches found
No related tags found
1 merge request!828TMSS-860
......@@ -17,6 +17,7 @@
import json
import time
import typing
import jsonschema
from copy import deepcopy
......@@ -429,35 +430,35 @@ def validate_json_against_its_schema(json_object: dict, cache: dict=None, max_ca
return validate_json_against_schema(json_object, referenced_schema, cache=cache, max_cache_age=max_cache_age, add_defaults=add_defaults)
def validate_json_against_schema(json_string: str, schema: str, cache: dict=None, max_cache_age: timedelta=DEFAULT_MAX_SCHEMA_CACHE_AGE, add_defaults: bool=False) -> dict:
def validate_json_against_schema(json_doc: typing.Union[str,dict], schema: typing.Union[str,dict], cache: dict=None, max_cache_age: timedelta=DEFAULT_MAX_SCHEMA_CACHE_AGE, add_defaults: bool=False) -> dict:
'''validate the given json_string against the given schema.
If no exception if thrown, then the given json_string validates against the given schema.
:raises SchemaValidationException if the json_string does not validate against the schema
'''
# ensure the given arguments are strings
if type(json_string) != str:
json_string = json.dumps(json_string)
if type(schema) != str:
schema = json.dumps(schema)
# ensure the specification and schema are both valid json in the first place
try:
json_object = json.loads(json_string)
if isinstance(json_doc, str):
json_doc = json.loads(json_doc)
except json.decoder.JSONDecodeError as e:
raise jsonschema.exceptions.ValidationError("Invalid JSON: %s\n%s" % (str(e), json_string))
raise jsonschema.exceptions.ValidationError("Invalid JSON: %s\n%s" % (str(e), json_doc))
try:
schema_object = json.loads(schema)
if isinstance(schema, str):
schema = json.loads(schema)
except json.decoder.JSONDecodeError as e:
raise jsonschema.exceptions.ValidationError("Invalid JSON: %s\n%s" % (str(e), schema))
if '$schema' not in json_doc:
raise jsonschema.exceptions.ValidationError("The json document does not contain a $schema property\n%s" % (schema,))
if json_doc['$schema'].rstrip('/ref_resolved').rstrip('#').rstrip('/') != schema.get('$id','').rstrip('#').rstrip('/'):
raise jsonschema.exceptions.ValidationError("The json document with $schema='%s' cannot be validated by schema with $id='%s'\n%s" % (json_doc['$schema'], schema.get('$id')))
# resolve $refs to fill in defaults for those, too
schema_object = resolved_remote_refs(schema_object, cache=cache, max_cache_age=max_cache_age)
schema = resolved_remote_refs(schema, cache=cache, max_cache_age=max_cache_age)
# now do the actual validation
try:
return validate_json_object_with_schema(json_object, schema_object, add_defaults=add_defaults)
return validate_json_object_with_schema(json_doc, schema, add_defaults=add_defaults)
except jsonschema.ValidationError as e:
raise jsonschema.exceptions.ValidationError(str(e))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment