Skip to content

validate

ensembl.io.genomio.schemas.json.validate

Validates a JSON file with the provided JSON schema.

Examples:

>>> from ensembl.io.genomio.schemas import json
>>> json.schema_validator(json_file="functional_annotation.json", json_schema="functional_annotation")
>>> json.schema_validator(json_file="functional_annotation.json", json_schema="genome")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ensembl-genomio/src/python/ensembl/io/genomio/schemas/json/validate.py", line 63,
  in schema_validator
    jsonschema.validate(instance=content, schema=schema)
  File ".venv/dev/lib/python3.10/site-packages/jsonschema/validators.py", line 1306, in validate
    raise error
<list of all the elements from functional_annotation.json that failed validation>

main()

Main script entry-point.

Source code in src/python/ensembl/io/genomio/schemas/json/validate.py
74
75
76
77
78
79
80
81
82
83
84
def main() -> None:
    """Main script entry-point."""
    parser = ArgumentParser(description="Validates a JSON file against a JSON schema.")
    parser.add_argument_src_path("--json_file", required=True, help="JSON file to check")
    parser.add_argument(
        "--json_schema", required=True, choices=_JSON_SCHEMAS.keys(), help="JSON schema to validate against"
    )
    parser.add_argument("--version", action="version", version=ensembl.io.genomio.__version__)
    args = parser.parse_args()

    schema_validator(args.json_file, args.json_schema)

schema_validator(json_file, json_schema)

Validates a JSON file with the provided JSON schema.

Parameters:

Name Type Description Default
json_file PathLike

Path to the JSON file to check.

required
json_schema Union[str, PathLike]

JSON schema to validate json_file against, either a string matching a existing schema (in data/schemas) or a JSON schema file.

required
Source code in src/python/ensembl/io/genomio/schemas/json/validate.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def schema_validator(json_file: PathLike, json_schema: Union[str, PathLike]) -> None:
    """Validates a JSON file with the provided JSON schema.

    Args:
        json_file: Path to the JSON file to check.
        json_schema: JSON schema to validate `json_file` against, either a string matching a existing
            schema (in data/schemas) or a JSON schema file.

    """
    # Open IO for JSON files and validate it
    with Path(json_file).open("r") as fh:
        content = json.load(fh)
    # Find the json_schema file if a known identifier is provided (if not, treat it as a file path)
    if isinstance(json_schema, str) and (json_schema in _JSON_SCHEMAS):
        json_schema = _JSON_SCHEMAS[json_schema]
    with Path(json_schema).open("r") as fh:
        schema = json.load(fh)
    jsonschema.validate(instance=content, schema=schema)