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