Skip to content

Validators

Schema validation for SSSOM files.

sssom.validators

Validators.

SSSOMValidationError

Bases: ValidationError

Raised when a mapping set fails validation.

All problems are available in :attr:results, with one problem per JSON path, so that a value that JSON Schema validation reports twice counts once. The string representation lists at most :data:MAX_REPORTED_PROBLEMS of them, so that tracebacks stay readable.

Source code in src/sssom/validators.py
class SSSOMValidationError(ValidationError):
    """Raised when a mapping set fails validation.

    All problems are available in :attr:`results`, with one problem per JSON path, so that a value
    that JSON Schema validation reports twice counts once. The string representation lists at
    most :data:`MAX_REPORTED_PROBLEMS` of them, so that tracebacks stay readable.
    """

    def __init__(self, results: List[ValidationResult]) -> None:
        """Instantiate the error from the validation results that caused it."""
        results = _deduplicate_by_path(results)
        super().__init__(_summarize(results))
        self.results = results

    def __str__(self) -> str:
        """List the problems, up to :data:`MAX_REPORTED_PROBLEMS` of them."""
        return format_validation_results(self.results, limit=MAX_REPORTED_PROBLEMS)
__init__(results)

Instantiate the error from the validation results that caused it.

Source code in src/sssom/validators.py
def __init__(self, results: List[ValidationResult]) -> None:
    """Instantiate the error from the validation results that caused it."""
    results = _deduplicate_by_path(results)
    super().__init__(_summarize(results))
    self.results = results
__str__()

List the problems, up to :data:MAX_REPORTED_PROBLEMS of them.

Source code in src/sssom/validators.py
def __str__(self) -> str:
    """List the problems, up to :data:`MAX_REPORTED_PROBLEMS` of them."""
    return format_validation_results(self.results, limit=MAX_REPORTED_PROBLEMS)

validate(msdf, validation_types=None, fail_on_error=True)

Validate SSSOM files against sssom-schema using linkML's validator function.

All requested validation types are run before anything is raised, so that the problems found by all of them are reported together. Nothing is logged; use :func:print_linkml_report or :func:format_validation_results to show the returned reports.

:param msdf: MappingSetDataFrame. :param validation_types: SchemaValidationType :param fail_on_error: If true, raise an error when any validation type reports problems with a severity in :data:FAILING_SEVERITIES

:returns: A dictionary from validation types to validation reports

:raises SSSOMValidationError: If fail_on_error is true and any validation type reports problems with a severity in :data:FAILING_SEVERITIES.

Source code in src/sssom/validators.py
def validate(
    msdf: MappingSetDataFrame,
    validation_types: Optional[List[SchemaValidationType]] = None,
    fail_on_error: bool = True,
) -> dict[SchemaValidationType, ValidationReport]:
    """Validate SSSOM files against `sssom-schema` using linkML's validator function.

    All requested validation types are run before anything is raised, so that the problems found
    by all of them are reported together. Nothing is logged; use :func:`print_linkml_report` or
    :func:`format_validation_results` to show the returned reports.

    :param msdf: MappingSetDataFrame.
    :param validation_types: SchemaValidationType
    :param fail_on_error: If true, raise an error when any validation type reports problems with
        a severity in :data:`FAILING_SEVERITIES`

    :returns: A dictionary from validation types to validation reports

    :raises SSSOMValidationError: If fail_on_error is true and any validation type reports
        problems with a severity in :data:`FAILING_SEVERITIES`.
    """
    if validation_types is None:
        validation_types = DEFAULT_VALIDATION_TYPES
    reports = {vt: _REPORT_METHODS[vt](msdf) for vt in validation_types}
    if fail_on_error and (failing := get_failing_results(reports.values())):
        raise SSSOMValidationError(failing)
    return reports

get_failing_results(reports)

Collect the results that make a mapping set fail validation.

:param reports: Validation reports.

:returns: The results with a severity in :data:FAILING_SEVERITIES, in report order.

Source code in src/sssom/validators.py
def get_failing_results(reports: Iterable[ValidationReport]) -> List[ValidationResult]:
    """Collect the results that make a mapping set fail validation.

    :param reports: Validation reports.

    :returns: The results with a severity in :data:`FAILING_SEVERITIES`, in report order.
    """
    return [
        result
        for report in reports
        for result in report.results
        if result.severity in FAILING_SEVERITIES
    ]

format_validation_results(results, limit=None)

Format validation results as a human-readable list of problems.

:param results: The validation results to format. :param limit: The maximum number of problems to list, or None to list all of them.

:returns: A summary line followed by one line per problem.

Source code in src/sssom/validators.py
def format_validation_results(results: List[ValidationResult], limit: Optional[int] = None) -> str:
    """Format validation results as a human-readable list of problems.

    :param results: The validation results to format.
    :param limit: The maximum number of problems to list, or None to list all of them.

    :returns: A summary line followed by one line per problem.
    """
    lines = [f"{_summarize(results)}:"]
    shown = results if limit is None else results[:limit]
    lines.extend(f"  - {r.message}" for r in shown)
    if len(results) > len(shown):
        lines.append(f"  ... and {len(results) - len(shown)} more")
    return "\n".join(lines)

print_linkml_report(report, fail_on_error=True)

Print the error messages in the report. Optionally throw exception.

