TRAPI Schema Validation

TRAPI Validation Functions.

exception reasoner_validator.trapi.TRAPIAccessError

Bases: RuntimeError

class reasoner_validator.trapi.TRAPISchemaValidator(default_test: str | None = None, default_target: str | None = None, trapi_version: str | None = None, strict_validation: bool | None = None)

Bases: ValidationReporter

TRAPI Validator is a wrapper class for validating conformance of JSON messages to the Translator Reasoner API.

Methods

add_messages(new_messages)

Batch addition of MESSAGES_BY_TARGET messages to a ValidationReporter instance. :param new_messages: MESSAGES_BY_TARGET, messages indexed by target, test and categories: one of "information", "skipped tests", "warnings", "errors" or "critical", with code-keyed dictionaries of (structured) message parameters.

apply_validation(validation_method, *args, ...)

Wrapper to allow validation_methods direct access to the ValidationReporter.

dump([title, id_rows, msg_rows, ...])

Dump all available messages captured by the ValidationReporter, printed as formatted human-readable text, on a specified file device.

dump_all_messages([test, target, flat])

Dump all messages for a given test from a given target, as JSON.

dump_critical([test, target, flat])

Dump critical error messages as JSON.

dump_errors([test, target, flat])

Dump 'error' messages as JSON.

dump_info([test, target, flat])

Dump 'information' messages as JSON.

dump_messages_type(message_type[, test, ...])

Dump ValidationReporter messages of type 'message_type' as JSON.

dump_skipped([test, target, flat])

Dump 'skipped test' messages as JSON.

dump_warnings([test, target, flat])

Dump 'warning' messages as JSON.

dumps([id_rows, msg_rows, compact_format])

Text string version of dump(): returns all available messages captured by the ValidationReporter, as a formatted human-readable text blob.

get_all_messages()

Get copy of all MESSAGES_BY_TARGET as a Python data structure.

get_all_messages_of_type(message_type)

Get MESSAGE_PARTITION dictionary of all ValidationReporter messages of a given 'message_type', harvested from all target and test contexts.

get_critical([test, target])

Get copy of all recorded 'critical' error messages, for a given test from a given target.

get_default_target()

Returns the current target of the ValidationReporter.

get_default_test()

Returns the current default test identifier of the ValidationReporter.

get_errors([test, target])

Get copy of all recorded 'error' messages, for a given test from a given target.

get_info([test, target])

Get copy of all recorded 'information' messages, for a given test from a given target.

get_message_type(code)

Get type of message code.

get_messages_by_target([target])

Returns a block of MESSAGES_BY_TEST corresponding to a given or default target.

get_messages_by_test([test, target])

Returns MESSAGE_CATALOG corresponding to a given or default target. Note that the dictionary returned is not a copy of the original thus caution should be taken not to mutate it! :param test: str, specified test (gets current 'default' test if not given) :param target: str, specified target (gets current 'default' test if not given) :return: MESSAGES_BY_TEST corresponding to a resolved target.

get_messages_of_type(message_type[, test, ...])

Get Python data dictionary of ValidationReporter messages of 'message_type', for a specified (or default?) target and test.

get_skipped([test, target])

Get copy of all recorded 'skipped test' messages, for a given test from a given target.

get_trapi_version()

return:

str, TRAPI (SemVer) version currently targeted by the TRAPISchemaValidator.

get_warnings([test, target])

Get copy of all recorded 'warning' messages, for a given test from a given target.

has_critical([test, target])

Predicate to detect any recorded critical error messages.

has_errors([test, target])

Predicate to detect any recorded error messages.

has_information([test, target])

Predicate to detect any recorded information messages.

has_message_type(message_type[, test, target])

Predicate to detect if ValidationReporter has any non-empty messages of type 'message_type'.

has_messages([test, target])

Predicate to detect any recorded validation messages.

has_skipped([test, target])

Predicate to detect any recorded 'skipped test' messages.

has_warnings([test, target])

Predicate to detect any recorded warning messages.

is_strict_validation(graph_type)

Predicate to test if strict validation is to be applied.

is_valid_trapi_query(instance[, component])

Make sure that the Message is a syntactically valid TRAPI Query JSON object.

merge(reporter)

Merge all messages and metadata from a second TRAPISchemaValidator, into the calling TRAPISchemaValidator instance.

merge_coded_messages(aggregated, additions)

Merge additional MESSAGE_PARTITION content into an already aggregate MESSAGE_PARTITION.

minimum_required_trapi_version(version)

param version:

simple 'major.minor.patch' TRAPI schema release SemVer

report(code[, test, target, source_trail])

