Adding proper error handling.

main
Nick Dumas 2 years ago
parent d421ca4995
commit b6b4eda80b

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

@ -2,7 +2,6 @@ package obp
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"github.com/santhosh-tekuri/jsonschema/v5" "github.com/santhosh-tekuri/jsonschema/v5"
@ -15,23 +14,22 @@ func Validate(schemaURL, filename string) error {
target, err := os.Open(filename) target, err := os.Open(filename)
if err != nil { 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) dec := yaml.NewDecoder(target)
err = dec.Decode(&m) err = dec.Decode(&m)
if err != nil { if err != nil {
log.Fatalf("error decoding YAML: %s\n", err) return fmt.Errorf("error decoding YAML: %w", err)
} }
compiler := jsonschema.NewCompiler() compiler := jsonschema.NewCompiler()
schema, err := compiler.Compile(schemaURL) schema, err := compiler.Compile(schemaURL)
if err != nil { 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 { 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 return nil
} }

Loading…
Cancel
Save