-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
53 lines (45 loc) · 889 Bytes
/
field.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
package fvalidation
type Field[T any] struct {
title string
value T
vCtx *ValidationContext
}
func RuleFor[T any](vCtx *ValidationContext, title string, value T) *Field[T] {
return &Field[T]{
vCtx: vCtx,
title: title,
value: value,
}
}
func (f *Field[T]) Must(rule Rule[T]) *RuleValidationFailure[T] {
if f.vCtx.err != nil {
return &RuleValidationFailure[T]{
field: f,
payload: nil,
}
}
valid, err := rule.validate(f.value)
if err != nil {
f.vCtx.err = err
return &RuleValidationFailure[T]{
field: f,
payload: nil,
}
}
payload := rule.DefaultPayload()
if valid {
return &RuleValidationFailure[T]{
field: f,
payload: &payload,
}
}
failure := Failure{
field: f.title,
payload: &payload,
}
f.vCtx.failures = append(f.vCtx.failures, failure)
return &RuleValidationFailure[T]{
field: f,
payload: &payload,
}
}