also working on unit tests

main
Nick Dumas 2 years ago
parent 1dcd39afa3
commit 54e60b113e

@ -2,6 +2,7 @@ package obp
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
@ -10,35 +11,39 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
var (
ErrUnsupportedOutputFormat = errors.New("unspported output format")
)
// Validate accepts a Markdown file as input via the Reader // Validate accepts a Markdown file as input via the Reader
// and parses the frontmatter present, if any. It then // and parses the frontmatter present, if any. It then
// applies the schema fetched from schemaURL against the // applies the schema fetched from schemaURL against the
// decoded YAML. // decoded YAML.
func Validate(schemaURL string, r io.Reader) error { func Validate(schemaURL string, r io.Reader) error {
var m interface{} var frontmatter interface{}
dec := yaml.NewDecoder(r) dec := yaml.NewDecoder(r)
err := dec.Decode(&m)
err := dec.Decode(&frontmatter)
if err != nil { if err != nil {
return fmt.Errorf("error decoding YAML: %w", 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 {
return fmt.Errorf("error compiling schema: %w", err) return fmt.Errorf("error compiling schema: %w", err)
} }
if err := schema.Validate(m); err != nil {
return err
}
return nil return schema.Validate(frontmatter)
} }
func recurseDetails(detailed jsonschema.Detailed, acc map[string]jsonschema.Detailed) map[string]jsonschema.Detailed { func recurseDetails(detailed jsonschema.Detailed, acc map[string]jsonschema.Detailed) map[string]jsonschema.Detailed {
if detailed.Error != "" { if detailed.Error != "" {
acc[detailed.AbsoluteKeywordLocation] = detailed acc[detailed.AbsoluteKeywordLocation] = detailed
} }
for _, e := range detailed.Errors { for _, e := range detailed.Errors {
acc = recurseDetails(e, acc) acc = recurseDetails(e, acc)
} }
@ -49,29 +54,30 @@ func recurseDetails(detailed jsonschema.Detailed, acc map[string]jsonschema.Deta
// PrettyDetails takes error output from jsonschema.Validate // PrettyDetails takes error output from jsonschema.Validate
// and pretty-prints it to stdout. // and pretty-prints it to stdout.
// //
// Supported formats are: JSON, Markdown // Supported formats are: JSON, Markdown.
func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed, filename string) error { func PrettyDetails(writer io.Writer, format string, details jsonschema.Detailed, filename string) error {
// acc := make([]jsonschema.Detailed, 0) // acc := make([]jsonschema.Detailed, 0)
acc := make(map[string]jsonschema.Detailed) acc := make(map[string]jsonschema.Detailed)
errors := recurseDetails(details, acc) errors := recurseDetails(details, acc)
switch format { switch format {
case "json": case "json":
enc := json.NewEncoder(w) enc := json.NewEncoder(writer)
err := enc.Encode(details) err := enc.Encode(details)
if err != nil { if err != nil {
return fmt.Errorf("error writing JSON payload to provided writer: %w", err) return fmt.Errorf("error writing JSON payload to provided writer: %w", err)
} }
case "markdown": case "markdown":
fmt.Fprintf(w, "# Validation Errors for %q\n", filename) fmt.Fprintf(writer, "# Validation Errors for %q\n", filename)
fmt.Fprintf(w, "Validation Rule|Failing Property|Error\n") fmt.Fprintf(writer, "Validation Rule|Failing Property|Error\n")
fmt.Fprintf(w, "--|---|---\n") fmt.Fprintf(writer, "--|---|---\n")
for _, e := range errors { for _, e := range errors {
fmt.Fprintf(w, "%s|%s|%s\n", e.KeywordLocation, e.InstanceLocation, e.Error) fmt.Fprintf(writer, "%s|%s|%s\n", e.KeywordLocation, e.InstanceLocation, e.Error)
} }
default: default:
return fmt.Errorf("unknown format") return ErrUnsupportedOutputFormat
} }
return nil return nil

@ -1,15 +1,18 @@
package obp package obp_test
import ( import (
"testing" "testing"
) )
func Test_BasicValidation(t *testing.T) { func Test_BasicValidation(t *testing.T) {
t.Parallel()
t.Run("KeyMissing", func(t *testing.T) { t.Run("KeyMissing", func(t *testing.T) {
t.Parallel()
t.Fail() t.Fail()
}) })
t.Run("KeyTypeMismatch", func(t *testing.T) { t.Run("KeyTypeMismatch", func(t *testing.T) {
t.Parallel()
t.Fail() t.Fail()
}) })
} }

Loading…
Cancel
Save