GEOPY-2702: Validate "geoh5" for existing path#857
GEOPY-2702: Validate "geoh5" for existing path#857
Conversation
There was a problem hiding this comment.
Pull request overview
This PR tightens validation for the geoh5 path in BaseUIJson by enforcing a .geoh5 file extension and adds a regression test to ensure invalid extensions are rejected.
Changes:
- Added a
geoh5extension validator to require.geoh5. - Switched
geoh5field validation to run inmode="before". - Added a unit test expecting a
ValidationErrorwhengeoh5has the wrong extension.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
geoh5py/ui_json/ui_json.py |
Adds .geoh5 extension validation and changes validator execution mode for geoh5. |
tests/ui_json/uijson_test.py |
Adds a test asserting invalid geoh5 extensions raise a Pydantic ValidationError. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
geoh5py/ui_json/ui_json.py
Outdated
| @field_validator("geoh5", mode="before") | ||
| @classmethod | ||
| def workspace_path_exists(cls, path: Path): | ||
| if not path.exists(): | ||
| raise FileNotFoundError(f"geoh5 path {path} does not exist.") | ||
| return path | ||
|
|
||
| @field_validator("geoh5", mode="before") | ||
| @classmethod | ||
| def valid_geoh5_extension(cls, path: Path): |
There was a problem hiding this comment.
geoh5 is frequently passed as a string (e.g., other tests call geoh5=str(...)). With mode="before", Pydantic validators receive the raw input, so path can be a str here and path.exists() will raise an AttributeError rather than producing the intended validation error. Consider switching this validator back to mode="after" (so the value is parsed into a Path first) or explicitly coercing path = Path(path) when a string is provided.
| @field_validator("geoh5", mode="before") | |
| @classmethod | |
| def workspace_path_exists(cls, path: Path): | |
| if not path.exists(): | |
| raise FileNotFoundError(f"geoh5 path {path} does not exist.") | |
| return path | |
| @field_validator("geoh5", mode="before") | |
| @classmethod | |
| def valid_geoh5_extension(cls, path: Path): | |
| @field_validator("geoh5", mode="after") | |
| @classmethod | |
| def workspace_path_exists(cls, path: Path | None): | |
| if path is None: | |
| return path | |
| if not path.exists(): | |
| raise FileNotFoundError(f"geoh5 path {path} does not exist.") | |
| return path | |
| @field_validator("geoh5", mode="after") | |
| @classmethod | |
| def valid_geoh5_extension(cls, path: Path | None): | |
| if path is None: | |
| return path |
There was a problem hiding this comment.
I think Copilot is right. Why before?
There was a problem hiding this comment.
Can't recall the reason. Makes sense to keep as after. Fixed.
geoh5py/ui_json/ui_json.py
Outdated
| @field_validator("geoh5", mode="before") | ||
| @classmethod | ||
| def workspace_path_exists(cls, path: Path): | ||
| if not path.exists(): | ||
| raise FileNotFoundError(f"geoh5 path {path} does not exist.") | ||
| return path | ||
|
|
||
| @field_validator("geoh5", mode="before") | ||
| @classmethod | ||
| def valid_geoh5_extension(cls, path: Path): |
There was a problem hiding this comment.
I think Copilot is right. Why before?
|
|
||
|
|
||
| def test_geoh5_validate_extension(tmp_path): | ||
| h5file = tmp_path / "test" |
There was a problem hiding this comment.
Similar to comment above, input would be a str from file. So test with that.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #857 +/- ##
===========================================
- Coverage 91.18% 91.18% -0.01%
===========================================
Files 115 115
Lines 10272 10298 +26
Branches 1898 1901 +3
===========================================
+ Hits 9367 9390 +23
- Misses 483 485 +2
- Partials 422 423 +1
🚀 New features to boost your workflow:
|
GEOPY-2702 - Validate "geoh5" for existing path