the following struct will raise err if "Field" does not exist

type Foo struct {
    Field time.Time `json:"field" binding:"required"`
}

but the following will not

type JSONDate time.Time
type Foo struct {
    Field JSONDate `json:"field" binding:"required"`
}

Comment From: ycdesu

Gin uses a third party lib validator.v8 to validate your fields. In the validator example, you have to register custom type validator to validate your own types. Here's the sample code.

The problem is, binding.StructValidator doesn't expose validator.RegisterCustomTypeFunc so you can't register your own validator yet.

Comment From: rodrigo-kayala

I think something like that should work:

func ValidateJSONDateType(field reflect.Value) interface{} {
    if field.Type() == reflect.TypeOf(JSONDate{}) {
        nilRef := time.Time{}
        if field.Interface().(JSONDate).Time == nilRef {
            return nil
        }
        return field.Interface().(JSONDate).Time
    }

    return nil
}

func main()  {
    r := gin.Default()

    if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
        v.RegisterCustomTypeFunc(ValidateJSONDateType, JSONDate{})
    }

        ...
}