Fix validation errors response during profile upsert

This commit is contained in:
ErickSkrauch 2024-06-03 04:34:35 +02:00
parent 32d749f245
commit ce6e62ae5c
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
4 changed files with 15 additions and 2 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -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"
]