-
Couldn't load subscription status.
- Fork 30
add datetime validation for collection time intervals #177
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Contributing | ||
|
|
||
| Issues and pull requests are more than welcome. | ||
|
|
||
| **dev install** | ||
|
|
||
| ```bash | ||
| git clone https://github.com/stac-utils/stac-pydantic.git | ||
| cd stac-pydantic | ||
| python -m pip install -e ".[dev]" | ||
| ``` | ||
|
|
||
| You can then run the tests with the following command: | ||
|
|
||
| ```sh | ||
| python -m pytest --cov stac_pydantic --cov-report term-missing | ||
| ``` | ||
|
|
||
|
|
||
| **pre-commit** | ||
|
|
||
| This repo is set to use `pre-commit` to run *ruff*, *pydocstring* and mypy when committing new code. | ||
|
|
||
| ```bash | ||
| pre-commit install | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,74 @@ | ||
| from typing import Any, Dict, List, Literal, Optional, Union | ||
|
|
||
| from pydantic import Field | ||
| from pydantic import AfterValidator, Field, conlist | ||
| from typing_extensions import Annotated | ||
|
|
||
| from stac_pydantic.catalog import _Catalog | ||
| from stac_pydantic.shared import Asset, NumType, Provider, StacBaseModel | ||
| from stac_pydantic.shared import ( | ||
| Asset, | ||
| BBox, | ||
| NumType, | ||
| Provider, | ||
| StacBaseModel, | ||
| UtcDatetime, | ||
| ) | ||
|
|
||
|
|
||
| def validate_time_interval(v): # noqa: C901 | ||
| ivalues = iter(v) | ||
|
|
||
| # The first time interval always describes the overall temporal extent of the data. | ||
| overall_interval = next(ivalues, None) | ||
| if not overall_interval: | ||
| return v | ||
|
|
||
| start, end = overall_interval | ||
| if start and end: | ||
| if start > end: | ||
| raise ValueError(f"`Start` time {start} older than `End` time {end}") | ||
|
|
||
| # All subsequent time intervals can be used to provide a more precise | ||
| # description of the extent and identify clusters of data. | ||
| for s, e in ivalues: | ||
| if s and e: | ||
| if s > e: | ||
| raise ValueError(f"`Start` time {s} older than `End` time {e}") | ||
|
|
||
| if start and s: | ||
| if start > s: | ||
| raise ValueError( | ||
| f"`Overall Start` time {start} older than `Start` time {s}" | ||
| ) | ||
|
|
||
| if end and e: | ||
| if e > end: | ||
| raise ValueError( | ||
| f"`End` time {e} older than `Overall Start` time {end}" | ||
| ) | ||
|
|
||
| return v | ||
|
|
||
|
|
||
| class SpatialExtent(StacBaseModel): | ||
| """ | ||
| https://github.com/radiantearth/stac-spec/blob/v1.0.0/collection-spec/collection-spec.md#spatial-extent-object | ||
| """ | ||
|
|
||
| bbox: List[List[NumType]] | ||
| bbox: List[BBox] | ||
vincentsarago marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class TimeInterval(StacBaseModel): | ||
| """ | ||
| https://github.com/radiantearth/stac-spec/blob/v1.0.0/collection-spec/collection-spec.md#temporal-extent-object | ||
| """ | ||
|
|
||
| interval: List[List[Union[str, None]]] | ||
| interval: Annotated[ # type: ignore | ||
|
||
| conlist( | ||
| conlist(Union[UtcDatetime, None], min_length=2, max_length=2), | ||
| min_length=1, | ||
| ), | ||
| AfterValidator(validate_time_interval), | ||
| ] | ||
|
||
|
|
||
|
|
||
| class Extent(StacBaseModel): | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved ☝️ to
shared