Adding proper error handling.

main
Nick Dumas 2 years ago
parent d421ca4995
commit b6b4eda80b

@ -31,10 +31,10 @@ var validateCmd = &cobra.Command{
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
schema := cmd.Flag("schema").Value.String()
target := cmd.Flag("target").Value.String()
obp.Validate(schema, target)
return obp.Validate(schema, target)
},
}

@ -2,7 +2,6 @@ package obp
import (
"fmt"
"log"
"os"
"github.com/santhosh-tekuri/jsonschema/v5"
@ -15,23 +14,22 @@ func Validate(schemaURL, filename string) error {
target, err := os.Open(filename)
if err != nil {
log.Fatalf("could not open target file: %s\n", err)
return fmt.Errorf("could not open target file: %w", err)
}
dec := yaml.NewDecoder(target)
err = dec.Decode(&m)
if err != nil {
log.Fatalf("error decoding YAML: %s\n", err)
return fmt.Errorf("error decoding YAML: %w", err)
}
compiler := jsonschema.NewCompiler()
schema, err := compiler.Compile(schemaURL)
if err != nil {
log.Fatalf("error compiling schema: %s\n", err)
return fmt.Errorf("error compiling schema: %w", err)
}
if err := schema.Validate(m); err != nil {
log.Fatalf("error validating: %#v\n", err)
return fmt.Errorf("error validating target %q: %w", filename, err)
}
fmt.Println("validation successfull")
return nil
}

Loading…
Cancel
Save