Skip to content

Conversation

@niksirbi
Copy link
Member

@niksirbi niksirbi commented Jan 26, 2026

This PR contains an alternative implementation of ValidROICollectionGeoJSON, compared to the one found in #773.

If we decide that we prefer this one, we should merge this PR into the branch of #773. I'm personally undecided.

The ValidROICollectionGeoJSON validator was refactored from a manual validation approach to a schema-based approach. The following components were added:

  • utils.py - Helper functions for loading schemas and validating files
  • schemas/roi_collection.json - JSON Schema for ROI collection GeoJSON files

This PR description summarises the trade-offs.

Approach taken in PR 773

The original implementation used four separate @path.validator methods, each reading the file and checking specific conditions:

  1. _file_is_valid_json - parsed JSON and caught JSONDecodeError
  2. _file_is_feature_collection - checked type and features keys
  3. _features_have_valid_geometry - iterated features to check geometry types
  4. _roi_type_matches_geometry - validated cross-field consistency

Approach in this PR (JSON Schema)

The refactored implementation uses a JSON schema file (schemas/roi_collection.json) validated via jsonschema.validate(), plus one custom validator for cross-field logic that cannot be expressed in JSON Schema.

Pros of Cons of the JSON Schema Approach

Pros:

  • Declarative validation logic: the schema file clearly documents what structure is expected.
  • Separation of concerns: changes to the expected structure only require editing the schema file.
  • Consistency with the approach used in ethology. If we go with this approach, we could even move some of the json schema-related utils that ethology has to movement.
  • Reusability: The utility functions can be reused for other JSON-based file formats in the future (assuming we'll have any).

Cons:

  • jsonschema as a new required dependency
  • Less control over error messages. The jsonschema library generates its own error messages, which are technically accurate but less user-friendly than custom messages we define.
  • Cross-field validation still requires Python.

niksirbi and others added 3 commits January 26, 2026 16:23
Replace manual validation with JSON Schema-based validation using the
jsonschema library. This simplifies the validator from ~90 lines to ~50
lines while maintaining the same validation behavior.

Changes:
- Add jsonschema as a dependency
- Create json_schemas module with utility functions for schema validation
- Add ROI collection JSON schema (schemas/roi_collection.json)
- Simplify ValidROICollectionGeoJSON to use schema-based validation
- Keep custom cross-field validation for roi_type/geometry matching
- Update tests to match jsonschema error message patterns

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@sonarqubecloud
Copy link

@codecov
Copy link

codecov bot commented Jan 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (96239de) to head (9fb4264).

Additional details and impacted files
@@               Coverage Diff                @@
##           load_save_rois      #780   +/-   ##
================================================
  Coverage          100.00%   100.00%           
================================================
  Files                  35        37    +2     
  Lines                2239      2236    -3     
================================================
- Hits                 2239      2236    -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant