In(...) function
The In(...) function executes one or more validators in a namespace, so the value names in the error result are prefixed with this namespace. This is useful for validating nested structures.
In the following example we are validating the Person and the nested Address structure. We can distinguish the errors of the nested Address structure in the error results.
type Address struct { Name string Street string}
type Person struct { Name string Address Address}
p := Person{"Bob", Address{"", "1600 Amphitheatre Pkwy"}}
val := v. Is(v.String(p.Name, "name").OfLengthBetween(4, 20)). In("address", v.Is( String(p.Address.Name, "name").Not().Blank(), String(p.Address.Street, "street").Not().Blank(), ))
if !val.Valid() { out, _ := json.MarshalIndent(val.Error(), "", " ") fmt.Println(string(out))}output:
{ "address.name": [ "Name can't be blank" ], "name": [ "Name must have a length between \"4\" and \"20\"" ]}