asaskevich / govalidator

[Go] Package of validators and sanitizers for strings, numerics, slices and structs
MIT License
6.05k stars 555 forks source link

suspicious regexp.Compile usage in utils.go #321

Open quasilyte opened 5 years ago

quasilyte commented 5 years ago
./github.com/asaskevich/govalidator/utils.go:156:21: regexpMust: for const patterns like `[ &_=+:]`, use regexp.MustCompile
./github.com/asaskevich/govalidator/utils.go:160:16: regexpMust: for const patterns like `[^[:alnum:]-.]`, use regexp.MustCompile

https://github.com/asaskevich/govalidator/blob/f9ffefc3facfbe0caee3fea233cbb6e8208f4541/utils.go#L156-L159

Since the pattern is constant, one might want to use regexp.MustCompile there and avoid the awkward err == nil branch. If there is an error in pattern, it should be fixed inside a source code.

As a side note, regexp compilation should probably be moved to outside of the function if it's called multiple times. Compilation is a slow operation for regexp pacakge.

sergeyglazyrindev commented 3 years ago

Hello guys! I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back Link to my repo: create issue there and we'll discuss it.