From f594d1d5564f420e25b94e80e4093eceb13c1d6a Mon Sep 17 00:00:00 2001 From: Aaron L Date: Mon, 30 Apr 2018 18:19:19 -0700 Subject: [PATCH] Add some validation helpers --- validation.go | 8 ++++++++ validation_test.go | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/validation.go b/validation.go index e715b0d..68e7018 100644 --- a/validation.go +++ b/validation.go @@ -18,12 +18,20 @@ type Validator interface { } // FieldError describes an error on a field +// Typically .Error() has both Name() and Err() together, hence the reason +// for separation. type FieldError interface { error Name() string Err() error } +// ErrorMap is a shortcut to change []error into ErrorList and call Map on it since +// this is a common operation. +func ErrorMap(e []error) map[string][]string { + return ErrorList(e).Map() +} + // ErrorList is simply a slice of errors with helpers. type ErrorList []error diff --git a/validation_test.go b/validation_test.go index 9fee83d..282f52b 100644 --- a/validation_test.go +++ b/validation_test.go @@ -78,3 +78,15 @@ func TestErrorList_Map(t *testing.T) { t.Error("Wrong unkown error at 0:", unknownErrs[0]) } } + +func TestErrorList_MapHelper(t *testing.T) { + t.Parallel() + + errList := []error{ + mockFieldError{"username", errors.New("")}, + mockFieldError{"username", errors.New("")}, + mockFieldError{"password", errors.New("")}, + } + + var _ map[string][]string = ErrorMap(errList) +}