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" "io/fs"
"log" "log"
"os" "os"
"path/filepath"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader" _ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -53,6 +54,15 @@ var validateCmd = &cobra.Command{
} }
log.Printf("scanning %q\n", path) 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)
}) })
// return obp.Validate(schema, target) // return obp.Validate(schema, target)

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

Loading…
Cancel
Save