Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Commit

Permalink
Improve jsonschema pregeneration (#18)
Browse files Browse the repository at this point in the history
* Add ability to provide list of classes to precompute jsonschema objects
* Optimize _generate_jsonschema method
  • Loading branch information
deepakunni3 authored Nov 18, 2022
1 parent a0c73aa commit 65ab0c5
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions linkml_validator/plugins/jsonschema_validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict
from typing import List, Dict
import jsonschema
import copy
from linkml.utils.generator import Generator
from linkml.generators.jsonschemagen import JsonSchemaGenerator
from linkml_validator.models import SeverityEnum, ValidationMessage, ValidationResult
Expand Down Expand Up @@ -29,25 +30,44 @@ def __init__(self, schema: str, jsonschema_generator: Generator = JsonSchemaGene
self.jsonschema_generator = jsonschema_generator
self.generator_args = generator_args if generator_args else {}
self.jsonschema_obj_map = {}
self._generate_jsonschema()
class_list = None
if 'class_list' in kwargs:
class_list = kwargs['class_list']
self._generate_jsonschema(class_list=class_list)

def _generate_jsonschema(self) -> None:
def _generate_jsonschema(self, class_list: List[str] = None) -> None:
"""
Generate JSON Schema representation for all classes in the schema.
Generate JSON Schema representation for all (or specific) classes
in the schema.
Args:
class_list: A list of classes for which to generate JSONSchema
"""
schemaview = SchemaView(self.schema)
jsonschema_obj = None
for class_name, class_def in schemaview.all_classes().items():
if not class_def.mixin:
formatted_name = camelcase(class_name)
if class_list:
if formatted_name not in class_list:
continue
if class_def.abstract:
# Skip abstract classes
continue
py_target_class = self.python_module.__dict__[formatted_name]
if formatted_name not in self.jsonschema_obj_map:
jsonschema_obj = get_jsonschema(
schema=self.schema,
py_target_class=py_target_class,
generator=self.jsonschema_generator,
**self.generator_args
)
self.jsonschema_obj_map[formatted_name] = jsonschema_obj
if not jsonschema_obj:
jsonschema_obj = get_jsonschema(
schema=self.schema,
py_target_class=py_target_class,
generator=self.jsonschema_generator,
**self.generator_args
)
target_jsonschema_obj = copy.deepcopy(jsonschema_obj)
target_jsonschema_obj['properties'] = jsonschema_obj["$defs"][formatted_name].get('properties', {})
target_jsonschema_obj['required'] = jsonschema_obj["$defs"][formatted_name].get('required', [])
self.jsonschema_obj_map[formatted_name] = target_jsonschema_obj

def process(self, obj: Dict, **kwargs) -> ValidationResult:
"""
Expand Down

0 comments on commit 65ab0c5

Please sign in to comment.