From 798d64d8a5abb241609a75b5499bb3aff8e260b6 Mon Sep 17 00:00:00 2001 From: Antonio Pagano Date: Wed, 5 Feb 2020 12:15:39 -0500 Subject: [PATCH] adding interface for Errors --- form/bootstrap/form_for.go | 2 +- form/bootstrap/form_for_test.go | 18 +++++++++++++++++- form/errors.go | 5 +++++ form/form_for.go | 31 +++++++++++++++++-------------- go.mod | 1 + go.sum | 2 ++ 6 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 form/errors.go diff --git a/form/bootstrap/form_for.go b/form/bootstrap/form_for.go index 218b66c..514adad 100644 --- a/form/bootstrap/form_for.go +++ b/form/bootstrap/form_for.go @@ -6,7 +6,7 @@ import ( "github.com/gobuffalo/tags/v3" "github.com/gobuffalo/tags/v3/form" - "github.com/gobuffalo/validate/validators" + "github.com/gobuffalo/validate/v3/validators" ) //FormFor is the FormFor version for bootstrap diff --git a/form/bootstrap/form_for_test.go b/form/bootstrap/form_for_test.go index 0817d09..524e20c 100644 --- a/form/bootstrap/form_for_test.go +++ b/form/bootstrap/form_for_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/gobuffalo/tags/v3" - "github.com/gobuffalo/validate" + "github.com/gobuffalo/validate/v3" "github.com/stretchr/testify/require" ) @@ -159,6 +159,22 @@ func Test_CheckBox(t *testing.T) { r.Equal(`
`, l.String()) } +type CustomError struct{} + +func (ce CustomError) Get(key string) []string { + return []string{"My Custom Error"} +} + +func Test_InputError_CustomError(t *testing.T) { + r := require.New(t) + + errors := CustomError{} + + f := NewFormFor(struct{ Name string }{}, tags.Options{"errors": errors}) + l := f.InputTag("Name", tags.Options{"label": "Custom"}) + r.Equal(`
My Custom Error
`, l.String()) +} + func Test_InputError(t *testing.T) { r := require.New(t) diff --git a/form/errors.go b/form/errors.go new file mode 100644 index 0000000..4e35cd4 --- /dev/null +++ b/form/errors.go @@ -0,0 +1,5 @@ +package form + +type Errors interface { + Get(key string) []string +} diff --git a/form/form_for.go b/form/form_for.go index 02f846e..5a01d60 100644 --- a/form/form_for.go +++ b/form/form_for.go @@ -11,7 +11,7 @@ import ( "github.com/gobuffalo/flect" "github.com/gobuffalo/tags/v3" - "github.com/gobuffalo/validate" + "github.com/gobuffalo/validate/v3" "github.com/gofrs/uuid" ) @@ -24,7 +24,7 @@ type FormFor struct { name string dashedName string reflection reflect.Value - Errors *validate.Errors + Errors Errors } // NewFormFor creates a new Formfor with passed options, it also creates the id of the form from the struct name and adds errors if present. @@ -53,20 +53,23 @@ func NewFormFor(model interface{}, opts tags.Options) *FormFor { } } -func loadErrors(opts tags.Options) *validate.Errors { +func loadErrors(opts tags.Options) Errors { errors := validate.NewErrors() - if opts["errors"] != nil { - switch t := opts["errors"].(type) { - default: - fmt.Printf("Unexpected errors type %T, please\n", t) // %T prints whatever type t has - case map[string][]string: - errors = &validate.Errors{ - Errors: opts["errors"].(map[string][]string), - Lock: new(sync.RWMutex), - } - case *validate.Errors: - errors = opts["errors"].(*validate.Errors) + + if opts["errors"] == nil { + return errors + } + + switch t := opts["errors"].(type) { + default: + fmt.Printf("Unexpected errors type %T, please\n", t) // %T prints whatever type t has + case map[string][]string: + errors = &validate.Errors{ + Errors: opts["errors"].(map[string][]string), + Lock: new(sync.RWMutex), } + case Errors: + return opts["errors"].(Errors) } return errors diff --git a/go.mod b/go.mod index 089d17f..6ea013a 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/fatih/structs v1.1.0 github.com/gobuffalo/flect v0.2.0 github.com/gobuffalo/validate v2.0.4+incompatible + github.com/gobuffalo/validate/v3 v3.0.0 github.com/gofrs/uuid v3.2.0+incompatible github.com/onsi/ginkgo v1.12.0 // indirect github.com/onsi/gomega v1.9.0 // indirect diff --git a/go.sum b/go.sum index 1d797b0..69b265b 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/gobuffalo/flect v0.2.0 h1:EWCvMGGxOjsgwlWaP+f4+Hh6yrrte7JeFL2S6b+0hdM github.com/gobuffalo/flect v0.2.0/go.mod h1:W3K3X9ksuZfir8f/LrfVtWmCDQFfayuylOJ7sz/Fj80= github.com/gobuffalo/validate v2.0.4+incompatible h1:ZTxozrIw8qQ5nfhShmc4izjYPTsPhfdXTdhXOd5OS9o= github.com/gobuffalo/validate v2.0.4+incompatible/go.mod h1:N+EtDe0J8252BgfzQUChBgfd6L93m9weay53EWFVsMM= +github.com/gobuffalo/validate/v3 v3.0.0 h1:dF7Bg8NMF9Zv8bZvUMXYJXxZdj+eSZ8z/lGM7/jVFUE= +github.com/gobuffalo/validate/v3 v3.0.0/go.mod h1:HFpjq+AIiA2RHoQnQVTFKF/ZpUPXwyw82LgyDPxQ9r0= github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=