From 34262e98643c2a47555fc964a2025ae6b5d1c5c9 Mon Sep 17 00:00:00 2001 From: Artem Batalov Date: Wed, 24 Jan 2024 10:40:39 +0300 Subject: [PATCH 1/2] Add handler for includes in JSON-schema --- .../folder_filename_key_repository.py | 4 ++-- .../message/validators/json_schema_validator.py | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/core/repositories/folder_filename_key_repository.py b/core/repositories/folder_filename_key_repository.py index ff492382..3cedda11 100644 --- a/core/repositories/folder_filename_key_repository.py +++ b/core/repositories/folder_filename_key_repository.py @@ -8,9 +8,9 @@ def _form_file_upload_map(self, shard_desc): filename_to_data = {} for shard in shard_desc: shard_data = self._load_item(shard) - key = os.path.basename(shard).split(".")[0] if shard_data: - filename_to_data[key] = shard_data + filename_to_data[os.path.relpath(shard, self.path)] = shard_data + filename_to_data[os.path.relpath(shard, self.path).split(".")[0]] = shard_data return filename_to_data def fill(self, data): diff --git a/smart_kit/message/validators/json_schema_validator.py b/smart_kit/message/validators/json_schema_validator.py index 2f06cb65..8af5656b 100644 --- a/smart_kit/message/validators/json_schema_validator.py +++ b/smart_kit/message/validators/json_schema_validator.py @@ -17,15 +17,19 @@ class JSONSchemaValidator(BaseMessageValidatorWithResources): def _update_resources(self): schemas = self.resources.get("payload_schema") - self._schemas = {k: self._compile_schema(v) for k, v in schemas.items()} + self._schemas = {k: self._compile_schema(v) for k, v in schemas.items() if '.json' not in k and "/" not in k} - @staticmethod - def _compile_schema(schema: dict) -> Callable[[dict], None]: + def _compile_schema(self, schema: dict) -> Callable[[dict], None]: cls = jsonschema.validators.validator_for(schema) cls.check_schema(schema) instance = cls(schema) + instance.resolver.handlers[""] = self._handle_includes return instance.validate + def _handle_includes(self, path): + schemas = self.resources.get("payload_schema") + return schemas[path] + def _log(self, exception: VALIDATOR_EXCEPTION, message: SmartAppMessage, level="WARNING"): log_params = self._log_params(message) log_params["json_path"] = exception.json_path @@ -41,6 +45,5 @@ def _validate(self, message: SmartAppFromMessage): class FastJSONSchemaValidator(JSONSchemaValidator): VALIDATOR_EXCEPTION = fastjsonschema.JsonSchemaValueException - @staticmethod - def _compile_schema(schema: dict) -> Callable[[dict], None]: - return fastjsonschema.compile(schema) + def _compile_schema(self, schema: dict) -> Callable[[dict], None]: + return fastjsonschema.compile(schema, handlers={"": self._handle_includes}) From 61617509a23539890b49e0617c7fe42ac5cc4d81 Mon Sep 17 00:00:00 2001 From: Artem Batalov Date: Thu, 8 Feb 2024 18:15:38 +0300 Subject: [PATCH 2/2] Fix tests --- .../message_test/test_required_fields_validators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/core_tests/message_test/test_required_fields_validators.py b/tests/core_tests/message_test/test_required_fields_validators.py index 5c61920c..8e73fe50 100644 --- a/tests/core_tests/message_test/test_required_fields_validators.py +++ b/tests/core_tests/message_test/test_required_fields_validators.py @@ -43,7 +43,7 @@ def test_valid_types_true(self): "messageName": "OOO", "1": 123, "2": "str" - }, headers=[('test_header', b'result')], validators=[validator_types]) + }, headers=[('test_header', b'result')], validators=[types_validator]) self.assertTrue(message.validate()) def test_valid_false(self): @@ -66,5 +66,5 @@ def test_valid_types_false(self): "messageName": "OOO", "1": {}, "2": [] - }, headers=[('test_header', b'result')], validators=[validator_types]) + }, headers=[('test_header', b'result')], validators=[types_validator]) self.assertFalse(message.validate())