refactored for maintainability

This commit is contained in:
2026-02-08 17:19:45 +11:00
parent 7125683e6a
commit ac38025b77
40 changed files with 1211 additions and 920 deletions

View 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
}