diff --git a/go.mod b/go.mod index 73860fa..9e70e27 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/go-playground/validator/v10 v10.17.0 github.com/golang-jwt/jwt/v5 v5.2.0 github.com/gorilla/mux v1.8.1 + github.com/huandu/xstrings v1.4.0 github.com/jellydator/ttlcache/v3 v3.1.1 github.com/mediocregopher/radix/v4 v4.1.4 github.com/spf13/cobra v1.8.0 diff --git a/go.sum b/go.sum index 25cbeac..6306915 100644 --- a/go.sum +++ b/go.sum @@ -64,6 +64,8 @@ github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslC github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU= +github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jellydator/ttlcache/v3 v3.1.1 h1:RCgYJqo3jgvhl+fEWvjNW8thxGWsgxi+TPhRir1Y9y8= diff --git a/internal/http/profiles.go b/internal/http/profiles.go index 7399030..04bf2cc 100644 --- a/internal/http/profiles.go +++ b/internal/http/profiles.go @@ -7,6 +7,7 @@ import ( "net/http" "github.com/gorilla/mux" + "github.com/huandu/xstrings" "go.opentelemetry.io/otel/metric" "go.uber.org/multierr" @@ -71,7 +72,16 @@ func (p *ProfilesApi) postProfileHandler(resp http.ResponseWriter, req *http.Req if err != nil { var v *profiles.ValidationError if errors.As(err, &v) { + // Manager returns ValidationError according to the struct fields names. + // They are uppercased, but otherwise the same as the names in the API. + // So to make them consistent it's enough just to make the first lowercased. + for field, errors := range v.Errors { + v.Errors[xstrings.FirstRuneToLower(field)] = errors + delete(v.Errors, field) + } + apiBadRequest(resp, v.Errors) + return } diff --git a/internal/http/profiles_test.go b/internal/http/profiles_test.go index d6e4207..9033058 100644 --- a/internal/http/profiles_test.go +++ b/internal/http/profiles_test.go @@ -101,7 +101,7 @@ func (t *ProfilesTestSuite) TestPostProfile() { t.Run("receive validation errors", func() { t.ProfilesManager.On("PersistProfile", mock.Anything, mock.Anything).Once().Return(&profiles.ValidationError{ Errors: map[string][]string{ - "mock": {"error1", "error2"}, + "Username": {"error1", "error2"}, }, }) @@ -116,7 +116,7 @@ func (t *ProfilesTestSuite) TestPostProfile() { body, _ := io.ReadAll(result.Body) t.JSONEq(`{ "errors": { - "mock": [ + "username": [ "error1", "error2" ]