Skip to content
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

Push stac-check linting messages into stac-validator messaging format #201

Merged
merged 18 commits into from
Mar 15, 2022
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/)

## Unreleased - 2022-03-14

### Added

- Added core linting messages from stac-check to stac-validator messages https://github.com/stac-utils/stac-validator/pull/201/

### Fixed

- Reordered exception handlers to avoid unreachable code
- Details about invalid items is shown in the message when in recursive mode
- Reordered exception handlers to avoid unreachable code https://github.com/stac-utils/stac-validator/pull/203/
- Details about invalid items are shown in the message when in recursive mode https://github.com/stac-utils/stac-validator/pull/202/
- Dockerfile - change cli command from stac_validator to stac-validator https://github.com/stac-utils/stac-validator/pull/201/

## [v3.0.0] - 2022-03-11

Expand Down
49 changes: 42 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Validate STAC json files against the [STAC spec](https://github.com/radiantearth/stac-spec).

```bash
stac_validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/extended-item.json
stac-validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/extended-item.json
[
{
"version": "1.0.0",
Expand Down Expand Up @@ -130,8 +130,8 @@ Options:
The validator can run using docker containers.

```bash
docker build -t stac_validator:2.2.0 .
docker run stac_validator:2.2.0 https://raw.githubusercontent.com/stac-extensions/projection/main/examples/item.json
docker build -t stac-validator .
docker run stac-validator https://raw.githubusercontent.com/stac-extensions/projection/main/examples/item.json
[
{
"version": "1.0.0",
Expand Down Expand Up @@ -221,7 +221,7 @@ See the [tests](./tests/test_stac_validator.py) files for examples on different
**--core**

```bash
stac_validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/extended-item.json --core
stac-validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/extended-item.json --core
[
{
"version": "1.0.0",
Expand All @@ -239,7 +239,7 @@ stac_validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/e
**--custom**

```bash
stac_validator https://radarstac.s3.amazonaws.com/stac/catalog.json --custom https://cdn.staclint.com/v0.7.0/catalog.json
stac-validator https://radarstac.s3.amazonaws.com/stac/catalog.json --custom https://cdn.staclint.com/v0.7.0/catalog.json
[
{
"version": "0.7.0",
Expand All @@ -257,7 +257,7 @@ stac_validator https://radarstac.s3.amazonaws.com/stac/catalog.json --custom htt
**--extensions**

```bash
stac_validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/extended-item.json --extensions
stac-validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/extended-item.json --extensions
[
{
"version": "1.0.0",
Expand All @@ -276,10 +276,45 @@ stac_validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/e
]
```

**--lint**

```bash
stac-validator https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/extended-item.json --lint
[
{
"version": "1.0.0",
"path": "https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/extended-item.json",
"schema": [
"https://stac-extensions.github.io/eo/v1.0.0/schema.json",
"https://stac-extensions.github.io/projection/v1.0.0/schema.json",
"https://stac-extensions.github.io/scientific/v1.0.0/schema.json",
"https://stac-extensions.github.io/view/v1.0.0/schema.json",
"https://stac-extensions.github.io/remote-data/v1.0.0/schema.json",
"https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json"
],
"valid_stac": true,
"asset_type": "ITEM",
"validation_method": "default",
"linting": {
"searchable_identifiers": [
"Item name '20201211_223832_CS2' should only contain Searchable identifiers",
"Identifiers should consist of only lowercase characters, numbers, '_', and '-'"
],
"check_item_id": [
"Item file names should match their ids: 'extended-item' not equal to '20201211_223832_CS2"
],
"bloated_metadata": [
"You have 21 properties. Please consider using links to avoid bloated metadata"
]
}
}
]
```

**--recursive**

```bash
stac_validator https://spot-canada-ortho.s3.amazonaws.com/catalog.json --recursive --max-depth 1 --verbose
stac-validator https://spot-canada-ortho.s3.amazonaws.com/catalog.json --recursive --max-depth 1 --verbose
[
{
"version": "0.8.1",
Expand Down
13 changes: 13 additions & 0 deletions stac_validator/lint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from stac_check.lint import Linter # type: ignore


class StacCheck:
def __init__(
self,
stac_file: str = None,
):
self.stac_file = stac_file

def lint_message(self):
linter = Linter(self.stac_file)
return linter.create_best_practices_dict()
45 changes: 23 additions & 22 deletions stac_validator/stac_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import sys

import click # type: ignore
from stac_check.cli import cli_message as lint_message # type: ignore
from stac_check.lint import Linter # type: ignore

from .lint import StacCheck
from .validate import StacValidate


Expand Down Expand Up @@ -73,27 +72,29 @@ def main(
):

valid = True
if lint is True:
linter = Linter(stac_file, assets=True, links=True, recursive=False)
lint_message(linter)
else:
stac = StacValidate(
stac_file=stac_file,
recursive=recursive,
max_depth=max_depth,
core=core,
links=links,
assets=assets,
extensions=extensions,
custom=custom,
verbose=verbose,
no_output=no_output,
log=log_file,
)
valid = stac.run()
stac = StacValidate(
stac_file=stac_file,
recursive=recursive,
max_depth=max_depth,
core=core,
links=links,
assets=assets,
extensions=extensions,
custom=custom,
verbose=verbose,
no_output=no_output,
log=log_file,
)
valid = stac.run()

if no_output is False:
click.echo(json.dumps(stac.message, indent=4))
message = stac.message

if lint and not recursive:
linter = StacCheck(stac_file=stac_file)
message[0]["linting"] = linter.lint_message()

if no_output is False:
click.echo(json.dumps(message, indent=4))

sys.exit(0 if valid else 1)

Expand Down