From 0cd02774ed741ea768d0cc91591ac5fee73fd99f Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Wed, 7 Jun 2023 14:46:14 -0400 Subject: [PATCH] Using a map to guarantee uniqueness of error output --- Resources/blog/published/schema-bad-tags.md | 9 +++++++++ cmd/obp/cmd/validate.go | 7 +++++-- validate.go | 14 ++++++++------ 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 Resources/blog/published/schema-bad-tags.md diff --git a/Resources/blog/published/schema-bad-tags.md b/Resources/blog/published/schema-bad-tags.md new file mode 100644 index 0000000..834c01b --- /dev/null +++ b/Resources/blog/published/schema-bad-tags.md @@ -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. diff --git a/cmd/obp/cmd/validate.go b/cmd/obp/cmd/validate.go index 7cc7344..501c5c7 100644 --- a/cmd/obp/cmd/validate.go +++ b/cmd/obp/cmd/validate.go @@ -62,8 +62,11 @@ var validateCmd = &cobra.Command{ defer target.Close() err = obp.Validate(schema, target) if err != nil { - details := err.(*jsonschema.ValidationError).DetailedOutput() - obp.PrettyDetails(cmd.OutOrStdout(), viper.GetString("format"), details,absPath) + details, ok := err.(*jsonschema.ValidationError) + if !ok { + return err + } + obp.PrettyDetails(cmd.OutOrStdout(), viper.GetString("format"), details.DetailedOutput(), absPath) } return nil }) diff --git a/validate.go b/validate.go index bb80b32..bd1288b 100644 --- a/validate.go +++ b/validate.go @@ -31,20 +31,22 @@ func Validate(schemaURL string, r io.Reader) error { 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 != "" { - acc = append(acc, detailed) + acc[detailed.AbsoluteKeywordLocation] = detailed } for _, e := range detailed.Errors { - acc = append(acc, flattenDetails(e, acc)...) + acc = recurseDetails(e, acc) } return acc } func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed, filename string) error { - acc := make([]jsonschema.Detailed, 0) - errors := flattenDetails(details, acc) + // acc := make([]jsonschema.Detailed, 0) + acc := make(map[string]jsonschema.Detailed) + errors := recurseDetails(details, acc) + switch format { case "json": enc := json.NewEncoder(w) @@ -54,7 +56,7 @@ func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed, file } case "markdown": 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") for _, e := range errors { fmt.Fprintf(w, "%s|%s|%s\n", e.KeywordLocation, e.InstanceLocation, e.Error)