go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.64k stars 1.32k forks source link

Execute all validators and get the collection of corresponding results #1233

Open maocatooo opened 7 months ago

maocatooo commented 7 months ago

Package version:

v10

Issue, Question or Enhancement:

Question What should I do instead?

Code sample, to showcase or reproduce:

package main

import (
    "fmt"
    "github.com/go-playground/validator/v10"
)

type User struct {
    Email string `validate:"required,email,max=10"`
}

func main() {
    validate := validator.New()

    user := User{
        Email: "john11233exampleqwertyu",
    }

    err := validate.Struct(user)
    fmt.Println(len(err.(validator.ValidationErrors)) == 1 == true) // i need max error and email error
}
deankarn commented 7 months ago

@maocatooo unfortunately there is no automated way to achieve what you're looking for as validator is designed to return only the first failed validation, per field, and changing the package to be able to support that would require it to be completely redesigned from the ground up.

There may be a way to achieve what your looking for using Struct Level validations, but would require you to run validations individually and them cobble the results together, almost the same as if doing it outside of the package.