Capture a single validation message, as per specified 'code' (with any code-specific contextual parameters). :param code: str, dot delimited validation path code :param test: str, specified test (gets current 'default' test if not given) :param target: str, specified target (gets current 'default' test if not given) :param source_trail, Optional[str], audit trail of knowledge source provenance for a given Edge, as a string. Defaults to "global" if not specified. :param message: **Dict, named parameters representing extra (str-formatted) context for the given code message :return: None (internally record the validation message).

report_header([title, compact_format])

Return a suitably generated report header. :param title: Optional[str], if title is None, then only the 'reasoner-validator' version is printed out in the header. If the title is an empty string (the default), then 'Validation Report' used. :param compact_format: bool, whether to print the header in compact format (default: True). Extra line feeds are otherwise provided to provide space around the header and control characters are output to underline the header. :return: str, generated header.

reset_default_target(name)

Resets the default target identifier of the ValidationReporter to a new string.

reset_default_test(name)

Resets the default test identifier of the ValidationReporter to a new string.

reset_trapi_version(version)

Reset TRAPI version tracked by the TRAPISchemaValidator.

test_case_has_validation_errors(tag, case)

Check if test case has validation errors.

to_dict()

Export TRAPISchemaValidator contents as a Python dictionary (including TRAPI version and parent class dictionary content) :return: Dict

validate(instance, component)

Validate instance against schema.

merge_identified_messages

merge_scoped_messages

default_target: str
default_test: str
get_trapi_version() str
Returns:

str, TRAPI (SemVer) version currently targeted by the TRAPISchemaValidator.

is_valid_trapi_query(instance, component: str = 'Query')

Make sure that the Message is a syntactically valid TRAPI Query JSON object.

Parameters:
instance:

Dict, instance to validate

component:

str, TRAPI subschema to validate (e.g. ‘Query’, ‘QueryGraph’, ‘KnowledgeGraph’, ‘Result’; Default: ‘Query’)

Returns:
Validation (“information”, “warning” and “error”) messages are returned within the host TRAPIValidator instance.

Examples

>>> TRAPISchemaValidator(trapi_version="1.3.0").is_valid_trapi_query({"message": {}}, component="Query")
merge(reporter)

Merge all messages and metadata from a second TRAPISchemaValidator, into the calling TRAPISchemaValidator instance.

Parameters:

reporter – second TRAPISchemaValidator

messages: MESSAGES_BY_TARGET
minimum_required_trapi_version(version: str) bool
Parameters:

version – simple ‘major.minor.patch’ TRAPI schema release SemVer

Returns:

True if current version is equal to, or newer than, a targeted ‘minimum_version’

report_header(title: str | None = None, compact_format: bool = True) str

Return a suitably generated report header. :param title: Optional[str], if title is None, then only the ‘reasoner-validator’ version is printed out

in the header. If the title is an empty string (the default), then ‘Validation Report’ used.

Parameters:

compact_format – bool, whether to print the header in compact format (default: True). Extra line feeds are otherwise provided to provide space around the header and control characters are output to underline the header.

Returns:

str, generated header.

reset_trapi_version(version: str)

Reset TRAPI version tracked by the TRAPISchemaValidator. :param version: new version :return: None

strict_validation: bool | None
to_dict() Dict

Export TRAPISchemaValidator contents as a Python dictionary (including TRAPI version and parent class dictionary content) :return: Dict

validate(instance, component)

Validate instance against schema.

Parameters:
instance

dict, instance to validate

componentstr

TRAPI subschema to validate (e.g. ‘Query’, ‘QueryGraph’, ‘KnowledgeGraph’, ‘Result’; Default: ‘Query’)

Raises:
`ValidationError

<https://python-jsonschema.readthedocs.io/en/latest/errors/#jsonschema.exceptions.ValidationError>`_ If the instance is invalid.

Examples

>>> TRAPISchemaValidator(trapi_version="1.3.0").validate({"message": {}}, "QGraph")
async reasoner_validator.trapi.call_trapi(url: str, trapi_message)

Given an url and a TRAPI message, post the message to the url and return the status and json response.

Parameters:
  • url

  • trapi_message

Returns:

reasoner_validator.trapi.fix_nullable(schema) None

Fix nullable schema.

reasoner_validator.trapi.load_schema(target: str)

Load schema from GitHub release or branch, or from a locally specified YAML schema file. :param target: release semver, schema file path (with ‘.yaml’ file extension)

or a git branch name, all referencing a target TRAPI schema.

Returns:

loaded TRAPI schema

reasoner_validator.trapi.map_semver(version: str)
reasoner_validator.trapi.openapi_to_jsonschema(schema, version: str) None

Convert OpenAPI schema to JSON schema. :param schema: Dict, in-memory representation of the OpenAPI schema to be validated. :param version: str, TRAPI version against which the schema is currently being validated. :return:

reasoner_validator.trapi.patch_schema(tag: str, schema: Dict, version: str)