mirror of
https://github.com/elyby/chrly.git
synced 2025-01-10 22:02:04 +05:30
Fix validation errors response during profile upsert
This commit is contained in:
parent
32d749f245
commit
ce6e62ae5c
1
go.mod
1
go.mod
@ -13,6 +13,7 @@ require (
|
|||||||
github.com/go-playground/validator/v10 v10.17.0
|
github.com/go-playground/validator/v10 v10.17.0
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.0
|
github.com/golang-jwt/jwt/v5 v5.2.0
|
||||||
github.com/gorilla/mux v1.8.1
|
github.com/gorilla/mux v1.8.1
|
||||||
|
github.com/huandu/xstrings v1.4.0
|
||||||
github.com/jellydator/ttlcache/v3 v3.1.1
|
github.com/jellydator/ttlcache/v3 v3.1.1
|
||||||
github.com/mediocregopher/radix/v4 v4.1.4
|
github.com/mediocregopher/radix/v4 v4.1.4
|
||||||
github.com/spf13/cobra v1.8.0
|
github.com/spf13/cobra v1.8.0
|
||||||
|
2
go.sum
2
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/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 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
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 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
github.com/jellydator/ttlcache/v3 v3.1.1 h1:RCgYJqo3jgvhl+fEWvjNW8thxGWsgxi+TPhRir1Y9y8=
|
github.com/jellydator/ttlcache/v3 v3.1.1 h1:RCgYJqo3jgvhl+fEWvjNW8thxGWsgxi+TPhRir1Y9y8=
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/huandu/xstrings"
|
||||||
"go.opentelemetry.io/otel/metric"
|
"go.opentelemetry.io/otel/metric"
|
||||||
"go.uber.org/multierr"
|
"go.uber.org/multierr"
|
||||||
|
|
||||||
@ -71,7 +72,16 @@ func (p *ProfilesApi) postProfileHandler(resp http.ResponseWriter, req *http.Req
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
var v *profiles.ValidationError
|
var v *profiles.ValidationError
|
||||||
if errors.As(err, &v) {
|
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)
|
apiBadRequest(resp, v.Errors)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ func (t *ProfilesTestSuite) TestPostProfile() {
|
|||||||
t.Run("receive validation errors", func() {
|
t.Run("receive validation errors", func() {
|
||||||
t.ProfilesManager.On("PersistProfile", mock.Anything, mock.Anything).Once().Return(&profiles.ValidationError{
|
t.ProfilesManager.On("PersistProfile", mock.Anything, mock.Anything).Once().Return(&profiles.ValidationError{
|
||||||
Errors: map[string][]string{
|
Errors: map[string][]string{
|
||||||
"mock": {"error1", "error2"},
|
"Username": {"error1", "error2"},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ func (t *ProfilesTestSuite) TestPostProfile() {
|
|||||||
body, _ := io.ReadAll(result.Body)
|
body, _ := io.ReadAll(result.Body)
|
||||||
t.JSONEq(`{
|
t.JSONEq(`{
|
||||||
"errors": {
|
"errors": {
|
||||||
"mock": [
|
"username": [
|
||||||
"error1",
|
"error1",
|
||||||
"error2"
|
"error2"
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user