Repository navigation
fix: reject duplicate variants in strict BCP47 tags - #1678
Conversation
Co-Authored-By: OpenAI GPT-6.1 <noreply@openai.com>
|
|
||
| import "testing" | ||
|
|
||
| func BenchmarkBCP47Variants(b *testing.B) { |
There was a problem hiding this comment.
this function should be in https://github.com/go-playground/validator/blob/master/benchmarks_test.go
Please move it there.
|
|
||
| import "testing" | ||
|
|
||
| func TestBCP47StrictDuplicateVariants(t *testing.T) { |
There was a problem hiding this comment.
This function should be in https://github.com/go-playground/validator/blob/master/validator_test.go
Please move it there
Move TestBCP47StrictDuplicateVariants into validator_test.go and BenchmarkBCP47Variants into benchmarks_test.go per review feedback. Prepared with AI assistance.
|
Moved TestBCP47StrictDuplicateVariants into validator_test.go and BenchmarkBCP47Variants into benchmarks_test.go, and removed the separate files (commit 231a941). Verified: Prepared with AI assistance. |
|
Moved in 231a941: BenchmarkBCP47Variants now lives in benchmarks_test.go and TestBCP47StrictDuplicateVariants in validator_test.go, with the standalone files removed. Full suite passes locally (go test ./...) and CI is green on that head. Prepared with AI assistance. |
Fixes Or Enhances
Fixes #1677. The strict BCP47 validator checks that each variant is registered but did not check whether the same variant already occurred in the tag. RFC5646 section2.2.5 item5 explicitly prohibits repeated variants and gives
de-DE-1901-1901as invalid: https://www.rfc-editor.org/rfc/rfc5646.html#section-2.2.5.Track variants in a local set inside the existing variant loop. The input has already been lowercased, so mixed-case repeats are rejected too. Six production lines; no changes to the non-strict validator,variant-prefix recommendations,private-use subtags or registry data.
Five duplicate-variant subtests fail on unchanged source and five controls pass. All ten pass after the fix. Controls retain distinct variants,mixed-case distinct variants,and repeated content in private-use subtags. Added a focused benchmark and one documentation sentence. There is no repository changelog file/convention requiring a separate entry.
Local validation
Linuxamd64,Go1.27.3 and Go1.26.10,baseline a5843c6 -> head5c0c4d5:
go test -run 'TestBCP47.*' -count=1 -v:passes existing strict/non-strict tests and all ten new subtests.go test -race -covermode=atomic -coverprofile=... ./...:passes all packages on Go1.27.3; rootpackage97.5%statementcoverage.go test ./...:passes all packages on Go1.26.10. Untouched baseline Go1.27.3 full suite also passes.go test -race -run TestBCP47StrictDuplicateVariants -count=20:passes.go vet ./...:passes on Go1.27.3.gofmtandgit diff --check:pass.Benchmark
Eight interleaved fresh-process baseline/patch samples,200ms each case,Go1.27.3:
en-US:baseline median492.30ns[448.3–551.9] -> patch499.75[487.1–614.0].sl-rozaj:615.60ns[578.1–665.6] ->644.05[600.0–676.5].sl-rozaj-biske:692.55ns[631.6–843.3] ->746.55[671.3–831.2].Valid-input ranges overlap,with sample-median increases around1.5%/4.6%/7.8%; this is not a proof of performance neutrality. Allocations for valid one/twovariant cases remain3/op in both arms. The local set does not affect tags without variants. No unresolved API/design choice; the extra check implements the normative MUSTNOT requirement.
AI attribution
Prepared with AI assistance for analysis, implementation, tests, benchmarks and this description. No human code review is claimed. The commit retains the attribution required by this repository.