Files
cloud-services/pkg/validator/dates.go

32 lines
1017 B
Go

package validator
import (
"regexp"
"github.com/go-playground/validator/v10"
)
const iso8601DateRegexString = "^(?:[1-9]\\d{3}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1\\d|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[1-9]\\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)-02-29)T(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d{1,9})?(?:Z|[+-][01]\\d:[0-5]\\d)$"
const yyyyMMDDDateRegexString = `^\d{4}\-\d{2}\-\d{2}$`
var iso8601DateRegex = regexp.MustCompile(iso8601DateRegexString)
var yyyyMMDDDateRegex = regexp.MustCompile(yyyyMMDDDateRegexString)
func IsISO8601Date(fl validator.FieldLevel) bool {
value := fl.Field().String()
// skip if blank. if required, add required tag
if len(value) == 0 {
return true
}
return iso8601DateRegex.MatchString(value)
}
func IsDateYYYYMMDD(fl validator.FieldLevel) bool {
value := fl.Field().String()
// skip if blank. if required, add required tag
if len(value) == 0 {
return true
}
return yyyyMMDDDateRegex.MatchString(value)
}