diff --git a/cmd/obp/cmd/validate.go b/cmd/obp/cmd/validate.go index f3c5ff1..d973cce 100644 --- a/cmd/obp/cmd/validate.go +++ b/cmd/obp/cmd/validate.go @@ -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) diff --git a/validate.go b/validate.go index 4f51e28..2571932 100644 --- a/validate.go +++ b/validate.go @@ -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