Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -3347,7 +3347,17 @@ func isBCP47StrictLanguageTag(fl FieldLevel) bool {
return false
}

// RFC 5646 section 2.2.2: extlang = 3ALPHA *("-" 3ALPHA) ; each at most once.
// A repeated extlang subtag is invalid, like the duplicate variants
// rejected below. The input has already been lowercased, so
// mixed-case repeats are rejected too.
seen := make(map[string]struct{})
for _, e := range parts[1:] {
if _, duplicate := seen[e]; duplicate {
return false
}
seen[e] = struct{}{}

prefixes, ok := iana_subtag_registry_extlangs[e]
if !ok {
return false
Expand Down
1 change: 1 addition & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,7 @@ More information on https://pkg.go.dev/golang.org/x/text/language
This validates that a string value is a valid BCP 47 language tag strictly following RFC 5646 rules,
unlike language.Parse which also accepts Unicode extensions.
Variant subtags cannot be repeated within the same tag (case-insensitively).
Extended language subtags cannot be repeated either (RFC 5646 section 2.2.2: each at most once).
see https://www.rfc-editor.org/rfc/bcp/bcp47.txt

Usage: bcp47_strict_language_tag
Expand Down
25 changes: 25 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17378,6 +17378,31 @@ func TestBCP47StrictDuplicateVariants(t *testing.T) {
}
}

func TestBCP47StrictDuplicateExtlangs(t *testing.T) {
validate := New()
// RFC 5646 section 2.2.2: extlang = 3ALPHA *("-" 3ALPHA) ; each at most once.
// A repeated extlang subtag must be rejected, like the duplicate variants
// fixed in #1678.
for _, value := range []string{
"zh-cmn-cmn", "zh-yue-yue", "zh-CMN-cmn", "ar-arb-arb",
} {
t.Run(value, func(t *testing.T) {
if err := validate.Var(value, "bcp47_strict_language_tag"); err == nil {
t.Errorf("duplicate extlang accepted: %s", value)
}
})
}
for _, value := range []string{
"zh-cmn-yue", "zh-CMN-yue", "ar-arb", "zh-cmn",
} {
t.Run(value, func(t *testing.T) {
if err := validate.Var(value, "bcp47_strict_language_tag"); err != nil {
t.Errorf("valid tag rejected: %s: %v", value, err)
}
})
}
}

func TestRequiredIfFloat64FieldValidation(t *testing.T) {
type Test struct {
Price float64
Expand Down
Loading