From 18f69d5fa64602426b90919bde06b631b5bef09d Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Fri, 2 Jul 2021 14:14:48 +0200
Subject: [PATCH] TMSS-745: added convenience method to validate any document
 against a template's schema

---
 SAS/TMSS/backend/src/tmss/tmssapp/models/common.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/models/common.py b/SAS/TMSS/backend/src/tmss/tmssapp/models/common.py
index ab1e8fc20ca..4f95a96b250 100644
--- a/SAS/TMSS/backend/src/tmss/tmssapp/models/common.py
+++ b/SAS/TMSS/backend/src/tmss/tmssapp/models/common.py
@@ -3,6 +3,8 @@ This file contains common constructs used by database models in other modules
 """
 
 import logging
+import typing
+
 logger = logging.getLogger(__name__)
 
 from django.db.models import Model, CharField, DateTimeField, IntegerField, UniqueConstraint
@@ -167,6 +169,13 @@ class Template(NamedCommon):
         # 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)
 
+    def validate_document(self, json_doc: typing.Union[str, dict]) -> bool:
+        '''validate the given json_doc against the template's 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 '''
+        validate_json_against_schema(json_doc, self.schema, cache=TemplateSchemaMixin._schema_cache, max_cache_age=TemplateSchemaMixin._MAX_SCHEMA_CACHE_AGE)
+
+
     @property
     def is_used(self) -> bool:
         '''Is this template used by any of its related objects?'''
-- 
GitLab