Skip to content

Commit

Permalink
Removed (.) in README, added more tests for the DATE Rule and improve…
Browse files Browse the repository at this point in the history
…d Coverage extraction
  • Loading branch information
adnanbrq committed Jul 26, 2024
1 parent e9c038e commit 4203d5d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
id: extract_coverage
run: |
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}')
echo "COVERAGE=${COVERAGE:0:3}" >> $GITHUB_ENV
echo "COVERAGE=${COVERAGE%%.*}" >> $GITHUB_ENV
- name: Update README with coverage badge
if: ${{ github.event_name == 'pull_request' }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Coverage](https://img.shields.io/badge/Coverage-98.-brightgreen) [![Go Reference](https://pkg.go.dev/badge/github.com/adnanbrq/validation/v2.svg)](https://pkg.go.dev/github.com/adnanbrq/validation/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/adnanbrq/validation/v2)](https://goreportcard.com/report/github.com/adnanbrq/validation/v2)
![Coverage](https://img.shields.io/badge/Coverage-98-brightgreen) [![Go Reference](https://pkg.go.dev/badge/github.com/adnanbrq/validation/v2.svg)](https://pkg.go.dev/github.com/adnanbrq/validation/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/adnanbrq/validation/v2)](https://goreportcard.com/report/github.com/adnanbrq/validation/v2)

# Go Validation

Expand Down
40 changes: 20 additions & 20 deletions rules/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package rules

import (
"regexp"
"strconv"
"time"

"github.com/adnanbrq/validation/v2/helper"
)

var (
Expand All @@ -14,39 +15,38 @@ var (

type DateRule struct{}

func (DateRule) Regex() (*regexp.Regexp, error) {
return regexp.Compile("(D[0-9]{1,2})")
}

func (DateRule) Name() string {
return "date"
}

func (DateRule) Validate(value, options any) []string {
if !helper.IsString(options) {
return noErrs
}

t, ok := value.(time.Time)

if !ok {
return []string{errNoTime}
}

expressions := []string{
`(D[0-9]{1,2})`,
`(Y[0-9]{2,4})`,
`(M[0-9]{1,2})`,
type Exp struct {
reg string
comparingValue int64
err string
}

expressions := []Exp{
{`D[0-9]{1,2}`, int64(t.Day()), errNotSameDay},
{`M[0-9]{1,2}`, int64(t.Month()), errNotSameMonth},
{`Y[0-9]{4}`, int64(t.Year()), errNotSameYear},
}

for _, exp := range expressions {
if reg, err := regexp.Compile(exp); err == nil {
if find := reg.FindString(t.UTC().Format("2006-01-02")); len(find) != 0 {
if value, errConv := strconv.ParseInt(find[1:], 0, 64); errConv == nil {
switch true {
case find[0:1] == "D" && t.Day() != int(value):
return []string{errNotSameDay}
case find[0:1] == "M" && int(t.Month()) != int(value):
return []string{errNotSameMonth}
case find[0:1] == "Y" && t.Year() != int(value):
return []string{errNotSameYear}
}
if reg, err := regexp.Compile(exp.reg); err == nil {
if find := reg.FindString(options.(string)); len(find) != 0 {
if exp.comparingValue != helper.ParseInt(find[1:]) {
return []string{exp.err, find[1:]}
}
}
}
Expand Down
64 changes: 63 additions & 1 deletion rules/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rules

import (
"fmt"
"math/rand"
"testing"
"time"

Expand All @@ -10,9 +11,70 @@ import (

func TestDate(t *testing.T) {
rule := DateRule{}
input := time.Date(2020, 1, 1, 1, 0, 0, 0, time.UTC)

// Name
assert.NotEmpty(t, rule.Name())

assert.Equal(t, noErrs, rule.Validate(time.Now(), fmt.Sprintf("D%d", time.Now().Day())))
// Invalid input
assert.Equal(t, []string{errNoTime}, rule.Validate(nil, "D"))
assert.Equal(t, []string{errNoTime}, rule.Validate(false, "D"))
assert.Equal(t, []string{errNoTime}, rule.Validate(0, "D"))
assert.Equal(t, []string{errNoTime}, rule.Validate(make(chan int), "D"))
assert.Equal(t, []string{errNoTime}, rule.Validate(map[int]int{}, "D"))

// No digits
assert.Equal(t, noErrs, rule.Validate(input, "D"))
assert.Equal(t, noErrs, rule.Validate(input, "M"))
assert.Equal(t, noErrs, rule.Validate(input, "Y"))

// Complete date
assert.Equal(t, noErrs, rule.Validate(input, "D1M1Y2020"))
assert.Equal(t, noErrs, rule.Validate(input, "D01M1Y2020"))
assert.Equal(t, noErrs, rule.Validate(input, "D1M01Y2020"))
assert.Equal(t, noErrs, rule.Validate(input, "D01M01Y2020"))
assert.Equal(t, noErrs, rule.Validate(input, "D1M1Y2020"))
assert.Equal(t, []string{errNotSameDay, "0"}, rule.Validate(input, "D0M1Y2020"))
assert.Equal(t, []string{errNotSameMonth, "0"}, rule.Validate(input, "D1M0Y2020"))
assert.Equal(t, []string{errNotSameYear, "2024"}, rule.Validate(input, "D1M1Y2024"))
assert.Equal(t, []string{errNotSameYear, "2024"}, rule.Validate(input, "D1M1Y2024"))

// Day
assert.Equal(t, noErrs, rule.Validate(input, fmt.Sprintf("D%d", input.Day())))
assert.Equal(t, noErrs, rule.Validate(input, "M01")) // Only valid because input on L:14 is constructed with 1 for the Day
assert.Equal(t, noErrs, rule.Validate(input, "M1")) // Leading zero is possible and ^
assert.Equal(t, []string{errNotSameDay, "99"}, rule.Validate(input, "D99")) // D99 is a valid input but will be treated as a possible date and thus result in a error
assert.Equal(t, noErrs, rule.Validate(input, nil)) // invalid options will not raise any error as the input cannot be validated It's the Developer who did something wrong here
assert.Equal(t, noErrs, rule.Validate(input, "")) // ^
assert.Equal(t, noErrs, rule.Validate(input, 5)) // ^
assert.Equal(t, noErrs, rule.Validate(input, rand.Int())) // ^
assert.Equal(t, noErrs, rule.Validate(input, rand.Float32())) // ^
assert.Equal(t, noErrs, rule.Validate(input, false)) // ^
assert.Equal(t, noErrs, rule.Validate(input, make(chan int))) // ^

// Month
assert.Equal(t, noErrs, rule.Validate(input, fmt.Sprintf("M%d", input.Month())))
assert.Equal(t, noErrs, rule.Validate(input, "M01")) // Only valid because input on L:14 is constructed with 1 for the Month
assert.Equal(t, noErrs, rule.Validate(input, "M1")) // Leading zero is possible and ^
assert.Equal(t, []string{errNotSameMonth, "99"}, rule.Validate(input, "M99")) // N99 is a valid input but will be treated as a possible month and thus result in a error
assert.Equal(t, noErrs, rule.Validate(input, nil)) // invalid options will not raise any error as the input cannot be validated It's the Developer who did something wrong here
assert.Equal(t, noErrs, rule.Validate(input, "")) // ^
assert.Equal(t, noErrs, rule.Validate(input, 5)) // ^
assert.Equal(t, noErrs, rule.Validate(input, rand.Int())) // ^
assert.Equal(t, noErrs, rule.Validate(input, rand.Float32())) // ^
assert.Equal(t, noErrs, rule.Validate(input, false)) // ^
assert.Equal(t, noErrs, rule.Validate(input, make(chan int))) // ^

// Year
assert.Equal(t, noErrs, rule.Validate(input, fmt.Sprintf("Y%d", input.Year())))
assert.Equal(t, noErrs, rule.Validate(input, "Y2020")) // Only valid because input on L:14 is constructed with 1 for the Year
assert.Equal(t, noErrs, rule.Validate(input, "Y99")) // Y99 is not a valid year and thus leads to noErr as it cannot be used for validation
assert.Equal(t, noErrs, rule.Validate(input, nil)) // invalid options will not raise any error as the input cannot be validated It's the Developer who did something wrong here
assert.Equal(t, noErrs, rule.Validate(input, "")) // ^
assert.Equal(t, noErrs, rule.Validate(input, 5)) // ^
assert.Equal(t, noErrs, rule.Validate(input, rand.Int())) // ^
assert.Equal(t, noErrs, rule.Validate(input, rand.Float32())) // ^
assert.Equal(t, noErrs, rule.Validate(input, false)) // ^
assert.Equal(t, noErrs, rule.Validate(input, make(chan int))) // ^

}
14 changes: 14 additions & 0 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,21 @@ func NewValidator() *Validator {
"no-uint": "is not a unsigned integer",
"uint-wrong-size": "value needs to be {{.O1}} bit",
"no-float": "is not a float",
"no-time": "is not a valid date",
"time-not-today": "has to be today",
"time-not-yesterday": "has to be yesterday",
"time-not-in-future": "needs to be in the future",
"time-not-in-past": "needs to be in the past",
"float-wrong-size": "value needs to be {{.O1}} bit",
"not-same-day": "Day is not {{.O1}}",
"not-same-month": "Month is not {{.O1}}",
"not-same-year": "Year is not {{.O1}}",
"min-day": "must be {{.O1}} or later",
"max-day": "must be before or {{.O1}}",
"min-month": "must be {{.O1}} or later",
"max-month": "must be before or {{.O1}}",
"min-year": "must be {{.O1}} or later",
"max-year": "must be before or {{.O1}}",
}

predefinedRules := map[string]rules.Rule{}
Expand Down

0 comments on commit 4203d5d

Please sign in to comment.