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

Validate a single resource #200

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package validator

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
Expand Down Expand Up @@ -160,6 +161,27 @@ func (v *Validator) Validate(ctx context.Context, resource string) error {
})
}

// ValidateBytes validates a single STAC resource.
//
// The location is a URL or file path that represents the resource and will
// be used in any validation error.
func ValidateBytes(ctx context.Context, data []byte, location string) error {
resource := crawler.Resource{}
if err := json.Unmarshal(data, &resource); err != nil {
return fmt.Errorf("failed to parse data as JSON: %w", err)
}
v := New(&Options{NoRecursion: true})
info := &crawler.ResourceInfo{
Location: location,
Entry: location,
}
err := v.validate(resource, info)
if !errors.Is(err, crawler.ErrStopRecursion) {
return err
}
return nil
}

func (v *Validator) validate(resource crawler.Resource, info *crawler.ResourceInfo) error {
v.logger.Info("validating resource", "resource", info.Location)
version := resource.Version()
Expand Down
34 changes: 34 additions & 0 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,40 @@ func (s *Suite) TestValidCases() {
}
}

func (s *Suite) TestValidateBytes() {
cases := []string{
"v1.0.0-beta.2/LC08_L1TP_097073_20130319_20200913_02_T1.json",
"v1.0.0/catalog.json",
"v1.0.0/collection.json",
"v1.0.0/item.json",
"v1.0.0/catalog-with-item.json",
"v1.0.0/catalog-with-multiple-items.json",
"v1.0.0/item-eo.json",
}

ctx := context.Background()
for _, c := range cases {
s.Run(c, func() {
location := path.Join("testdata", "cases", c)
data, err := os.ReadFile(location)
s.Require().NoError(err)
s.NoError(validator.ValidateBytes(ctx, data, location))
})
}
}

func (s *Suite) TestValidateBytesInvalidItem() {
location := "testdata/cases/v1.0.0/item-missing-id.json"

data, readErr := os.ReadFile(location)
s.Require().NoError(readErr)
ctx := context.Background()

err := validator.ValidateBytes(ctx, data, location)
s.Require().Error(err)
s.Assert().True(strings.HasSuffix(fmt.Sprintf("%#v", err), "missing properties: 'id'"))
}

func (s *Suite) TestSchemaMap() {
v := validator.New(&validator.Options{
SchemaMap: map[string]string{
Expand Down