Using a map to guarantee uniqueness of error output

main
Nick Dumas 2 years ago
parent 8bfc7c5268
commit 0cd02774ed

@ -0,0 +1,9 @@
---
aliases: ["bad tag schema test case"]
title: "Bad Schema: Tags"
description: "this is a file with bad tags"
tags: [1, 2, 4]
---
## Textual Cartography
Aardwolf has a fairly active developer community, people who write and maintain plugins and try to map the game world and its contents.

@ -62,8 +62,11 @@ var validateCmd = &cobra.Command{
defer target.Close() defer target.Close()
err = obp.Validate(schema, target) err = obp.Validate(schema, target)
if err != nil { if err != nil {
details := err.(*jsonschema.ValidationError).DetailedOutput() details, ok := err.(*jsonschema.ValidationError)
obp.PrettyDetails(cmd.OutOrStdout(), viper.GetString("format"), details,absPath) if !ok {
return err
}
obp.PrettyDetails(cmd.OutOrStdout(), viper.GetString("format"), details.DetailedOutput(), absPath)
} }
return nil return nil
}) })

@ -31,20 +31,22 @@ func Validate(schemaURL string, r io.Reader) error {
return nil return nil
} }
func flattenDetails(detailed jsonschema.Detailed, acc []jsonschema.Detailed) []jsonschema.Detailed { func recurseDetails(detailed jsonschema.Detailed, acc map[string]jsonschema.Detailed) map[string]jsonschema.Detailed {
if detailed.Error != "" { if detailed.Error != "" {
acc = append(acc, detailed) acc[detailed.AbsoluteKeywordLocation] = detailed
} }
for _, e := range detailed.Errors { for _, e := range detailed.Errors {
acc = append(acc, flattenDetails(e, acc)...) acc = recurseDetails(e, acc)
} }
return acc return acc
} }
func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed, filename string) error { func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed, filename string) error {
acc := make([]jsonschema.Detailed, 0) // acc := make([]jsonschema.Detailed, 0)
errors := flattenDetails(details, acc) acc := make(map[string]jsonschema.Detailed)
errors := recurseDetails(details, acc)
switch format { switch format {
case "json": case "json":
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
@ -54,7 +56,7 @@ func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed, file
} }
case "markdown": case "markdown":
fmt.Fprintf(w, "# Validation Errors for %q\n", filename) fmt.Fprintf(w, "# Validation Errors for %q\n", filename)
fmt.Fprintf(w, "eyword Location|Instance Location|Error\n") fmt.Fprintf(w, "Keyword Location|Instance Location|Error\n")
fmt.Fprintf(w, "--|---|---\n") fmt.Fprintf(w, "--|---|---\n")
for _, e := range errors { for _, e := range errors {
fmt.Fprintf(w, "%s|%s|%s\n", e.KeywordLocation, e.InstanceLocation, e.Error) fmt.Fprintf(w, "%s|%s|%s\n", e.KeywordLocation, e.InstanceLocation, e.Error)

Loading…
Cancel
Save