-
Notifications
You must be signed in to change notification settings - Fork 7
/
validator_context.go
163 lines (138 loc) · 4.86 KB
/
validator_context.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
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
158
159
160
161
162
163
package valgo
type validatorFragment struct {
errorKey string
template []string
templateParams map[string]any
function func() bool
boolOperation bool
orOperation bool
isValid bool
}
// The context keeps the state and provides the functions to control a
// custom validator.
type ValidatorContext struct {
fragments []*validatorFragment
value any
name *string
title *string
boolOperation bool
orOperation bool
}
// Create a new [ValidatorContext] to be used by a custom validator.
func NewContext(value any, nameAndTitle ...string) *ValidatorContext {
context := &ValidatorContext{
value: value,
fragments: []*validatorFragment{},
boolOperation: true,
orOperation: false,
}
sizeNameAndTitle := len(nameAndTitle)
if sizeNameAndTitle > 0 {
name := nameAndTitle[0]
context.name = &name
if sizeNameAndTitle > 1 {
title := nameAndTitle[1]
context.title = &title
}
}
return context
}
// Invert the boolean value associated with the next validator function in
// a custom validator.
func (ctx *ValidatorContext) Not() *ValidatorContext {
ctx.boolOperation = false
return ctx
}
// Add Or operation to validation.
func (ctx *ValidatorContext) Or() *ValidatorContext {
if len(ctx.fragments) > 0 {
ctx.orOperation = true
}
return ctx
}
// Add a function to a custom validator and pass a value used for the
// validator function to be displayed in the error message.
//
// Use [AddWithParams()] if the error message requires more input values.
func (ctx *ValidatorContext) AddWithValue(function func() bool, errorKey string, value any, template ...string) *ValidatorContext {
return ctx.AddWithParams(
function,
errorKey,
map[string]any{"title": ctx.title, "value": value}, template...)
}
// Add a function to a custom validator.
func (ctx *ValidatorContext) Add(function func() bool, errorKey string, template ...string) *ValidatorContext {
return ctx.AddWithParams(
function,
errorKey,
map[string]any{"title": ctx.title}, template...)
}
// Add a function to a custom validator and pass a map with values used for the
// validator function to be displayed in the error message.
func (ctx *ValidatorContext) AddWithParams(function func() bool, errorKey string, templateParams map[string]any, template ...string) *ValidatorContext {
fragment := &validatorFragment{
errorKey: errorKey,
templateParams: templateParams,
function: function,
boolOperation: ctx.boolOperation,
orOperation: ctx.orOperation,
isValid: true,
}
if len(template) > 0 {
fragment.template = template
}
ctx.fragments = append(ctx.fragments, fragment)
ctx.boolOperation = true
ctx.orOperation = false
return ctx
}
func (ctx *ValidatorContext) validateIs(validation *Validation) *Validation {
return ctx.validate(validation, true)
}
func (ctx *ValidatorContext) validateCheck(validation *Validation) *Validation {
return ctx.validate(validation, false)
}
func (ctx *ValidatorContext) validate(validation *Validation, shortCircuit bool) *Validation {
// valid := true
validation.currentIndex++
// Iterating through each fragment in the context's fragment list
for i, fragment := range ctx.fragments {
// If the previous fragment is not valid, the current fragment is not in an "or" operation, and the short circuit flag is true,
// we return the current state of the validation without evaluating the current fragment
if i > 0 && !ctx.fragments[i-1].isValid && !fragment.orOperation && shortCircuit {
break
}
// If the current fragment is a part of an "or" operation and the previous fragment in the "or" operation
// is valid, we mark the current fragment as valid and move to the next iteration
if fragment.orOperation && ctx.fragments[i-1].isValid {
continue
}
// Evaluating the validation function of the current fragment and updating the valid flag
// The valid flag will be true only if the fragment function returns a value matching the fragment's boolean operation
// and the valid flag was true before this evaluation
fragment.isValid = fragment.function() == fragment.boolOperation
// If the current fragment is valid and is part of an "or" operation, we backtrack to mark all preceding
// fragments in the "or" operation chain as valid
if fragment.isValid && fragment.orOperation {
for j := i - 1; j >= 0; j-- {
ctx.fragments[j].isValid = true
// Breaking the loop when we reach the start of the "or" operation chain
if !ctx.fragments[j].orOperation {
break
}
}
}
// Setting the validation state of the current fragment
// valid = fragment.isValid && valid
}
for _, fragment := range ctx.fragments {
if !fragment.isValid {
validation.invalidate(ctx.name, ctx.title, fragment)
}
}
return validation
}
// Return the value being validated in a custom validator.
func (ctx *ValidatorContext) Value() any {
return ctx.value
}