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

TMSS-860: check if doc. equals schema. when validating

parent a5d2c68d
No related branches found
No related tags found
1 merge request!828TMSS-860
...@@ -488,6 +488,12 @@ def validate_json_object_with_schema(json_object, schema, add_defaults: bool=Fal ...@@ -488,6 +488,12 @@ def validate_json_object_with_schema(json_object, schema, add_defaults: bool=Fal
""" """
Validate the given json_object with schema Validate the given json_object with schema
""" """
if '$schema' in json_object and '$id' in schema:
object_schema_id = json_object['$schema'].partition('#')[0].rstrip('/').rstrip('ref_resolved').rstrip('/')
schema_id = schema['$id'].partition('#')[0].rstrip('/').rstrip('ref_resolved').rstrip('/')
if object_schema_id != schema_id:
raise JSONError("json_object refers to different $schema='%s' and cannot be compared to the schema with $id='%s'" % (object_schema_id, schema_id))
get_validator_for_schema(schema, add_defaults=add_defaults).validate(json_object) get_validator_for_schema(schema, add_defaults=add_defaults).validate(json_object)
return json_object return json_object
......
...@@ -260,10 +260,10 @@ class AbstractSchemaTemplate(AbstractTemplate): ...@@ -260,10 +260,10 @@ class AbstractSchemaTemplate(AbstractTemplate):
# this template's schema has a schema of its own (usually the draft-06 meta schema). Validate it. # this template's schema has a schema of its own (usually the draft-06 meta schema). Validate it.
validate_json_against_its_schema(self.schema, cache=TemplateSchemaMixin._schema_cache, max_cache_age=TemplateSchemaMixin._MAX_SCHEMA_CACHE_AGE) validate_json_against_its_schema(self.schema, cache=TemplateSchemaMixin._schema_cache, max_cache_age=TemplateSchemaMixin._MAX_SCHEMA_CACHE_AGE)
def validate_document(self, json_doc: typing.Union[str, dict]) -> bool: def validate_document_against_schema(self, json_doc: typing.Union[str, dict]):
'''validate the given json_doc against the template's schema '''validate the given json_doc against this template's schema
If no exception if thrown, then the given json_string validates against the given schema. If no exception if thrown, then the given json_doc validates.
:raises SchemaValidationException if the json_string does not validate against the schema ''' :raises SchemaValidationException if the json_doc does not validate against this template's schema'''
validate_json_against_schema(json_doc, self.schema, cache=TemplateSchemaMixin._schema_cache, max_cache_age=TemplateSchemaMixin._MAX_SCHEMA_CACHE_AGE) validate_json_against_schema(json_doc, self.schema, cache=TemplateSchemaMixin._schema_cache, max_cache_age=TemplateSchemaMixin._MAX_SCHEMA_CACHE_AGE)
@property @property
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment