-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnumber.go
49 lines (42 loc) · 1.36 KB
/
number.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
package check
import "strconv"
// LowerThan validates that a number must be lower than its value
type LowerThan struct {
Constraint float64
}
// Validate check value against constraint
func (validator LowerThan) Validate(v interface{}) Error {
switch val := v.(type) {
default:
return NewValidationError("NaN")
case int:
if validator.Constraint <= float64(val) {
return NewValidationError("lowerThan", strconv.Itoa(val), strconv.FormatFloat(validator.Constraint, 'f', -1, 64))
}
case float64:
if validator.Constraint <= val {
return NewValidationError("lowerThan", strconv.FormatFloat(val, 'f', -1, 64), strconv.FormatFloat(validator.Constraint, 'f', -1, 64))
}
}
return nil
}
// GreaterThan validates that a number must be greater than its value
type GreaterThan struct {
Constraint float64
}
// Validate check value against constraint
func (validator GreaterThan) Validate(v interface{}) Error {
switch val := v.(type) {
default:
return NewValidationError("NaN")
case int:
if validator.Constraint >= float64(val) {
return NewValidationError("greaterThan", strconv.Itoa(val), strconv.FormatFloat(validator.Constraint, 'f', -1, 64))
}
case float64:
if validator.Constraint >= val {
return NewValidationError("greaterThan", strconv.FormatFloat(val, 'f', -1, 64), strconv.FormatFloat(validator.Constraint, 'f', -1, 64))
}
}
return nil
}