validator icon indicating copy to clipboard operation
validator copied to clipboard

[Bug]: Numeric arrays pass string validations

Open luckv opened this issue 1 month ago • 4 comments

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 avatar Oct 30 '25 14:10 luckv

@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 avatar Nov 15 '25 17:11 nodivbyzero

@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 avatar Nov 15 '25 18:11 luckv

@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.

nodivbyzero avatar Nov 18 '25 01:11 nodivbyzero

I'll open a PR for that in a few days 👀

luk3skyw4lker avatar Nov 21 '25 01:11 luk3skyw4lker