Passed
Push — main ( da04d5...f09b9b )
by Rushan
02:09 queued 11s
created

it.NumberComparisonConstraint.Name   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 0
cts 1
cp 0
crap 2
nop 0
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
10
// NumberComparisonConstraint is used for various numeric comparisons between integer and float values.
11
// Values are compared as integers if the compared and specified values are integers.
12
// Otherwise, numbers are always compared as floating point numbers.
13
type NumberComparisonConstraint struct {
14
	isIgnored       bool
15
	code            string
16
	messageTemplate string
17
	comparedValue   string
18
	isValid         func(value generic.Number) bool
19
}
20
21
// IsEqualToInteger checks that the number (integer or float) is equal to the specified integer value.
22
// Values are compared as integers if the compared and specified values are integers.
23
// Otherwise, numbers are always compared as floating point numbers.
24
//
25
// Example
26
//  v := 1
27
//  err := validator.ValidateNumber(&v, it.IsEqualToInteger(2))
28
func IsEqualToInteger(value int64) NumberComparisonConstraint {
29 1
	v := generic.NewNumberFromInt(value)
30
31 1
	return NumberComparisonConstraint{
32
		code:            code.Equal,
33
		messageTemplate: message.Equal,
34
		comparedValue:   v.String(),
35
		isValid: func(n generic.Number) bool {
36 1
			return n.IsEqualTo(v)
37
		},
38
	}
39
}
40
41
// IsEqualToFloat checks that the number (integer or float) is equal to the specified float value.
42
// Values are compared as integers if the compared and specified values are integers.
43
// Otherwise, numbers are always compared as floating point numbers.
44
//
45
// Example
46
//  v := 1.1
47
//  err := validator.ValidateNumber(&v, it.IsEqualToFloat(1.2))
48
func IsEqualToFloat(value float64) NumberComparisonConstraint {
49 1
	v := generic.NewNumberFromFloat(value)
50
51 1
	return NumberComparisonConstraint{
52
		code:            code.Equal,
53
		messageTemplate: message.Equal,
54
		comparedValue:   v.String(),
55
		isValid: func(n generic.Number) bool {
56 1
			return n.IsEqualTo(v)
57
		},
58
	}
59
}
60
61
// IsNotEqualToInteger checks that the number (integer or float) is not equal to the specified integer value.
62
// Values are compared as integers if the compared and specified values are integers.
63
// Otherwise, numbers are always compared as floating point numbers.
64
//
65
// Example
66
//  v := 1
67
//  err := validator.ValidateNumber(&v, it.IsNotEqualToInteger(1))
68
func IsNotEqualToInteger(value int64) NumberComparisonConstraint {
69 1
	v := generic.NewNumberFromInt(value)
70
71 1
	return NumberComparisonConstraint{
72
		code:            code.NotEqual,
73
		messageTemplate: message.NotEqual,
74
		comparedValue:   v.String(),
75
		isValid: func(n generic.Number) bool {
76 1
			return !n.IsEqualTo(v)
77
		},
78
	}
79
}
80
81
// IsNotEqualToFloat checks that the number (integer or float) is not equal to the specified float value.
82
// Values are compared as integers if the compared and specified values are integers.
83
// Otherwise, numbers are always compared as floating point numbers.
84
//
85
// Example
86
//  v := 1.1
87
//  err := validator.ValidateNumber(&v, it.IsNotEqualToFloat(1.1))
88
func IsNotEqualToFloat(value float64) NumberComparisonConstraint {
89 1
	v := generic.NewNumberFromFloat(value)
90
91 1
	return NumberComparisonConstraint{
92
		code:            code.NotEqual,
93
		messageTemplate: message.NotEqual,
94
		comparedValue:   v.String(),
95
		isValid: func(n generic.Number) bool {
96 1
			return !n.IsEqualTo(v)
97
		},
98
	}
99
}
100
101
// IsLessThanInteger checks that the number (integer or float) is less than the specified integer value.
102
// Values are compared as integers if the compared and specified values are integers.
103
// Otherwise, numbers are always compared as floating point numbers.
104
//
105
// Example
106
//  v := 1
107
//  err := validator.ValidateNumber(&v, it.IsLessThanInteger(1))
108
func IsLessThanInteger(value int64) NumberComparisonConstraint {
109 1
	v := generic.NewNumberFromInt(value)
110
111 1
	return NumberComparisonConstraint{
112
		code:            code.TooHigh,
113
		messageTemplate: message.TooHigh,
114
		comparedValue:   v.String(),
115
		isValid: func(n generic.Number) bool {
116 1
			return n.IsLessThan(v)
117
		},
118
	}
119
}
120
121
// IsLessThanFloat checks that the number (integer or float) is less than the specified float value.
122
// Values are compared as integers if the compared and specified values are integers.
123
// Otherwise, numbers are always compared as floating point numbers.
124
//
125
// Example
126
//  v := 1.1
127
//  err := validator.ValidateNumber(&v, it.IsLessThanFloat(1.1))
128
func IsLessThanFloat(value float64) NumberComparisonConstraint {
129 1
	v := generic.NewNumberFromFloat(value)
130
131 1
	return NumberComparisonConstraint{
132
		code:            code.TooHigh,
133
		messageTemplate: message.TooHigh,
134
		comparedValue:   v.String(),
135
		isValid: func(n generic.Number) bool {
136 1
			return n.IsLessThan(v)
137
		},
138
	}
139
}
140
141
// IsLessThanOrEqualInteger checks that the number (integer or float) is less than or
142
// equal to the specified integer value. Values are compared as integers if the compared
143
// and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
144
//
145
// Example
146
//  v := 1
147
//  err := validator.ValidateNumber(&v, it.IsLessThanOrEqualInteger(2))
148
func IsLessThanOrEqualInteger(value int64) NumberComparisonConstraint {
149 1
	v := generic.NewNumberFromInt(value)
150
151 1
	return NumberComparisonConstraint{
152
		code:            code.TooHighOrEqual,
153
		messageTemplate: message.TooHighOrEqual,
154
		comparedValue:   v.String(),
155
		isValid: func(n generic.Number) bool {
156 1
			return n.IsLessThan(v) || n.IsEqualTo(v)
157
		},
158
	}
159
}
160
161
// IsLessThanOrEqualFloat checks that the number (integer or float) is less than or
162
// equal to the specified float value. Values are compared as integers if the compared
163
// and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
164
//
165
// Example
166
//  v := 1.1
167
//  err := validator.ValidateNumber(&v, it.IsLessThanOrEqualFloat(1.2))
168
func IsLessThanOrEqualFloat(value float64) NumberComparisonConstraint {
169 1
	v := generic.NewNumberFromFloat(value)
170
171 1
	return NumberComparisonConstraint{
172
		code:            code.TooHighOrEqual,
173
		messageTemplate: message.TooHighOrEqual,
174
		comparedValue:   v.String(),
175
		isValid: func(n generic.Number) bool {
176 1
			return n.IsLessThan(v) || n.IsEqualTo(v)
177
		},
178
	}
179
}
180
181
// IsGreaterThanInteger checks that the number (integer or float) is greater than the specified integer value.
182
// Values are compared as integers if the compared and specified values are integers.
183
// Otherwise, numbers are always compared as floating point numbers.
184
//
185
// Example
186
//  v := 1
187
//  err := validator.ValidateNumber(&v, it.IsGreaterThanInteger(1))
188
func IsGreaterThanInteger(value int64) NumberComparisonConstraint {
189 1
	v := generic.NewNumberFromInt(value)
190
191 1
	return NumberComparisonConstraint{
192
		code:            code.TooLow,
193
		messageTemplate: message.TooLow,
194
		comparedValue:   v.String(),
195
		isValid: func(n generic.Number) bool {
196 1
			return n.IsGreaterThan(v)
197
		},
198
	}
199
}
200
201
// IsGreaterThanFloat checks that the number (integer or float) is greater than the specified float value.
202
// Values are compared as integers if the compared and specified values are integers.
203
// Otherwise, numbers are always compared as floating point numbers.
204
//
205
// Example
206
//  v := 1.1
207
//  err := validator.ValidateNumber(&v, it.IsGreaterThanFloat(1.1))
208
func IsGreaterThanFloat(value float64) NumberComparisonConstraint {
209 1
	v := generic.NewNumberFromFloat(value)
210
211 1
	return NumberComparisonConstraint{
212
		code:            code.TooLow,
213
		messageTemplate: message.TooLow,
214
		comparedValue:   v.String(),
215
		isValid: func(n generic.Number) bool {
216 1
			return n.IsGreaterThan(v)
217
		},
218
	}
219
}
220
221
// IsGreaterThanOrEqualInteger checks that the number (integer or float) is greater than or
222
// equal to the specified integer value. Values are compared as integers if the compared
223
// and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
224
//
225
// Example
226
//  v := 1
227
//  err := validator.ValidateNumber(&v, it.IsGreaterThanOrEqualInteger(2))
228
func IsGreaterThanOrEqualInteger(value int64) NumberComparisonConstraint {
229 1
	v := generic.NewNumberFromInt(value)
230
231 1
	return NumberComparisonConstraint{
232
		code:            code.TooLowOrEqual,
233
		messageTemplate: message.TooLowOrEqual,
234
		comparedValue:   v.String(),
235
		isValid: func(n generic.Number) bool {
236 1
			return n.IsGreaterThan(v) || n.IsEqualTo(v)
237
		},
238
	}
239
}
240
241
// IsGreaterThanOrEqualFloat checks that the number (integer or float) is greater than or
242
// equal to the specified float value. Values are compared as integers if the compared
243
// and specified values are integers. Otherwise, numbers are always compared as floating point numbers.
244
//
245
// Example
246
//  v := 1.1
247
//  err := validator.ValidateNumber(&v, it.IsGreaterThanOrEqualFloat(1.2))
248
func IsGreaterThanOrEqualFloat(value float64) NumberComparisonConstraint {
249 1
	v := generic.NewNumberFromFloat(value)
250
251 1
	return NumberComparisonConstraint{
252
		code:            code.TooLowOrEqual,
253
		messageTemplate: message.TooLowOrEqual,
254
		comparedValue:   v.String(),
255
		isValid: func(n generic.Number) bool {
256 1
			return n.IsGreaterThan(v) || n.IsEqualTo(v)
257
		},
258
	}
259
}
260
261
// IsPositive checks that the value is a positive number (integer or float). Zero is neither
262
// positive nor negative. If you want to allow zero use IsPositiveOrZero comparison.
263
//
264
// Example
265
//  v := -1
266
//  err := validator.ValidateNumber(&v, it.IsPositive())
267
func IsPositive() NumberComparisonConstraint {
268 1
	v := generic.NewNumberFromInt(0)
269
270 1
	return NumberComparisonConstraint{
271
		code:            code.NotPositive,
272
		messageTemplate: message.NotPositive,
273
		comparedValue:   v.String(),
274
		isValid: func(n generic.Number) bool {
275 1
			return n.IsGreaterThan(v)
276
		},
277
	}
278
}
279
280
// IsPositiveOrZero checks that the value is a positive number (integer or float) or equal to zero.
281
// If you don't want to allow zero as a valid value, use IsPositive comparison.
282
//
283
// Example
284
//  v := -1
285
//  err := validator.ValidateNumber(&v, it.IsPositiveOrZero())
286
func IsPositiveOrZero() NumberComparisonConstraint {
287 1
	v := generic.NewNumberFromInt(0)
288
289 1
	return NumberComparisonConstraint{
290
		code:            code.NotPositiveOrZero,
291
		messageTemplate: message.NotPositiveOrZero,
292
		comparedValue:   v.String(),
293
		isValid: func(n generic.Number) bool {
294 1
			return n.IsGreaterThan(v) || n.IsEqualTo(v)
295
		},
296
	}
297
}
298
299
// IsNegative checks that the value is a negative number (integer or float). Zero is neither
300
// positive nor negative. If you want to allow zero use IsNegativeOrZero comparison.
301
//
302
// Example
303
//  v := 1
304
//  err := validator.ValidateNumber(&v, it.IsNegative())
305
func IsNegative() NumberComparisonConstraint {
306 1
	v := generic.NewNumberFromInt(0)
307
308 1
	return NumberComparisonConstraint{
309
		code:            code.NotNegative,
310
		messageTemplate: message.NotNegative,
311
		comparedValue:   v.String(),
312
		isValid: func(n generic.Number) bool {
313 1
			return n.IsLessThan(v)
314
		},
315
	}
316
}
317
318
// IsNegativeOrZero checks that the value is a negative number (integer or float) or equal to zero.
319
// If you don't want to allow zero as a valid value, use IsNegative comparison.
320
//
321
// Example
322
//  v := -1
323
//  err := validator.ValidateNumber(&v, it.IsNegativeOrZero())
324
func IsNegativeOrZero() NumberComparisonConstraint {
325 1
	v := generic.NewNumberFromInt(0)
326
327 1
	return NumberComparisonConstraint{
328
		code:            code.NotNegativeOrZero,
329
		messageTemplate: message.NotNegativeOrZero,
330
		comparedValue:   v.String(),
331
		isValid: func(n generic.Number) bool {
332 1
			return n.IsLessThan(v) || n.IsEqualTo(v)
333
		},
334
	}
335
}
336
337
// SetUp always returns no error.
338
func (c NumberComparisonConstraint) SetUp() error {
339 1
	return nil
340
}
341
342
// Name is the constraint name.
343
func (c NumberComparisonConstraint) Name() string {
344
	return "NumberComparisonConstraint"
345
}
346
347
// Message sets the violation message template. You can use template parameters
348
// for injecting its values into the final message:
349
//
350
//	{{ comparedValue }} - the expected value;
351
//	{{ value }} - the current (invalid) value.
352
func (c NumberComparisonConstraint) Message(message string) NumberComparisonConstraint {
353 1
	c.messageTemplate = message
354 1
	return c
355
}
356
357
// When enables conditional validation of this constraint. If the expression evaluates to false,
358
// then the constraint will be ignored.
359
func (c NumberComparisonConstraint) When(condition bool) NumberComparisonConstraint {
360 1
	c.isIgnored = !condition
361 1
	return c
362
}
363
364
func (c NumberComparisonConstraint) ValidateNumber(value generic.Number, scope validation.Scope) error {
365 1
	if c.isIgnored || value.IsNil() || c.isValid(value) {
366 1
		return nil
367
	}
368
369 1
	return scope.BuildViolation(c.code, c.messageTemplate).
370
		SetParameters([]validation.TemplateParameter{
371
			{Key: "{{ comparedValue }}", Value: c.comparedValue},
372
			{Key: "{{ value }}", Value: value.String()},
373
		}).
374
		CreateViolation()
375
}
376