You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package obp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
|
|
"github.com/santhosh-tekuri/jsonschema/v5"
|
|
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func Validate(schemaURL string, r io.Reader) error {
|
|
var m interface{}
|
|
|
|
dec := yaml.NewDecoder(r)
|
|
err := dec.Decode(&m)
|
|
if err != nil {
|
|
return fmt.Errorf("error decoding YAML: %w", err)
|
|
}
|
|
|
|
compiler := jsonschema.NewCompiler()
|
|
schema, err := compiler.Compile(schemaURL)
|
|
if err != nil {
|
|
return fmt.Errorf("error compiling schema: %w", err)
|
|
}
|
|
if err := schema.Validate(m); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func recurseDetails(detailed jsonschema.Detailed, acc []jsonschema.Detailed) []jsonschema.Detailed {
|
|
acc = append(acc, detailed)
|
|
for _, e := range detailed.Errors {
|
|
acc = append(acc, recurseDetails(e, acc)...)
|
|
}
|
|
|
|
return acc
|
|
}
|
|
|
|
func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed) error {
|
|
log.Printf("received format %q\n", format)
|
|
acc := make([]jsonschema.Detailed, 0)
|
|
errors := recurseDetails(details, acc)
|
|
switch format {
|
|
case "json":
|
|
enc := json.NewEncoder(w)
|
|
err := enc.Encode(details)
|
|
if err != nil {
|
|
return fmt.Errorf("error writing JSON payload to provided writer: %w", err)
|
|
}
|
|
case "markdown":
|
|
fmt.Fprintf(w, "# Validation errors for \n")
|
|
fmt.Fprintf(w, "Valid|KeywordLocation|AbsoluteKeywordLocation|Instance Location|Error")
|
|
fmt.Fprintf(w, "---|---|---|---|---")
|
|
for _, e := range errors {
|
|
fmt.Fprintf(w, "%t|%s|%s|%s|%s\n", e.Valid, e.KeywordLocation, e.AbsoluteKeywordLocation, e.InstanceLocation, e.Error)
|
|
}
|
|
default:
|
|
return fmt.Errorf("unknown format")
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|