Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func isDateDDMMYY(date string) bool {
return regexDateDDMMYY.MatchString(date)
}

func isDateTimeIso8601(date string) bool {
return regexDateTimeIso8601.MatchString(date)
}

// isEmail check a email is valid or not
func isEmail(email string) bool {
return regexEmail.MatchString(email)
Expand Down
51 changes: 51 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,49 @@ var (
"2012/11/30": false,
"201/11/30": false,
}

_dateTimeIso8601 = inputs{
"2016-07-08T12:30:00Z": true,
"2016-07-08T12:30:00+0100": true,
"2020-09-10T12:30:00-0100": true,
"1990-10-04T23:40:01Z": true,
"2020-04-20T09:32:28+00:00": true,
"2020-04-20T09:32:28-12:45": true,
"2020-04-20T09:32:28+12:45": true,
"2020-04-20T18:30:00+00:00": true,
"2020-04-20T18:30:00Z": true,
"2020-04-20T22:30:00+0400": true,
"2020-04-20T11:30:00-07:00": true,
"2020-04-20T15:00:00-0330": true,
"2020-04-20T15:00:59-0330": true,
"9019-07-08T12:30:00Z": true,
"1212-12-12T12:12:12+1212": true,
"9999-09-09T09:09:09-0909": true,
"2000-01-03T12:30:01.99999Z": true,
"2000-01-03T12:30:01.999Z": true,
"2000-01-03T12:30:01.0Z": true,
"2099-02-02T12:30:02.000+0100": true,
"2050-03-01T12:30:03.123-0100": true,
"2022-09-30": false,
"20200420T093228Z": false,
"2016-07-08T12:30:00": false,
"2016-07-0812:30:00Z": false,
"201607-08T12:30:00Z": false,
"2016-0708T12:30:00+0100": false,
"2020-09-1012:30:00-0100": false,
"1990-10-04T2340:01Z": false,
"2020-04-20T09:3228+00:00": false,
"2020-04-20T09:32:2812:45": false,
"200-04-20T09:32:28+1245": false,
"2020-04-20T18:30:0000:00": false,
"2020-4-20T18:30:00+00:00": false,
"2020-04-2T18:30:00Z": false,
"2020-04-20T2:30:00+0400": false,
"2020-04-20T11:3:00-07:00": false,
"2020-04-20T15:00:0-0330": false,
"2000-01-03T12:30:01999Z": false,
"2000-01-03T12:30:01.Z": false,
}
_emailList = inputs{
"john@example.com": true,
"thedevsaddam@gmail.com": true,
Expand Down Expand Up @@ -247,6 +290,14 @@ func Test_IsDateDDMMYY(t *testing.T) {
}
}

func Test_IsDateTimeIso8601(t *testing.T) {
for d, s := range _dateTimeIso8601 {
if isDateTimeIso8601(d) != s {
t.Error("isDateTimeIso8806 failed to determine date!")
}
}
}

func Benchmark_IsDateDDMMYY(b *testing.B) {
for n := 0; n < b.N; n++ {
isDateDDMMYY("23-10-2014")
Expand Down
51 changes: 27 additions & 24 deletions regex_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,34 @@ const (
UUID4 string = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
// UUID5 represents regular expression for UUID version 5
UUID5 string = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
//
DateTimeISO8601 string = `(?m)\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2]\d|3[0-1])T(?:[0-1]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+|)(?:Z|(?:\+|\-)(?:\d{2}):?(?:\d{2}))`
)

var (
regexAlpha = regexp.MustCompile(Alpha)
regexAlphaDash = regexp.MustCompile(AlphaDash)
regexAlphaSpace = regexp.MustCompile(AlphaSpace)
regexAlphaNumeric = regexp.MustCompile(AlphaNumeric)
regexCreditCard = regexp.MustCompile(CreditCard)
regexCoordinate = regexp.MustCompile(Coordinate)
regexCSSColor = regexp.MustCompile(CSSColor)
regexDate = regexp.MustCompile(Date)
regexDateDDMMYY = regexp.MustCompile(DateDDMMYY)
regexDigits = regexp.MustCompile(Digits)
regexEmail = regexp.MustCompile(Email)
regexFloat = regexp.MustCompile(Float)
regexMacAddress = regexp.MustCompile(MacAddress)
regexNumeric = regexp.MustCompile(Numeric)
regexLatitude = regexp.MustCompile(Latitude)
regexLongitude = regexp.MustCompile(Longitude)
regexIP = regexp.MustCompile(IP)
regexIPV4 = regexp.MustCompile(IPV4)
regexIPV6 = regexp.MustCompile(IPV6)
regexURL = regexp.MustCompile(URL)
regexUUID = regexp.MustCompile(UUID)
regexUUID3 = regexp.MustCompile(UUID3)
regexUUID4 = regexp.MustCompile(UUID4)
regexUUID5 = regexp.MustCompile(UUID5)
regexAlpha = regexp.MustCompile(Alpha)
regexAlphaDash = regexp.MustCompile(AlphaDash)
regexAlphaSpace = regexp.MustCompile(AlphaSpace)
regexAlphaNumeric = regexp.MustCompile(AlphaNumeric)
regexCreditCard = regexp.MustCompile(CreditCard)
regexCoordinate = regexp.MustCompile(Coordinate)
regexCSSColor = regexp.MustCompile(CSSColor)
regexDate = regexp.MustCompile(Date)
regexDateDDMMYY = regexp.MustCompile(DateDDMMYY)
regexDigits = regexp.MustCompile(Digits)
regexEmail = regexp.MustCompile(Email)
regexFloat = regexp.MustCompile(Float)
regexMacAddress = regexp.MustCompile(MacAddress)
regexNumeric = regexp.MustCompile(Numeric)
regexLatitude = regexp.MustCompile(Latitude)
regexLongitude = regexp.MustCompile(Longitude)
regexIP = regexp.MustCompile(IP)
regexIPV4 = regexp.MustCompile(IPV4)
regexIPV6 = regexp.MustCompile(IPV6)
regexURL = regexp.MustCompile(URL)
regexUUID = regexp.MustCompile(UUID)
regexUUID3 = regexp.MustCompile(UUID3)
regexUUID4 = regexp.MustCompile(UUID4)
regexUUID5 = regexp.MustCompile(UUID5)
regexDateTimeIso8601 = regexp.MustCompile(DateTimeISO8601)
)
13 changes: 13 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,19 @@ func init() {
return nil
})

AddCustomRule("datetime_iso8601", func(field string, rule string, message string, value interface{}) error {
str := toString(value)

if !isDateTimeIso8601(str) {
if message != "" {
return errors.New(message)
}
return fmt.Errorf("The %s field must be a valid datetime ISO-8601 format. e.g: yyyy-mm-ddThh:mm:ss-0500 etc", field)
}

return nil
})

// Email check the provided field is valid Email
AddCustomRule("email", func(field string, rule string, message string, value interface{}) error {
str := toString(value)
Expand Down
6 changes: 3 additions & 3 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ func (v *Validator) ValidateStruct() url.Values {
if v.Opts.Request != nil {
panic(errRequestNotAccepted)
}
if v.Opts.Data != nil && reflect.TypeOf(v.Opts.Data).Kind() != reflect.Ptr {
panic(errRequirePtr)
}
if v.Opts.Data == nil {
panic(errRequireData)
}
if reflect.TypeOf(v.Opts.Data).Kind() != reflect.Ptr {
panic(errRequirePtr)
}

return v.internalValidateStruct()
}
Expand Down