Passed
Pull Request — main (#23)
by Igor
01:35
created

it.BlankConstraint.newViolation   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
nop 1
1
package it
2
3
import (
4
	"github.com/muonsoft/validation"
5
	"github.com/muonsoft/validation/code"
6
	"github.com/muonsoft/validation/generic"
7
	"github.com/muonsoft/validation/message"
8
9
	"time"
10
)
11
12
// NotBlankConstraint checks that a value is not blank: not equal to zero, an empty string, an empty
13
// slice/array, an empty map, `false` or `nil`. Nil behavior is configurable via AllowNil() method.
14
// To check that a value is not nil only use NotNilConstraint.
15
type NotBlankConstraint struct {
16
	messageTemplate string
17
	isIgnored       bool
18
	allowNil        bool
19
}
20
21
func IsNotBlank() NotBlankConstraint {
22 1
	return NotBlankConstraint{
23
		messageTemplate: message.NotBlank,
24
	}
25
}
26
27
func (c NotBlankConstraint) AllowNil() NotBlankConstraint {
28 1
	c.allowNil = true
29 1
	return c
30
}
31
32
func (c NotBlankConstraint) When(condition bool) NotBlankConstraint {
33 1
	c.isIgnored = !condition
34 1
	return c
35
}
36
37
func (c NotBlankConstraint) Message(message string) NotBlankConstraint {
38 1
	c.messageTemplate = message
39 1
	return c
40
}
41
42
func (c NotBlankConstraint) SetUp(scope *validation.Scope) error {
43 1
	return nil
44
}
45
46
func (c NotBlankConstraint) Name() string {
47
	return "NotBlankConstraint"
48
}
49
50
func (c NotBlankConstraint) ValidateNil(scope validation.Scope) error {
51 1
	if c.isIgnored || c.allowNil {
52 1
		return nil
53
	}
54
55 1
	return c.newViolation(scope)
56
}
57
58
func (c NotBlankConstraint) ValidateBool(value *bool, scope validation.Scope) error {
59 1
	if c.isIgnored {
60 1
		return nil
61
	}
62 1
	if c.allowNil && value == nil {
63 1
		return nil
64
	}
65 1
	if value != nil && *value {
66 1
		return nil
67
	}
68
69 1
	return c.newViolation(scope)
70
}
71
72
func (c NotBlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
73 1
	if c.isIgnored {
74 1
		return nil
75
	}
76 1
	if c.allowNil && value.IsNil() {
77 1
		return nil
78
	}
79 1
	if !value.IsNil() && !value.IsZero() {
80 1
		return nil
81
	}
82
83 1
	return c.newViolation(scope)
84
}
85
86
func (c NotBlankConstraint) ValidateString(value *string, scope validation.Scope) error {
87 1
	if c.isIgnored {
88 1
		return nil
89
	}
90 1
	if c.allowNil && value == nil {
91 1
		return nil
92
	}
93 1
	if value != nil && *value != "" {
94 1
		return nil
95
	}
96
97 1
	return c.newViolation(scope)
98
}
99
100
func (c NotBlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
101 1
	if c.isIgnored {
102 1
		return nil
103
	}
104 1
	if c.allowNil && value.IsNil() {
105 1
		return nil
106
	}
107 1
	if value.Count() > 0 {
108 1
		return nil
109
	}
110
111 1
	return c.newViolation(scope)
112
}
113
114
func (c NotBlankConstraint) ValidateCountable(count int, scope validation.Scope) error {
115 1
	if c.isIgnored || count > 0 {
116 1
		return nil
117
	}
118
119 1
	return c.newViolation(scope)
120
}
121
122
func (c NotBlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
123 1
	if c.isIgnored {
124 1
		return nil
125
	}
126 1
	if c.allowNil && value == nil {
127 1
		return nil
128
	}
129
130 1
	var empty time.Time
131 1
	if value != nil && *value != empty {
132 1
		return nil
133
	}
134
135 1
	return c.newViolation(scope)
136
}
137
138
func (c NotBlankConstraint) newViolation(scope validation.Scope) validation.Violation {
139 1
	return scope.BuildViolation(code.NotBlank, c.messageTemplate).GetViolation()
140
}
141
142
// BlankConstraint checks that a value is blank: equal to `false`, `nil`, zero, an empty string, an empty
143
// slice, array, or a map.
144
type BlankConstraint struct {
145
	messageTemplate string
146
	isIgnored       bool
147
}
148
149
func IsBlank() BlankConstraint {
150 1
	return BlankConstraint{
151
		messageTemplate: message.Blank,
152
	}
153
}
154
155
func (c BlankConstraint) SetUp(scope *validation.Scope) error {
156 1
	return nil
157
}
158
159
func (c BlankConstraint) Name() string {
160
	return "BlankConstraint"
161
}
162
163
func (c BlankConstraint) When(condition bool) BlankConstraint {
164 1
	c.isIgnored = !condition
165 1
	return c
166
}
167
168
func (c BlankConstraint) Message(message string) BlankConstraint {
169 1
	c.messageTemplate = message
170 1
	return c
171
}
172
173
func (c BlankConstraint) ValidateNil(scope validation.Scope) error {
174 1
	return nil
175
}
176
177
func (c BlankConstraint) ValidateBool(value *bool, scope validation.Scope) error {
178 1
	if c.isIgnored || value == nil || !*value {
179 1
		return nil
180
	}
181
182 1
	return c.newViolation(scope)
183
}
184
185
func (c BlankConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
186 1
	if c.isIgnored || value.IsNil() || value.IsZero() {
187 1
		return nil
188
	}
189
190 1
	return c.newViolation(scope)
191
}
192
193
func (c BlankConstraint) ValidateString(value *string, scope validation.Scope) error {
194 1
	if c.isIgnored || value == nil || *value == "" {
195 1
		return nil
196
	}
197
198 1
	return c.newViolation(scope)
199
}
200
201
func (c BlankConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
202 1
	if c.isIgnored || value.Count() == 0 {
203 1
		return nil
204
	}
205
206 1
	return c.newViolation(scope)
207
}
208
209
func (c BlankConstraint) ValidateCountable(count int, scope validation.Scope) error {
210 1
	if c.isIgnored || count == 0 {
211 1
		return nil
212
	}
213
214 1
	return c.newViolation(scope)
215
}
216
217
func (c BlankConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
218 1
	var empty time.Time
219 1
	if c.isIgnored || value == nil || *value == empty {
220 1
		return nil
221
	}
222
223 1
	return c.newViolation(scope)
224
}
225
226
func (c BlankConstraint) newViolation(scope validation.Scope) validation.Violation {
227 1
	return scope.BuildViolation(code.Blank, c.messageTemplate).GetViolation()
228
}
229
230
// NotNilConstraint checks that a value in not strictly equal to `nil`. To check that values in not blank use
231
// NotBlankConstraint.
232
type NotNilConstraint struct {
233
	messageTemplate string
234
	isIgnored       bool
235
}
236
237
func IsNotNil() NotNilConstraint {
238 1
	return NotNilConstraint{
239
		messageTemplate: message.NotNil,
240
	}
241
}
242
243
func (c NotNilConstraint) When(condition bool) NotNilConstraint {
244 1
	c.isIgnored = !condition
245 1
	return c
246
}
247
248
func (c NotNilConstraint) Message(message string) NotNilConstraint {
249 1
	c.messageTemplate = message
250 1
	return c
251
}
252
253
func (c NotNilConstraint) SetUp(scope *validation.Scope) error {
254 1
	return nil
255
}
256
257
func (c NotNilConstraint) Name() string {
258
	return "NotNilConstraint"
259
}
260
261
func (c NotNilConstraint) ValidateNil(scope validation.Scope) error {
262
	if c.isIgnored {
263
		return nil
264
	}
265
266
	return c.newViolation(scope)
267
}
268
269
func (c NotNilConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
270 1
	if c.isIgnored {
271 1
		return nil
272
	}
273 1
	if !value.IsNil() {
274 1
		return nil
275
	}
276
277 1
	return c.newViolation(scope)
278
}
279
280
func (c NotNilConstraint) ValidateString(value *string, scope validation.Scope) error {
281 1
	if c.isIgnored {
282 1
		return nil
283
	}
284 1
	if value != nil {
285 1
		return nil
286
	}
287
288 1
	return c.newViolation(scope)
289
}
290
291
func (c NotNilConstraint) ValidateTime(value *time.Time, scope validation.Scope) error {
292 1
	if c.isIgnored {
293 1
		return nil
294
	}
295 1
	if value != nil {
296 1
		return nil
297
	}
298
299 1
	return c.newViolation(scope)
300
}
301
302
func (c NotNilConstraint) ValidateIterable(value generic.Iterable, scope validation.Scope) error {
303 1
	if c.isIgnored {
304 1
		return nil
305
	}
306 1
	if !value.IsNil() {
307 1
		return nil
308
	}
309
310 1
	return c.newViolation(scope)
311
}
312
313
func (c NotNilConstraint) newViolation(scope validation.Scope) validation.Violation {
314 1
	return scope.BuildViolation(code.NotNil, c.messageTemplate).GetViolation()
315
}
316