Skip to content

Commit a07f3bb

Browse files
authored
Add regex validation pattern in schema version property (#450)
1 parent e76872a commit a07f3bb

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/probeinterface/schema/probe.json.schema

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "https://raw.githubusercontent.com/SpikeInterface/probeinterface/main/src/probeinterface/schema/probe.json.schema",
4+
"$comment": "Validates probeinterface specification versions matching the 'version' property pattern (currently 0.3.x and 0.4.x). The pattern is bumped only when the schema changes incompatibly. See https://probeinterface.readthedocs.io",
35
"type": "object",
46
"properties": {
57
"specification": {
68
"type": "string",
7-
"value": "probeinterface"
9+
"const": "probeinterface"
810
},
911
"version": {
1012
"type": "string",
11-
"pattern": "^\\d+\\.\\d+\\.\\d+$"
13+
"$comment": "Range of specification versions this schema validates. Bump when the schema changes incompatibly.",
14+
"pattern": "^0\\.[34]\\.\\d+$"
1215
},
1316
"probes": {
1417
"type": "array",

tests/test_schema.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import re
2+
3+
from probeinterface import __version__
4+
from probeinterface.testing import schema
5+
6+
7+
def test_schema_is_annotated():
8+
"""The schema must carry the annotations documenting what it validates."""
9+
assert schema.get("$schema"), "The schema must declare the JSON Schema draft it uses ('$schema')."
10+
assert "specification versions" in schema.get(
11+
"$comment", ""
12+
), "The schema '$comment' must document which specification versions it validates."
13+
14+
15+
def test_package_version_is_compatible_with_schema():
16+
"""
17+
The current package (specification) version must match the compatibility pattern
18+
declared by the schema's 'version' property.
19+
20+
The schema rarely changes, so it stays compatible across many releases. When a
21+
release introduces an incompatible schema change, bump the 'version' pattern in
22+
``src/probeinterface/schema/probe.json.schema`` (e.g. to allow ``1.x.y``).
23+
"""
24+
pattern = schema["properties"]["version"]["pattern"]
25+
assert re.fullmatch(pattern, __version__), (
26+
f"The package version ({__version__}) does not match the schema's declared "
27+
f"compatibility pattern ({pattern}). Either this is an incompatible schema "
28+
f"change (update the pattern) or the version is malformed."
29+
)

0 commit comments

Comments
 (0)