Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cdm_data_loaders/schemas/gff/gff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
}
},
"required": [
"sequid",
"seqid",
"source",
"type",
"start",
Expand Down
Empty file.
39 changes: 39 additions & 0 deletions tests/cdm_data_loaders/schemas/test_required_properties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Check that the bundled JSON Schemas only require properties they define."""

import json
from collections.abc import Iterator
from pathlib import Path
from typing import Any, Final

import pytest

import cdm_data_loaders.schemas

SCHEMA_DIR: Final[Path] = Path(cdm_data_loaders.schemas.__file__).parent
SCHEMA_FILES: Final[list[Path]] = sorted(SCHEMA_DIR.rglob("*.json"))


def _objects_with_required(node: Any) -> Iterator[dict[str, Any]]: # noqa: ANN401
"""Yield every sub-schema of ``node`` that has both ``properties`` and ``required``."""
if isinstance(node, dict):
if isinstance(node.get("properties"), dict) and isinstance(node.get("required"), list):
yield node
for value in node.values():
yield from _objects_with_required(value)
elif isinstance(node, list):
for value in node:
yield from _objects_with_required(value)


def test_schema_files_found() -> None:
"""Ensure the parametrized test below has schemas to check."""
assert SCHEMA_FILES


@pytest.mark.parametrize("path", SCHEMA_FILES, ids=lambda p: str(p.relative_to(SCHEMA_DIR)))
def test_required_names_are_properties(path: Path) -> None:
"""Every name in a ``required`` list must be defined in the same sub-schema's ``properties``."""
schema = json.loads(path.read_text())
for obj in _objects_with_required(schema):
missing = sorted(set(obj["required"]) - set(obj["properties"]))
assert not missing, f"{path.name} requires names with no matching property: {missing}"
Loading