-
Notifications
You must be signed in to change notification settings - Fork 25
fix(py,ts,mcp): make validation mean the same thing at every version and in both ports #687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
vboussot
wants to merge
10
commits into
fideus-labs:fix/rfc3-axes
from
vboussot:fix/py-667-validation-coherence
Closed
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f390d89
fix(py,ts): accept at 0.6 what the 0.6 schema declares
vboussot 4f385e8
fix(py): make validate=True mean the same thing at every version
vboussot 5fecc67
fix(mcp): report a store the spec refuses as invalid
vboussot edfc814
docs(py): correct two claims the code does not keep
vboussot 6655e4f
fix(ts): stop measuring a v0.6 inter-system transform against the int…
vboussot db2ef60
docs(py): say which structural rules restate a schema keyword
vboussot 58853b0
fix(mcp): validate against the version the store declares
vboussot c5254d0
fix(mcp,ts): satisfy the type check and the TypeScript formatter
vboussot 7445667
fix(py): keep active only when it is a boolean, and validate by version
vboussot 1973ffd
docs: pass the version in the direct-validation example
vboussot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) Fideus Labs LLC | ||
| # SPDX-License-Identifier: MIT | ||
| """``validate_ome_zarr`` reports a store the spec refuses as invalid. | ||
|
|
||
| The tool runs two passes over a store: the JSON Schema of the store's own | ||
| version, and the structural rules that carry the spec MUSTs no schema states. | ||
| A failure in either is an error, so it reaches the caller through ``valid``. | ||
| """ | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| from ngff_zarr import to_multiscales, to_ngff_image, to_ome_zarr | ||
|
|
||
| from ngff_zarr_mcp.tools import validate_ome_zarr | ||
|
|
||
| VERSIONS = ["0.4", "0.5", "0.6"] | ||
|
|
||
|
|
||
| def _write_store(path: Path, version: str) -> Path: | ||
| image = to_ngff_image( | ||
| np.zeros((32, 32), dtype=np.uint8), dims=["y", "x"], scale={"y": 1.0, "x": 1.0} | ||
| ) | ||
| multiscales = to_multiscales(image, scale_factors=[2]) | ||
| to_ome_zarr(str(path), multiscales, version=version) | ||
| return path | ||
|
|
||
|
|
||
| def _root_document(store: Path) -> tuple[Path, dict]: | ||
| """The root attributes document and its path, whichever layout is on disk.""" | ||
| v3 = store / "zarr.json" | ||
| if v3.exists(): | ||
| return v3, json.loads(v3.read_text()) | ||
| v2 = store / ".zattrs" | ||
| return v2, json.loads(v2.read_text()) | ||
|
|
||
|
|
||
| def _edit_multiscales(store: Path, mutate) -> None: | ||
| path, document = _root_document(store) | ||
| if "attributes" in document: | ||
| multiscales = document["attributes"]["ome"]["multiscales"] | ||
| else: | ||
| multiscales = document["multiscales"] | ||
| mutate(multiscales[0]) | ||
| path.write_text(json.dumps(document)) | ||
| consolidated = store / "zarr.json" | ||
| if consolidated.exists() and path == consolidated: | ||
| return | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("version", VERSIONS) | ||
| async def test_a_valid_store_is_valid_at_every_version(tmp_path, version): | ||
| """Zarr v3 stores, which is every 0.5 and 0.6 store, get past the store check.""" | ||
| store = _write_store(tmp_path / f"valid-{version}.zarr", version) | ||
|
|
||
| result = await validate_ome_zarr(str(store)) | ||
|
|
||
| assert result.valid, result.errors | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("version", VERSIONS) | ||
| async def test_datasets_ordered_coarsest_to_finest_are_refused(tmp_path, version): | ||
| """`dataset-order-highest-to-lowest`, a rule no JSON Schema states.""" | ||
| store = _write_store(tmp_path / f"reversed-{version}.zarr", version) | ||
| _edit_multiscales(store, lambda ms: ms["datasets"].reverse()) | ||
|
|
||
| result = await validate_ome_zarr(str(store)) | ||
|
|
||
| assert not result.valid | ||
| assert any("dataset" in error.lower() for error in result.errors), result.errors | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_a_document_the_schema_rejects_is_an_error_not_a_warning(tmp_path): | ||
| """A schema failure used to be demoted to a warning, leaving `valid` True.""" | ||
| pytest.importorskip("jsonschema") | ||
| store = _write_store(tmp_path / "no-axes.zarr", "0.4") | ||
| _edit_multiscales(store, lambda ms: ms.pop("axes")) | ||
|
|
||
| result = await validate_ome_zarr(str(store)) | ||
|
|
||
| assert not result.valid | ||
| assert result.errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.