1
1
"""Validators."""
2
2
3
3
import logging
4
- from typing import Callable , List , Mapping
4
+ from typing import Callable , List , Mapping , Optional
5
5
6
6
from jsonschema import ValidationError
7
7
from linkml .validator import ValidationReport , Validator
12
12
from sssom .parsers import to_mapping_set_document
13
13
from sssom .util import MappingSetDataFrame , get_all_prefixes
14
14
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
+ )
16
21
17
22
18
23
def validate (
19
24
msdf : MappingSetDataFrame ,
20
- validation_types : List [SchemaValidationType ],
25
+ validation_types : Optional [ List [SchemaValidationType ]] = None ,
21
26
fail_on_error : bool = True ,
22
- ) -> None :
27
+ ) -> dict [ SchemaValidationType , ValidationReport ] :
23
28
"""Validate SSSOM files against `sssom-schema` using linkML's validator function.
24
29
25
30
:param msdf: MappingSetDataFrame.
26
31
:param validation_types: SchemaValidationType
27
32
: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
28
34
"""
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 }
31
38
32
39
33
40
def print_linkml_report (report : ValidationReport , fail_on_error : bool = True ):
@@ -88,7 +95,7 @@ def _clean_dict(d):
88
95
return cleaned_dict
89
96
90
97
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 :
92
99
"""Validate JSON Schema using linkml's JsonSchemaDataValidator.
93
100
94
101
:param msdf: MappingSetDataFrame to eb validated.
@@ -106,9 +113,10 @@ def validate_json_schema(msdf: MappingSetDataFrame, fail_on_error: bool = True)
106
113
107
114
report = validator .validate (mapping_set_dict , "mapping set" )
108
115
print_linkml_report (report , fail_on_error )
116
+ return report
109
117
110
118
111
- def validate_shacl (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> None :
119
+ def validate_shacl (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> ValidationReport :
112
120
"""Validate SCHACL file.
113
121
114
122
: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
118
126
raise NotImplementedError
119
127
120
128
121
- def validate_sparql (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> None :
129
+ def validate_sparql (msdf : MappingSetDataFrame , fail_on_error : bool = True ) -> ValidationReport :
122
130
"""Validate SPARQL file.
123
131
124
132
:param msdf: MappingSetDataFrame
@@ -132,7 +140,9 @@ def validate_sparql(msdf: MappingSetDataFrame, fail_on_error: bool = True) -> No
132
140
raise NotImplementedError
133
141
134
142
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 :
136
146
"""Check all `EntityReference` slots are mentioned in 'curie_map'.
137
147
138
148
:param msdf: MappingSetDataFrame
@@ -154,9 +164,12 @@ def check_all_prefixes_in_curie_map(msdf: MappingSetDataFrame, fail_on_error: bo
154
164
)
155
165
report = ValidationReport (results = validation_results )
156
166
print_linkml_report (report , fail_on_error )
167
+ return report
157
168
158
169
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 :
160
173
"""Check all `EntityReference` slots are formatted as unambiguous curies.
161
174
162
175
Implemented rules:
@@ -194,9 +207,10 @@ def check_strict_curie_format(msdf: MappingSetDataFrame, fail_on_error: bool = T
194
207
195
208
report = ValidationReport (results = validation_results )
196
209
print_linkml_report (report , fail_on_error )
210
+ return report
197
211
198
212
199
- VALIDATION_METHODS : Mapping [SchemaValidationType , Callable ] = {
213
+ VALIDATION_METHODS : Mapping [SchemaValidationType , Callable [..., ValidationReport ] ] = {
200
214
SchemaValidationType .JsonSchema : validate_json_schema ,
201
215
SchemaValidationType .Shacl : validate_shacl ,
202
216
SchemaValidationType .Sparql : validate_sparql ,
0 commit comments