Skip to content

Commit f512eeb

Browse files
renichLinkinStars
authored andcommitted
fix(validator): support regional locale fallback and multi-byte UTF-8 capitalization
- Match base language prefix in GetValidatorByLang when a regional locale (such as pt_BR or es_MX) is not pre-registered in GlobalValidatorMapping, preserving the active locale for field name translation via translator.Tr instead of falling back to en_US. - Slice field.ErrorMsg by rune index instead of byte index when capitalizing the initial Latin character in MyValidator.Check to prevent UTF-8 corruption on multi-byte accented characters. - Add unit tests covering regional locale fallback and multi-byte Latin capitalization. Co-authored-by: Antigravity <antigravity@google.com> Signed-off-by: Rénich Bon Ćirić <renich@evalinux.com>
1 parent 0584ca0 commit f512eeb

2 files changed

Lines changed: 121 additions & 5 deletions

File tree

‎internal/base/validator/validator.go‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"reflect"
2626
"strings"
27+
"sync"
2728
"unicode"
2829

2930
"github.com/apache/answer/internal/base/reason"
@@ -96,7 +97,10 @@ type FormErrorField struct {
9697
}
9798

9899
// GlobalValidatorMapping is a mapping from validator to translator used
99-
var GlobalValidatorMapping = make(map[i18n.Language]*MyValidator, 0)
100+
var (
101+
GlobalValidatorMapping = make(map[i18n.Language]*MyValidator, 0)
102+
validatorMappingMu sync.RWMutex
103+
)
100104

101105
func init() {
102106
for _, t := range allLanguageTranslators {
@@ -180,9 +184,44 @@ func createDefaultValidator(la i18n.Language) *validator.Validate {
180184
}
181185

182186
func GetValidatorByLang(lang i18n.Language) *MyValidator {
183-
if GlobalValidatorMapping[lang] != nil {
184-
return GlobalValidatorMapping[lang]
187+
validatorMappingMu.RLock()
188+
if v := GlobalValidatorMapping[lang]; v != nil {
189+
validatorMappingMu.RUnlock()
190+
return v
191+
}
192+
validatorMappingMu.RUnlock()
193+
194+
baseLang := string(lang)
195+
if idx := strings.IndexAny(baseLang, "_-"); idx > 0 {
196+
baseLang = baseLang[:idx]
185197
}
198+
199+
for _, t := range allLanguageTranslators {
200+
tBase := string(t.La)
201+
if idx := strings.IndexAny(tBase, "_-"); idx > 0 {
202+
tBase = tBase[:idx]
203+
}
204+
if strings.EqualFold(baseLang, tBase) {
205+
validatorMappingMu.Lock()
206+
defer validatorMappingMu.Unlock()
207+
if v := GlobalValidatorMapping[lang]; v != nil {
208+
return v
209+
}
210+
tran, val := getTran(t.Lo), createDefaultValidator(lang)
211+
if t.RegisterFunc != nil {
212+
if err := t.RegisterFunc(val, tran); err != nil {
213+
log.Error(err)
214+
break
215+
}
216+
}
217+
v := &MyValidator{Validate: val, Tran: tran, Lang: lang}
218+
GlobalValidatorMapping[lang] = v
219+
return v
220+
}
221+
}
222+
223+
validatorMappingMu.RLock()
224+
defer validatorMappingMu.RUnlock()
186225
return GlobalValidatorMapping[i18n.DefaultLanguage]
187226
}
188227

@@ -196,12 +235,16 @@ func (m *MyValidator) Check(value any) (errFields []*FormErrorField, err error)
196235
if len(field.ErrorField) == 0 {
197236
continue
198237
}
199-
firstRune := []rune(field.ErrorMsg)[0]
238+
runes := []rune(field.ErrorMsg)
239+
if len(runes) == 0 {
240+
continue
241+
}
242+
firstRune := runes[0]
200243
if !unicode.IsLetter(firstRune) || !unicode.Is(unicode.Latin, firstRune) {
201244
continue
202245
}
203246
upperFirstRune := unicode.ToUpper(firstRune)
204-
field.ErrorMsg = string(upperFirstRune) + field.ErrorMsg[1:]
247+
field.ErrorMsg = string(upperFirstRune) + string(runes[1:])
205248
if !strings.HasSuffix(field.ErrorMsg, ".") {
206249
field.ErrorMsg += "."
207250
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package validator
21+
22+
import (
23+
"strings"
24+
"testing"
25+
"unicode/utf8"
26+
27+
"github.com/segmentfault/pacman/i18n"
28+
"github.com/stretchr/testify/assert"
29+
"github.com/stretchr/testify/require"
30+
)
31+
32+
type sampleCommentReq struct {
33+
OriginalText string `validate:"required,notblank,gte=2,lte=5" json:"original_text"`
34+
}
35+
36+
type sampleAccentedFieldReq struct {
37+
Email string `validate:"required,email" json:"émail"`
38+
}
39+
40+
func TestGetValidatorByLang_RegionalLocaleFallback(t *testing.T) {
41+
req := &sampleCommentReq{OriginalText: "123456"}
42+
43+
esMXValidator := GetValidatorByLang(i18n.Language("es_MX"))
44+
require.NotNil(t, esMXValidator)
45+
assert.Equal(t, i18n.Language("es_MX"), esMXValidator.Lang)
46+
47+
errFields, err := esMXValidator.Check(req)
48+
require.Error(t, err)
49+
require.Len(t, errFields, 1)
50+
assert.Contains(t, errFields[0].ErrorMsg, "debe tener un máximo de 5")
51+
52+
ptBRValidator := GetValidatorByLang(i18n.Language("pt_BR"))
53+
require.NotNil(t, ptBRValidator)
54+
assert.Equal(t, i18n.Language("pt_BR"), ptBRValidator.Lang)
55+
56+
errFieldsPT, errPT := ptBRValidator.Check(req)
57+
require.Error(t, errPT)
58+
require.Len(t, errFieldsPT, 1)
59+
assert.Contains(t, errFieldsPT[0].ErrorMsg, "máximo")
60+
}
61+
62+
func TestCheck_MultiByteLatinCapitalization(t *testing.T) {
63+
req := &sampleAccentedFieldReq{Email: "not-an-email"}
64+
65+
esValidator := GetValidatorByLang(i18n.LanguageSpanish)
66+
errFields, err := esValidator.Check(req)
67+
require.Error(t, err)
68+
require.Len(t, errFields, 1)
69+
70+
msg := errFields[0].ErrorMsg
71+
assert.True(t, utf8.ValidString(msg), "error message must be valid UTF-8, got: %q", msg)
72+
assert.True(t, strings.HasPrefix(msg, "Émail "), "expected prefix 'Émail ', got: %q", msg)
73+
}

0 commit comments

Comments
 (0)