Use jsonschema for ROI collection validation
#780
Draft
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.



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
ValidROICollectionGeoJSONvalidator 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 filesschemas/roi_collection.json- JSON Schema for ROI collection GeoJSON filesThis PR description summarises the trade-offs.
Approach taken in PR 773
The original implementation used four separate
@path.validatormethods, each reading the file and checking specific conditions:_file_is_valid_json- parsed JSON and caughtJSONDecodeError_file_is_feature_collection- checkedtypeandfeatureskeys_features_have_valid_geometry- iterated features to check geometry types_roi_type_matches_geometry- validated cross-field consistencyApproach in this PR (JSON Schema)
The refactored implementation uses a JSON schema file (
schemas/roi_collection.json) validated viajsonschema.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:
ethology. If we go with this approach, we could even move some of the json schema-related utils thatethologyhas tomovement.Cons:
jsonschemaas a new required dependencyjsonschemalibrary generates its own error messages, which are technically accurate but less user-friendly than custom messages we define.