Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPNLPF-2213: Includes in JSON-schema #182

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/repositories/folder_filename_key_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
15 changes: 9 additions & 6 deletions smart_kit/message/validators/json_schema_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

подумать как сделать умнее


@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
Expand All @@ -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})
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())
Loading