go-ozzo / ozzo-validation

An idiomatic Go (golang) validation package. Supports configurable and extensible validation rules (validators) using normal language constructs instead of error-prone struct tags.
MIT License
3.73k stars 224 forks source link

validate dates between fields #83

Closed rafa-acioly closed 4 years ago

rafa-acioly commented 4 years ago

I've tried to pass a string on the validation.Date() method and i'm getting this error:

end_at: must be either a string or byte slice; start_at: must be either a string or byte slice.

The struct:

type Price struct {
    StartAt      time.Time `bson:"start_at" json:"start_at"`
    EndAt        time.Time `bson:"end_at" json:"end_at"`
}

The validation:

func (p Price) isValid() error {
    return validation.ValidateStruct(&p,
        validation.Field(&p.StartAt, validation.Required, validation.Date("")),
        validation.Field(&p.EndAt, validation.Required, validation.Date("").Min(p.StartAt)),
    )
}

The payload:

{
    "end_at": "2019-10-01T00:00:00Z",
    "start_at": "2019-11-01T00:00:00Z"
}

the doc says:

An empty value is considered valid. Use the Required rule to make sure a value is not empty.

Can i set a min value using another field?

qiangxue commented 4 years ago

The Date rule is used to validate a date/time string. You can use Min and Max rules to validate the range of a time.Time value.