Skip to content

Commit

Permalink
Fix validation errors response during profile upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
erickskrauch committed Jun 3, 2024
1 parent 32d749f commit ce6e62a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
10 changes: 10 additions & 0 deletions internal/http/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/huandu/xstrings"
"go.opentelemetry.io/otel/metric"
"go.uber.org/multierr"

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions internal/http/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
})

Expand All @@ -116,7 +116,7 @@ func (t *ProfilesTestSuite) TestPostProfile() {
body, _ := io.ReadAll(result.Body)
t.JSONEq(`{
"errors": {
"mock": [
"username": [
"error1",
"error2"
]
Expand Down

0 comments on commit ce6e62a

Please sign in to comment.