Validate now walks recursively

need to figure out a good way to collect the errors
main
Nick Dumas 2 years ago
parent b3f04d1452
commit dc453d7539

@ -8,6 +8,7 @@ import (
"io/fs"
"log"
"os"
"path/filepath"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"github.com/spf13/cobra"
@ -53,6 +54,15 @@ var validateCmd = &cobra.Command{
}
log.Printf("scanning %q\n", path)
absPath, err := filepath.Abs(filepath.Join(target, path))
if err != nil {
return fmt.Errorf("error generating absolute path for %q", target)
}
target, err := os.Open(absPath)
if err != nil {
return fmt.Errorf("could not open target file: %w", err)
}
defer target.Close()
return obp.Validate(schema, target)
})
// return obp.Validate(schema, target)

@ -2,22 +2,18 @@ package obp
import (
"fmt"
"os"
"io"
"github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"gopkg.in/yaml.v3"
)
func Validate(schemaURL, filename string) error {
func Validate(schemaURL string, r io.Reader) error {
var m interface{}
target, err := os.Open(filename)
if err != nil {
return fmt.Errorf("could not open target file: %w", err)
}
dec := yaml.NewDecoder(target)
err = dec.Decode(&m)
dec := yaml.NewDecoder(r)
err := dec.Decode(&m)
if err != nil {
return fmt.Errorf("error decoding YAML: %w", err)
}
@ -28,7 +24,7 @@ func Validate(schemaURL, filename string) error {
return fmt.Errorf("error compiling schema: %w", err)
}
if err := schema.Validate(m); err != nil {
return fmt.Errorf("error validating target %q: %w", filename, err)
return fmt.Errorf("error validating target: %w", err)
}
return nil

Loading…
Cancel
Save