Skip to content

Validation.Valid() function

A Validation session provide this function, which returns either true if all their validators are valid or false if any one of them is invalid.

In the following example, even though the Validator for age is valid, the Validator for status is invalid, making the entire Validator session invalid.

val := v.Is(
v.Number(21, "age").GreaterThan(18),
v.String("singl", "status").InSlice([]string{"married", "single"}),
)
if !val.Valid() {
out, _ := json.MarshalIndent(val.Error(), "", " ")
fmt.Println(string(out))
}

output:

{
"status": [
"Status is not valid"
]
}