diff --git a/README.md b/README.md index 6c88537..32fc641 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,21 @@ func (v valuer) Value() (sql.Value, error) { return v.Name, nil } +// ValidateValuerType implements validator.CustomTypeFunc +func ValidateValuerType(field reflect.Value) interface{} { + if valuer, ok := field.Interface().(sql.Valuer); ok { + val, err := valuer.Value() + if err != nil { + // handle the error how you want + return nil + } + + return val + } + + return nil +} + func main() { customTypes := map[reflect.Type]validator.CustomTypeFunc{} diff --git a/examples/simple.go b/examples/simple.go index fd503c4..b685dda 100644 --- a/examples/simple.go +++ b/examples/simple.go @@ -118,6 +118,21 @@ func (v valuer) Value() (sql.Value, error) { return v.Name, nil } +// ValidateValuerType implements validator.CustomTypeFunc +func ValidateValuerType(field reflect.Value) interface{} { + if valuer, ok := field.Interface().(sql.Valuer); ok { + val, err := valuer.Value() + if err != nil { + // handle the error how you want + return nil + } + + return val + } + + return nil +} + func main2() { customTypes := map[reflect.Type]validator.CustomTypeFunc{}