Skip to content

Validator value's name and title

Validators only require the value to be validated, so, for example, the following code validates a string value by checking if it is empty.

val := v.New(v.String("").Empty())

val.Error() output:

{
"value_0": [
"Value 0 can't be empty",
]
}

In the example above, since we didn’t specify a name for the value, Valgo generates a value_0 name and consequently the Value 0 title in the error message.

However, Validators allow you, optionally, to specify the value’s name and title, as shown below:

Validator with value’s name:

val := v.New(v.String("", "company_name").Not().Empty())

val.Error() output:

{
"company_name": [
"Company name can't be empty",
]
}

Validator with value’s name and title:

val := v.New(v.String("", "company_name", "Customer").Not().Empty())

val.Error() output:

{
"company_name": [
"Customer can't be empty",
]
}