:param report: A LinkML validation report :param fail_on_error: if true, the function will throw an SSSOMValidationError exception when there are results with a severity in :data:FAILING_SEVERITIES, listing them in its message. Otherwise, the problems are logged.

:raises SSSOMValidationError: If fail_on_error is true and the report has results with a severity in :data:FAILING_SEVERITIES.

Source code in src/sssom/validators.py
def print_linkml_report(report: ValidationReport, fail_on_error: bool = True) -> None:
    """Print the error messages in the report. Optionally throw exception.

    :param report: A LinkML validation report
    :param fail_on_error: if true, the function will throw an SSSOMValidationError exception when
        there are results with a severity in :data:`FAILING_SEVERITIES`, listing them in its
        message. Otherwise, the problems are logged.

    :raises SSSOMValidationError: If fail_on_error is true and the report has results with a
        severity in :data:`FAILING_SEVERITIES`.
    """
    results = report.results
    if not results:
        logging.info("The instance is valid!")
        return

    failing = get_failing_results([report])
    if fail_on_error and failing:
        raise SSSOMValidationError(failing)

    for result in results:
        logging.log(_LOG_LEVELS.get(result.severity, logging.ERROR), result.message)

validate_json_schema(msdf, fail_on_error=True)

Validate JSON Schema using linkml's JsonSchemaDataValidator.

:param msdf: MappingSetDataFrame to eb validated. :param fail_on_error: if true, the function will throw an ValidationError exception when there are errors

Source code in src/sssom/validators.py
def validate_json_schema(msdf: MappingSetDataFrame, fail_on_error: bool = True) -> ValidationReport:
    """Validate JSON Schema using linkml's JsonSchemaDataValidator.

    :param msdf: MappingSetDataFrame to eb validated.
    :param fail_on_error: if true, the function will throw an ValidationError exception when there
        are errors
    """
    report = _json_schema_report(msdf)
    print_linkml_report(report, fail_on_error)
    return report

validate_shacl(msdf, fail_on_error=True)

Validate SCHACL file.

:param msdf: TODO: https://github.com/linkml/linkml/issues/850 . :param fail_on_error: if true, the function will throw an ValidationError exception when there are errors

:raises NotImplementedError: Not yet implemented.

Source code in src/sssom/validators.py
def validate_shacl(msdf: MappingSetDataFrame, fail_on_error: bool = True) -> ValidationReport:
    """Validate SCHACL file.

    :param msdf: TODO: https://github.com/linkml/linkml/issues/850 .
    :param fail_on_error: if true, the function will throw an ValidationError exception when there
        are errors

    :raises NotImplementedError: Not yet implemented.
    """
    raise NotImplementedError

validate_sparql(msdf, fail_on_error=True)

Validate SPARQL file.

:param msdf: MappingSetDataFrame :param fail_on_error: if true, the function will throw an ValidationError exception when there are errors

:raises NotImplementedError: Not yet implemented.

Source code in src/sssom/validators.py
def validate_sparql(msdf: MappingSetDataFrame, fail_on_error: bool = True) -> ValidationReport:
    """Validate SPARQL file.

    :param msdf: MappingSetDataFrame
    :param fail_on_error: if true, the function will throw an ValidationError exception when there
        are errors

    :raises NotImplementedError: Not yet implemented.
    """
    # queries = {}
    # validator = SparqlDataValidator(SCHEMA_YAML,queries=queries)
    # mapping_set = to_mapping_set_document(msdf).mapping_set
    # TODO: Complete this function
    raise NotImplementedError

check_all_prefixes_in_curie_map(msdf, fail_on_error=True)

Check all EntityReference slots are mentioned in 'curie_map'.

:param msdf: MappingSetDataFrame :param fail_on_error: if true, the function will throw an ValidationError exception when there are errors

:raises ValidationError: If all prefixes not in curie_map.

Source code in src/sssom/validators.py
def check_all_prefixes_in_curie_map(
    msdf: MappingSetDataFrame, fail_on_error: bool = True
) -> ValidationReport:
    """Check all `EntityReference` slots are mentioned in 'curie_map'.

    :param msdf: MappingSetDataFrame
    :param fail_on_error: if true, the function will throw an ValidationError exception when there
        are errors

    :raises ValidationError: If all prefixes not in curie_map.
    """
    report = _prefix_map_completeness_report(msdf)
    print_linkml_report(report, fail_on_error)
    return report

check_strict_curie_format(msdf, fail_on_error=True)

Check all EntityReference slots are formatted as unambiguous curies.

Implemented rules:

  1. CURIE does not contain pipe "|" character to ensure that multivalued processing of in TSV works correctly.

:param msdf: MappingSetDataFrame :param fail_on_error: if true, the function will throw an ValidationError exception when there are errors

:raises ValidationError: If any entity reference does not follow the strict CURIE format

Source code in src/sssom/validators.py
def check_strict_curie_format(
    msdf: MappingSetDataFrame, fail_on_error: bool = True
) -> ValidationReport:
    """Check all `EntityReference` slots are formatted as unambiguous curies.

    Implemented rules:

    1. CURIE does not contain pipe "|" character to ensure that multivalued processing of in TSV
       works correctly.

    :param msdf: MappingSetDataFrame
    :param fail_on_error: if true, the function will throw an ValidationError exception when there
        are errors

    :raises ValidationError: If any entity reference does not follow the strict CURIE format
    """
    report = _strict_curie_format_report(msdf)
    print_linkml_report(report, fail_on_error)
    return report