Skip to content

Commit

Permalink
Validation: remove suffix '[]' from n-dimensional array elements fiel…
Browse files Browse the repository at this point in the history
…d name in error messages
  • Loading branch information
System-Glitch committed Apr 11, 2024
1 parent 38f3fab commit 50092b9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 0 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ type Service interface {
// to retrieve the service.
Name() string
}

// TODO Document why Service, why repository, how they are initialized, etc
8 changes: 7 additions & 1 deletion validation/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,13 @@ func translateFieldName(lang *lang.Language, fieldName string) string {
if i := strings.LastIndex(fieldName, "."); i != -1 {
fieldName = fieldName[i+1:]
}
fieldName = strings.TrimSuffix(fieldName, "[]")
for {
f := strings.TrimSuffix(fieldName, "[]")
if len(f) == len(fieldName) {
break
}
fieldName = f
}
entry := "validation.fields." + fieldName
name := lang.Get(entry)
if name == entry {
Expand Down
4 changes: 2 additions & 2 deletions validation/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func TestValidate(t *testing.T) {
Elements: ArrayErrors{
0: &Errors{
Elements: ArrayErrors{
1: &Errors{Errors: []string{"The narray[] elements must be integers."}}, // TODO should the "[]" be completely removed?
1: &Errors{Errors: []string{"The narray elements must be integers."}},
},
},
},
Expand Down Expand Up @@ -641,7 +641,7 @@ func TestValidate(t *testing.T) {
Elements: ArrayErrors{
0: &Errors{
Elements: ArrayErrors{
1: &Errors{Errors: []string{"The narray[] elements must be integers."}},
1: &Errors{Errors: []string{"The narray elements must be integers."}},
2: &Errors{Errors: []string{"merge err"}},
},
},
Expand Down

0 comments on commit 50092b9

Please sign in to comment.