forked from go-goyave/goyave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathafter.go
109 lines (88 loc) Β· 3.7 KB
/
after.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package validation
import (
"fmt"
"time"
"goyave.dev/goyave/v5/util/errors"
"goyave.dev/goyave/v5/util/walk"
)
// AfterValidator validates the field under validation must be a date (`time.Time`) before
// the specified date.
type AfterValidator struct {
DateComparisonValidator
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *AfterValidator) Validate(ctx *Context) bool {
return v.validate(ctx, func(t1, t2 time.Time) bool {
return t1.After(t2)
})
}
// Name returns the string name of the validator.
func (v *AfterValidator) Name() string { return "after" }
// After the field under validation must be a date (`time.Time`) before the given date.
func After(date time.Time) *AfterValidator {
return &AfterValidator{DateComparisonValidator: DateComparisonValidator{Date: date}}
}
//------------------------------
// AfterEqualValidator validates the field under validation must be a date (`time.Time`) after
// or equal to the specified date.
type AfterEqualValidator struct {
DateComparisonValidator
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *AfterEqualValidator) Validate(ctx *Context) bool {
return v.validate(ctx, func(t1, t2 time.Time) bool {
return t1.After(t2) || t1.Equal(t2)
})
}
// Name returns the string name of the validator.
func (v *AfterEqualValidator) Name() string { return "after_equal" }
// AfterEqual the field under validation must be a date (`time.Time`) after or equal to the given date.
func AfterEqual(date time.Time) *AfterEqualValidator {
return &AfterEqualValidator{DateComparisonValidator: DateComparisonValidator{Date: date}}
}
//------------------------------
// AfterFieldValidator validates the field under validation must be a date (`time.Time`) before
// all the other dates matched by the specified path.
type AfterFieldValidator struct {
DateFieldComparisonValidator
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *AfterFieldValidator) Validate(ctx *Context) bool {
return v.validate(ctx, func(t1, t2 time.Time) bool {
return t1.After(t2)
})
}
// Name returns the string name of the validator.
func (v *AfterFieldValidator) Name() string { return "after" }
// AfterField the field under validation must be a date (`time.Time`) before the date field identified
// by the given path.
func AfterField(path string) *AfterFieldValidator {
p, err := walk.Parse(path)
if err != nil {
panic(errors.NewSkip(fmt.Errorf("validation.AfterField: path parse error: %w", err), 3))
}
return &AfterFieldValidator{DateFieldComparisonValidator: DateFieldComparisonValidator{Path: p}}
}
//------------------------------
// AfterEqualFieldValidator validates the field under validation must be a date (`time.Time`) after
// or equal to all the other dates matched by the specified path.
type AfterEqualFieldValidator struct {
DateFieldComparisonValidator
}
// Validate checks the field under validation satisfies this validator's criteria.
func (v *AfterEqualFieldValidator) Validate(ctx *Context) bool {
return v.validate(ctx, func(t1, t2 time.Time) bool {
return t1.After(t2) || t1.Equal(t2)
})
}
// Name returns the string name of the validator.
func (v *AfterEqualFieldValidator) Name() string { return "after_equal" }
// AfterEqualField the field under validation must be a date (`time.Time`) after or equal to the date field identified
// by the given path.
func AfterEqualField(path string) *AfterEqualFieldValidator {
p, err := walk.Parse(path)
if err != nil {
panic(errors.NewSkip(fmt.Errorf("validation.AfterEqualField: path parse error: %w", err), 3))
}
return &AfterEqualFieldValidator{DateFieldComparisonValidator: DateFieldComparisonValidator{Path: p}}
}