syft/internal/formats/syftjson/validator.go
Alex Goodman 5fb0235cfb
experiment with encoder/decoder for data encapsulation
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
2021-11-22 16:11:52 -05:00

35 lines
630 B
Go

package syftjson
import (
"encoding/json"
"fmt"
"io"
"strings"
"github.com/anchore/syft/internal/formats/syftjson/model"
)
type validator struct {
}
func (v validator) Validate(reader io.Reader) error {
type Document struct {
Schema model.Schema `json:"schema"`
}
dec := json.NewDecoder(reader)
var doc Document
err := dec.Decode(&doc)
if err != nil {
return fmt.Errorf("unable to decode: %w", err)
}
// note: we accept all schema versions
// TODO: add per-schema version parsing
if strings.Contains(doc.Schema.URL, "anchore/syft") {
return nil
}
return fmt.Errorf("could not extract syft schema")
}