[Bug]: Numeric arrays pass string validations
What happened?
Array of integers pass validations for strings.
Tested with all string tags validation.
Go playground with all strings validations tested (they all pass) https://go.dev/play/p/cky_4b8iONw
Maybe I misinterpreted something, or this is a strange bug?
Version
Tested with 2 versions:
- 10.22.0
- 10.28.0
Example Code
package main
import (
"log"
"github.com/go-playground/validator/v10"
)
func main() {
validate := validator.New(validator.WithRequiredStructEnabled())
arr := []int{3000}
tagsToTest := [...]string{
"alpha",
"alphaspace",
"alphanum",
"alphanumunicode",
"alphaunicode",
"ascii",
"boolean",
"contains",
"containsany",
"containsrune",
"endsnotwith",
"endswith",
"excludes",
"excludesall",
"excludesrune",
"lowercase",
"multibyte",
"number",
"numeric",
"printascii",
"startsnotwith",
"startswith",
"uppercase",
}
for i := range tagsToTest {
tag := tagsToTest[i]
if err := validate.Var(arr, "printascii"); err != nil {
log.Printf("%s: NOT PASSED - %s\n", tag, err.Error())
} else {
log.Printf("%s: PASSED\n", tag)
}
}
}
@luckv It looks like your example loops through the tagsToTest list, but ends up testing only the printascii tag.
if err := validate.Var(arr, "printascii"); err != nil {
@nodivbyzero I'm so sorry, I forgot to pass that current tag of the loop in the validation.
Only ascii and printascii present this bug and pass integers arrays as valid strings.
I corrected the example code: https://go.dev/play/p/ket9OEcElO_M
@luckv Good catch!
isPrintableASCII and isASCII do have a bug and mishandles the input as an array.
A PR with a fix would be greatly appreciated.
I'll open a PR for that in a few days 👀