-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Labels
Description
Package version eg. v8, v9:
v9
Issue, Question or Enhancement:
The custom validation tag appears to have no impact when it is used against a field that is of type struct. If the tag is not registered, the validator library panics as expected, however if the tag is registered and deliberately set to return false, I would expect validation to fail.
However it does not.
Is this intended behaviour? Can field validation not be performed in this way? If I was to use my custom tag against a standard system type, such as a string, the validation fails as expected.
Apologies if this is covered in the documentation somewhere.
Code sample, to showcase or reproduce:
package main
import (
"fmt"
"time"
validator "gopkg.in/go-playground/validator.v9"
)
// We want to validate the ValidUntil is equal to or after ValidFrom
type Record struct {
ValidFrom ExtendedDate `validate:"required"`
ValidUntil ExtendedDate `validate:"required,gteEpoch=ValidFrom"`
}
type ExtendedDate struct {
Epoch int64 `validate:"required"`
Iso6801 time.Time `validate:"required"`
}
var validate *validator.Validate
func main() {
record := Record{
ValidFrom: ExtendedDate{
Epoch: 1,
Iso6801: time.Now(),
},
ValidUntil: ExtendedDate{
Epoch: 1,
Iso6801: time.Now().AddDate(0, 0, -1),
},
}
validate = validator.New()
validate.RegisterValidation("gteEpoch", gteEpoch)
err := validate.Struct(record)
// This should fail, but it does not?
if err != nil {
fmt.Printf("%v\n", err.Error())
}
}
func gteEpoch(fl validator.FieldLevel) bool {
// Perform required validation checks here...
// Deliberately fail here for this example
return false
}noibar, alecthomas, upovod, micheam, pandwoter and 2 more