-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime.go
More file actions
157 lines (137 loc) · 3.46 KB
/
time.go
File metadata and controls
157 lines (137 loc) · 3.46 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package check
import (
"fmt"
"time"
)
// TimeSchema validates time.Time values. It can also parse time strings
// using configurable layouts (defaults to RFC3339).
type TimeSchema struct {
optional bool
defaultVal *time.Time
layouts []string // time layouts to try when parsing strings
before *time.Time
after *time.Time
customFns []func(time.Time) error
}
func NewTime() *TimeSchema {
return &TimeSchema{
layouts: []string{time.RFC3339},
}
}
// clone returns a copy of this schema to preserve immutability in chains.
func (s *TimeSchema) clone() *TimeSchema {
copy := *s
if s.layouts != nil {
copyLayouts := make([]string, len(s.layouts))
for i, v := range s.layouts {
copyLayouts[i] = v
}
copy.layouts = copyLayouts
}
if s.customFns != nil {
copyCustom := make([]func(time.Time) error, len(s.customFns))
for i, v := range s.customFns {
copyCustom[i] = v
}
copy.customFns = copyCustom
}
return ©
}
func (s *TimeSchema) Optional() *TimeSchema {
s = s.clone()
s.optional = true
return s
}
func (s *TimeSchema) Default(v time.Time) *TimeSchema {
s = s.clone()
s.optional = true
s.defaultVal = &v
return s
}
// Layout sets the time layout(s) used when parsing strings.
// Replaces the default RFC3339. Multiple layouts are tried in order.
func (s *TimeSchema) Layout(layouts ...string) *TimeSchema {
s = s.clone()
s.layouts = layouts
return s
}
// Before requires the time to be before the given time.
func (s *TimeSchema) Before(t time.Time) *TimeSchema {
s = s.clone()
s.before = &t
return s
}
// After requires the time to be after the given time.
func (s *TimeSchema) After(t time.Time) *TimeSchema {
s = s.clone()
s.after = &t
return s
}
// Custom adds a custom validation function.
func (s *TimeSchema) Custom(fn func(time.Time) error) *TimeSchema {
s = s.clone()
s.customFns = append(s.customFns, fn)
return s
}
func (s *TimeSchema) IsOptional() bool { return s.optional }
func (s *TimeSchema) parseTime(value any) (time.Time, bool) {
switch v := value.(type) {
case time.Time:
return v, true
case string:
for _, layout := range s.layouts {
if t, err := time.Parse(layout, v); err == nil {
return t, true
}
}
}
return time.Time{}, false
}
func (s *TimeSchema) Parse(value any) (any, error) {
errs := s.Validate(value)
if len(errs) > 0 {
return nil, ValidationErrors(errs)
}
if value == nil && s.defaultVal != nil {
return *s.defaultVal, nil
}
if value == nil {
return nil, nil
}
t, _ := s.parseTime(value)
return t, nil
}
func (s *TimeSchema) Validate(value any) []ValidationError {
if value == nil {
if s.optional {
return nil
}
return []ValidationError{{Message: "required value is missing"}}
}
t, ok := s.parseTime(value)
if !ok {
return []ValidationError{{
Message: fmt.Sprintf("expected time.Time or parseable time string, got %T", value),
Value: value,
}}
}
var errs []ValidationError
if s.before != nil && !t.Before(*s.before) {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("must be before %s (got %s)", s.before.Format(time.RFC3339), t.Format(time.RFC3339)),
Value: t,
})
}
if s.after != nil && !t.After(*s.after) {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("must be after %s (got %s)", s.after.Format(time.RFC3339), t.Format(time.RFC3339)),
Value: t,
})
}
for _, fn := range s.customFns {
if err := fn(t); err != nil {
errs = append(errs, ValidationError{Message: err.Error(), Value: t})
}
}
return errs
}