Coverage for src/python/ensembl/io/genomio/schemas/json/validate.py: 83%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-21 15:37 +0000

1# See the NOTICE file distributed with this work for additional information 

2# regarding copyright ownership. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15"""Validates a JSON file with the provided JSON schema. 

16 

17Examples: 

18 

19 >>> from ensembl.io.genomio.schemas import json 

20 >>> json.schema_validator(json_file="functional_annotation.json", json_schema="functional_annotation") 

21 >>> json.schema_validator(json_file="functional_annotation.json", json_schema="genome") 

22 Traceback (most recent call last): 

23 File "<stdin>", line 1, in <module> 

24 File "ensembl-genomio/src/python/ensembl/io/genomio/schemas/json/validate.py", line 63, 

25 in schema_validator 

26 jsonschema.validate(instance=content, schema=schema) 

27 File ".venv/dev/lib/python3.10/site-packages/jsonschema/validators.py", line 1306, in validate 

28 raise error 

29 <list of all the elements from functional_annotation.json that failed validation> 

30 

31""" 

32 

33__all__ = ["schema_validator"] 

34 

35from importlib.resources import as_file, files 

36import json 

37from os import PathLike 

38from pathlib import Path 

39from typing import Union 

40 

41import jsonschema 

42 

43import ensembl.io.genomio 

44from ensembl.utils.argparse import ArgumentParser 

45 

46 

47_JSON_SCHEMAS = {} 

48for schema_file in files("ensembl.io.genomio.data.schemas").iterdir(): 

49 with as_file(schema_file) as file_path: 

50 if file_path.suffix == ".json": 

51 _JSON_SCHEMAS[file_path.stem] = file_path 

52 

53 

54def schema_validator(json_file: PathLike, json_schema: Union[str, PathLike]) -> None: 

55 """Validates a JSON file with the provided JSON schema. 

56 

57 Args: 

58 json_file: Path to the JSON file to check. 

59 json_schema: JSON schema to validate `json_file` against, either a string matching a existing 

60 schema (in data/schemas) or a JSON schema file. 

61 

62 """ 

63 # Open IO for JSON files and validate it 

64 with Path(json_file).open("r") as fh: 

65 content = json.load(fh) 

66 # Find the json_schema file if a known identifier is provided (if not, treat it as a file path) 

67 if isinstance(json_schema, str) and (json_schema in _JSON_SCHEMAS): 

68 json_schema = _JSON_SCHEMAS[json_schema] 

69 with Path(json_schema).open("r") as fh: 

70 schema = json.load(fh) 

71 jsonschema.validate(instance=content, schema=schema) 

72 

73 

74def main() -> None: 

75 """Main script entry-point.""" 

76 parser = ArgumentParser(description="Validates a JSON file against a JSON schema.") 

77 parser.add_argument_src_path("--json_file", required=True, help="JSON file to check") 

78 parser.add_argument( 

79 "--json_schema", required=True, choices=_JSON_SCHEMAS.keys(), help="JSON schema to validate against" 

80 ) 

81 parser.add_argument("--version", action="version", version=ensembl.io.genomio.__version__) 

82 args = parser.parse_args() 

83 

84 schema_validator(args.json_file, args.json_schema)