Skip to content

Commit

Permalink
Sort error keys before returning string.
Browse files Browse the repository at this point in the history
  • Loading branch information
NullHypothesis committed Oct 20, 2024
1 parent 3bc98a8 commit a601ffc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 15 additions & 3 deletions internal/util/validator.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
package util

import "context"
import (
"context"
"slices"
)

func SprintErrs(errs map[string]string) string {
var s string
for field, problem := range errs {
s += field + ": " + problem + "\n"

// Sort the error keys.
errKeys := []string{}
for key := range errs {
errKeys = append(errKeys, key)
}
slices.Sort(errKeys)

for _, key := range errKeys {
s += key + ": " + errs[key] + "\n"
}

return s
}

Expand Down
10 changes: 6 additions & 4 deletions internal/util/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ func TestSpringErrs(t *testing.T) {
want: "foo: bar\n",
},
{
name: "multiple errors",
name: "sorted errors",
errs: map[string]string{
"foo": "bar",
"baz": "qux",
"a": "foo",
"b": "bar",
"c": "baz",
"d": "qux",
},
want: "foo: bar\nbaz: qux\n",
want: "a: foo\nb: bar\nc: baz\nd: qux\n",
},
}

Expand Down

0 comments on commit a601ffc

Please sign in to comment.