Package schemaops

initialize schemas and validator classes using jsonschema

Sub-modules

schemaops.templates

templates.py classes and methods for creating blank datastructures based on schemas

schemaops.validators

apply defaults supplied in schema when absent

Functions

def empty_entity()

get an empty object fit for inclusion in input->'entities'

def empty_job(date_of_service: str, **kwargs)

convenience method to instance and return asdict() for JobTemplate

def path_fragments(path: str) ‑> list[str]

get list of object keys represented by the path. splits at all instances of . that do NOT appear between double quotes.

Classes

class DefaultingValidatorGroup (parent_dir: str = 'c:/Users/samha/pdf-extractor/app', source_stem: str = '/schemaops/', base=jsonschema.validators.Draft202012Validator)

Loads and creates validators for all json schemas defined at the spcified path. The validators are defined such that missing required fields will be added and set to their default values at validation time. schema filenames must end with .model.json to be loaded automatically. additional schemas can be added via the add_schema function. Once loaded, arbitrary dicts can be validated by calling the instance. If a top level key in the dict matches a key in the loaded/added schema validators of the instance, its contents will be validated using that validator and associated schema. The 'level' parameter can also be used to check lower level keys in a series of nested dictionaries as shown in the example.

Example

from schemaops import DefaultingValidatorGroup validator = DefaultingValidatorGroup() validator.add_schema( version="validate_me", schema={ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "default": {}, "required": [ "inner_array" ], "properties": { "inner_array": { "type": "array", "default": [{}], "items": { "anyOf": [ { "type": "object", "default": {}, "required": [ "required1", "required2", ], "properties": { "required1": { "type": "string", "default": "I exist, hooray!" }, "required2": { "type": "string", "default": "I exist, hooray!" }, } } ] } } } }, )

test_dict = { "first0": { "second": { "third": { "validate_me": {}, } } }, "first1": { "second": { "third": { "validate_me": {}, } } }, }

test_dict_with_defaults = validator(test_dict, level=3, inplace=False)

Example Output (test_dict will remain unchanged): test_dict_with_defaults = { "first0": { "second": { "third": { "validate_me": { "inner_array": [ { "required1": "I exist, hooray!", "required2": "I exist, hooray!" } ] } } } }, "first1": { "second": { "third": { "validate_me": { "inner_array": [ { "required1": "I exist, hooray!", "required2": "I exist, hooray!" } ] } } } } }

Static methods

def path_fragments(path: str) ‑> list[str]

get list of object keys represented by the path. splits at all instances of . that do NOT appear between double quotes.

Instance variables

prop note_type_title_map : dict[str, str]

a mapping from document type (noteType) to displayed text (noteHeader) extracted from the input.model.json schema

Methods

def add_schema(self, schema: dict[str, typing.Any], version: str, overwrite: bool = True, strict: bool = True) ‑> jsonschema.validators.Draft202012Validator

add a schema to self._validators. 'version' is used to specify the 'version' field of the created validator as well as the top level key to search for in the dict being validated. if overwrite=False, a ValueError will be raised if 'version' is in self._validators.

def inst_for_obj_path(self, path: str, table: str = '') ‑> dict[str, typing.Any]

given a jmespath search pattern targeting a path of an instance object, return a default instance of a valid object appearing at that level of the instance

def subschema(self, version: str, path: str)

returns the result of a jmespath search for path against the target schema version

def update_schema_array_prefix_items(self, version: str, prefix_items: list[dict[str, typing.Any]], new_items: bool | dict[str, typing.Any] | None = None)

Add a 'prefixItems' array to the target schema version and update the 'items' key to the supplied 'new_items' value. If 'new_items' is None, leave the existing 'items' value in place to serve in the legacy 'additionalItems' capacity.

def update_schema_from_base(self, version: str, extend_all: dict[str, dict[str, str]], extend_for_title: dict[str, dict[str, dict[str, str]]], extend_with_format: dict[str, dict[str, str]])

build a new schema from -base for specfied version. props in extend_all are added to all entries. extend_for_title contains additional extensions to be applied if base entry title matches top level key. extend_for_props applies new subkeys to specific props.

def update_schema_object_by_key(self, version: str, key_reference: list[dict[str, typing.Any]] | dict[str, dict[str, typing.Any]], target_elements: collections.abc.Sequence[str] = ('properties',))

inspect the object contained in the target_element of the specified schema version and merge the object elements from any matching keys in key_reference.