Skip to content

New() function

This function allows you to create a new Validation session without a Validator. This is useful for conditional validation or reusing validation logic.

The function accepts an optional parameter of type [Options] struct, which allows you to specify options such as the specific locale code and locale to use, and a custom JSON marshaler for errors.

The following example conditionally adds a Validator rule for the month_day value.

month := 5
monthDay := 11
val := v.New()
if month == 6 {
val.Is(v.Number(monthDay, "month_day").LessOrEqualTo(10))
}
if val.Valid() {
fmt.Println("The validation passes")
}

output:

Terminal window
The validation passes

As we mentioned above, you can pass the Options type to the New() function, in order to specify additional options when creating a new Validation session, such as the specific locale code and locale to use, and a custom JSON marshaler for errors. More information about the Options parameter in the following sections.