Add bcp47_strict_language_tag validator#1489
Add bcp47_strict_language_tag validator#1489bfabio wants to merge 3 commits intogo-playground:masterfrom
Conversation
ba8f1be to
505d53f
Compare
Add strict BCP47 language tag validator enforcing RFC5646 and rejecting Unicode extensions unlike language.Parse(). Fix go-playground#1221.
505d53f to
8a96018
Compare
|
@nodivbyzero rebased |
The `go-playground/validator` bcp47_language_tag validator accepted tags that are not valid BCP47: go-playground/validator#1221. Use the strict validator from go-playground/validator#1489 locally until it lands upstream. Fix #47.
go-playground/validator's bcp47_language_tag accepted 3-letter ISO 639-2 codes (ita, fra, deu) and POSIX underscore tags (en_GB, hr_HR), which are not valid BCP47. validators/bcp47.go is a local copy of go-playground/validator#1489 (not yet merged) and should be removed once that lands. Fix #47.
go-playground/validator's bcp47_language_tag accepted 3-letter ISO 639-2 codes (ita, fra, deu) and POSIX underscore tags (en_GB, hr_HR), which are not valid BCP47. validators/bcp47.go is a local copy of go-playground/validator#1489 (not yet merged) and should be removed once that lands. Fix #47.
baked_in.go
Outdated
| field := fl.Field() | ||
|
|
||
| if field.Kind() == reflect.String { | ||
| var languageTagRe = regexp.MustCompile(strings.Join([]string{ |
There was a problem hiding this comment.
This regex compiled every call. That means compiling a huge regex every invocation, which is very expensive.
It's better to move it to a package-level var.
|
@bfabio Thanks for contributing! Feels like
Something like that: Also, is it possible to completely replace regex? and then validate against IANA constraints manually. |
|
Thanks for the review
Done
I refactored it a little bit more so they're not necessary. I don't even know why I did upcase it, but it was a while ago 🤷
Done.
I'm not sure what you mean here, a custom parser? |
Add strict BCP47 language tag validator enforcing RFC5646 and rejecting Unicode extensions unlike language.Parse().
Fixes Or Enhances
Fix #1221.
Make sure that you've checked the boxes below before you submit PR:
@go-playground/validator-maintainers