refactored for maintainability
This commit is contained in:
50
internal/validation/timefield.go
Normal file
50
internal/validation/timefield.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.haelnorr.com/h/timefmt"
|
||||
)
|
||||
|
||||
type TimeField struct {
|
||||
Field
|
||||
Value time.Time
|
||||
}
|
||||
|
||||
func newTimeField(key string, format *timefmt.Format, g Getter) *TimeField {
|
||||
raw := g.Get(key)
|
||||
var startDate time.Time
|
||||
if raw != "" {
|
||||
var err error
|
||||
startDate, err = format.Parse(raw)
|
||||
if err != nil {
|
||||
g.AddCheck(newFailedCheck(
|
||||
"Invalid date/time format",
|
||||
fmt.Sprintf("%s should be in format %s", key, format.LDML()),
|
||||
))
|
||||
}
|
||||
}
|
||||
return &TimeField{
|
||||
Value: startDate,
|
||||
Field: newField(key, g),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TimeField) Required() *TimeField {
|
||||
if t.Value.IsZero() {
|
||||
t.getter.AddCheck(newFailedCheck(
|
||||
"Date/Time not provided",
|
||||
fmt.Sprintf("%s must be provided", t.Key),
|
||||
))
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// Optional will skip all validations if value is empty
|
||||
func (t *TimeField) Optional() *TimeField {
|
||||
if t.Value.IsZero() {
|
||||
t.optional = true
|
||||
}
|
||||
return t
|
||||
}
|
||||
Reference in New Issue
Block a user