-
Notifications
You must be signed in to change notification settings - Fork 4
/
date.go
30 lines (26 loc) · 783 Bytes
/
date.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package govalidator
import (
"time"
)
const (
// Date represents rule name which will be used to find the default error message.
Date = "date"
// DateMsg is the default error message format for fields with Date validation rule.
DateMsg = "%s has wrong date format"
)
// Date checks the value under validation to be a valid, non-relative date with give layout.
//
// Example:
//
// v := validator.New()
// v.Date("2006-01-02", "2024-03-09","birthdate", "birthdate must be a valid date in the format YYYY-MM-DD.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) Date(layout, d, field, msg string) Validator {
_, err := time.Parse(layout, d)
if err != nil {
v.check(false, field, v.msg(Date, msg, field))
}
return v
}