11"""Validators."""
22
33import logging
4- from typing import Callable , List , Mapping
4+ from typing import Callable , List , Mapping , Optional
55
66from jsonschema import ValidationError
77from linkml .validator import ValidationReport , Validator
1212from sssom .parsers import to_mapping_set_document
1313from sssom .util import MappingSetDataFrame , get_all_prefixes
1414
15- from .constants import SCHEMA_YAML , SchemaValidationType , _get_sssom_schema_object
15+ from .constants import (
16+ DEFAULT_VALIDATION_TYPES ,
17+ SCHEMA_YAML ,
18+ SchemaValidationType ,
19+ _get_sssom_schema_object ,
20+ )
1621
1722
1823def validate (
1924 msdf : MappingSetDataFrame ,
20- validation_types : List [SchemaValidationType ],
25+ validation_types : Optional [ List [SchemaValidationType ]] = None ,
2126 fail_on_error : bool = True ,
22- ) -> None :
27+ ) -> dict [ SchemaValidationType , ValidationReport ] :
2328 """Validate SSSOM files against `sssom-schema` using linkML's validator function.
2429
2530 :param msdf: MappingSetDataFrame.
2631 :param validation_types: SchemaValidationType
2732 :param fail_on_error: If true, throw an error when execution of a method has failed
33+ :returns: A dictionary from validation types to validation reports
2834 """
29- for vt in validation_types :
30- VALIDATION_METHODS [vt ](msdf , fail_on_error )
35+ if validation_types is None :
36+ validation_types = DEFAULT_VALIDATION_TYPES
37+ return {vt : VALIDATION_METHODS [vt ](msdf , fail_on_error ) for vt in validation_types }
3138
3239
3340def print_linkml_report (report : ValidationReport , fail_on_error : bool = True ):
@@ -88,7 +95,7 @@ def _clean_dict(d):
8895 return cleaned_dict
8996
9097
91- def validate_json_schema (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> None :
98+ def validate_json_schema (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> ValidationReport :
9299 """Validate JSON Schema using linkml's JsonSchemaDataValidator.
93100
94101 :param msdf: MappingSetDataFrame to eb validated.
@@ -106,9 +113,10 @@ def validate_json_schema(msdf: MappingSetDataFrame, fail_on_error: bool = True)
106113
107114 report = validator .validate (mapping_set_dict , "mapping set" )
108115 print_linkml_report (report , fail_on_error )
116+ return report
109117
110118
111- def validate_shacl (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> None :
119+ def validate_shacl (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> ValidationReport :
112120 """Validate SCHACL file.
113121
114122 :param msdf: TODO: https://github.com/linkml/linkml/issues/850 .
@@ -118,7 +126,7 @@ def validate_shacl(msdf: MappingSetDataFrame, fail_on_error: bool = True) -> Non
118126 raise NotImplementedError
119127
120128
121- def validate_sparql (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> None :
129+ def validate_sparql (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> ValidationReport :
122130 """Validate SPARQL file.
123131
124132 :param msdf: MappingSetDataFrame
@@ -132,7 +140,9 @@ def validate_sparql(msdf: MappingSetDataFrame, fail_on_error: bool = True) -> No
132140 raise NotImplementedError
133141
134142
135- def check_all_prefixes_in_curie_map (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> None :
143+ def check_all_prefixes_in_curie_map (
144+ msdf : MappingSetDataFrame , fail_on_error : bool = True
145+ ) -> ValidationReport :
136146 """Check all `EntityReference` slots are mentioned in 'curie_map'.
137147
138148 :param msdf: MappingSetDataFrame
@@ -154,9 +164,12 @@ def check_all_prefixes_in_curie_map(msdf: MappingSetDataFrame, fail_on_error: bo
154164 )
155165 report = ValidationReport (results = validation_results )
156166 print_linkml_report (report , fail_on_error )
167+ return report
157168
158169
159- def check_strict_curie_format (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> None :
170+ def check_strict_curie_format (
171+ msdf : MappingSetDataFrame , fail_on_error : bool = True
172+ ) -> ValidationReport :
160173 """Check all `EntityReference` slots are formatted as unambiguous curies.
161174
162175 Implemented rules:
@@ -194,9 +207,10 @@ def check_strict_curie_format(msdf: MappingSetDataFrame, fail_on_error: bool = T
194207
195208 report = ValidationReport (results = validation_results )
196209 print_linkml_report (report , fail_on_error )
210+ return report
197211
198212
199- VALIDATION_METHODS : Mapping [SchemaValidationType , Callable ] = {
213+ VALIDATION_METHODS : Mapping [SchemaValidationType , Callable [..., ValidationReport ] ] = {
200214 SchemaValidationType .JsonSchema : validate_json_schema ,
201215 SchemaValidationType .Shacl : validate_shacl ,
202216 SchemaValidationType .Sparql : validate_sparql ,
0 commit comments