Skip to content

Commit

Permalink
Merge pull request #498 from shintarou37/master
Browse files Browse the repository at this point in the history
feat IsYYYYMMDD function
  • Loading branch information
asaskevich authored Jan 22, 2025
2 parents 5c3be24 + 192714b commit e113478
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func IsULID(str string) bool
func IsUnixTime(str string) bool
func IsUpperCase(str string) bool
func IsVariableWidth(str string) bool
func IsYYYYMMDD(str string) bool
func IsWhole(value float64) bool
func LeftTrim(str, chars string) string
func Map(array []interface{}, iterator ResultIterator) []interface{}
Expand Down Expand Up @@ -385,6 +386,7 @@ Here is a list of available validators for struct fields (validator - used funct
"ISO3166Alpha2": IsISO3166Alpha2,
"ISO3166Alpha3": IsISO3166Alpha3,
"ulid": IsULID,
"yyyymmdd": IsYYYYMMDD,
```
Validators with parameters

Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ var TagMap = map[string]Validator{
"ISO4217": IsISO4217,
"IMEI": IsIMEI,
"ulid": IsULID,
"yyyymmdd": IsYYYYMMDD,
}

// ISO3166Entry stores country codes
Expand Down
23 changes: 15 additions & 8 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,26 +454,26 @@ func IsCreditCard(str string) bool {
if !rxCreditCard.MatchString(sanitized) {
return false
}

number, _ := ToInt(sanitized)
number, lastDigit := number / 10, number % 10
number, lastDigit := number/10, number%10

var sum int64
for i:=0; number > 0; i++ {
for i := 0; number > 0; i++ {
digit := number % 10
if i % 2 == 0 {

if i%2 == 0 {
digit *= 2
if digit > 9 {
digit -= 9
}
}

sum += digit
number = number / 10
}
return (sum + lastDigit) % 10 == 0

return (sum+lastDigit)%10 == 0
}

// IsISBN10 checks if the string is an ISBN version 10.
Expand Down Expand Up @@ -1766,3 +1766,10 @@ func (sv stringValues) get(i int) string { return sv[i].String() }
func IsE164(str string) bool {
return rxE164.MatchString(str)
}

// IsYYYYMMDD checks if a string is in the "YYYY-MM-DD" date format and returns true if valid.
func IsYYYYMMDD(str string) bool {
layout := "2006-01-02"
_, err := time.Parse(layout, str)
return err == nil
}
20 changes: 20 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3865,3 +3865,23 @@ func TestIsE164(t *testing.T) {
}
}
}

func TestIsYYYYMMDD(t *testing.T) {
t.Parallel()

var tests = []struct {
param string
expected bool
}{
{"2000-01-01", true},
{"2000-13-01", false},
{"20000-01-01", false},
{"2000-01-32", false},
}
for _, test := range tests {
actual := IsYYYYMMDD(test.param)
if actual != test.expected {
t.Errorf("Expected IsYYYYMMDD(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}

0 comments on commit e113478

Please sign in to comment.