-
Notifications
You must be signed in to change notification settings - Fork 4
/
integer.go
184 lines (156 loc) · 4.21 KB
/
integer.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package vjson
import (
"encoding/json"
"fmt"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"strings"
)
type intRange struct {
start int
end int
}
// IntegerField is the type for validating integers in a JSON
type IntegerField struct {
name string
required bool
min int
minValidation bool
max int
maxValidation bool
signValidation bool
positive bool
rangeValidation bool
ranges []intRange
}
// To Force Implementing Field interface by IntegerField
var _ Field = (*IntegerField)(nil)
// GetName returns name of the field
func (i *IntegerField) GetName() string {
return i.name
}
// Validate is used for validating a value. it returns an error if the value is invalid.
func (i *IntegerField) Validate(v interface{}) error {
if v == nil {
if !i.required {
return nil
}
return errors.Errorf("Value for %s field is required", i.name)
}
var value int
var intOK bool
//gjson library returns float64 when field value is number
floatValue, floatOK := v.(float64)
value, intOK = v.(int)
if !floatOK && !intOK {
return errors.Errorf("Value for %s should be a number", i.name)
}
if floatOK {
value = int(floatValue)
}
var result error
if i.signValidation && i.positive {
if value < 0 {
result = multierror.Append(result, errors.Errorf("Value for %s should be a positive integer", i.name))
}
} else if i.signValidation && !i.positive {
if value > 0 {
result = multierror.Append(result, errors.Errorf("Value for %s should be a negative integer", i.name))
}
}
if i.minValidation {
if value < i.min {
result = multierror.Append(result, errors.Errorf("Value for %s should be at least %d", i.name, i.min))
}
}
if i.maxValidation {
if value > i.max {
result = multierror.Append(result, errors.Errorf("Value for %s should be at most %d", i.name, i.max))
}
}
if i.rangeValidation {
inRange := false
for _, r := range i.ranges {
if value >= r.start && value <= r.end {
inRange = true
break
}
}
if !inRange {
var ranges strings.Builder
for _, r := range i.ranges {
ranges.WriteString(fmt.Sprintf("[%d,%d] ", r.start, r.end))
}
result = multierror.Append(result, errors.Errorf("Value for %s should be in one of these ranges: %s", i.name, ranges.String()))
}
}
return result
}
// Required is called to make a field required in a JSON
func (i *IntegerField) Required() *IntegerField {
i.required = true
return i
}
// Positive is called when we want to force the value to be positive in validation.
func (i *IntegerField) Positive() *IntegerField {
i.signValidation = true
i.positive = true
return i
}
// Negative is called when we want to force the value to be negative in validation.
func (i *IntegerField) Negative() *IntegerField {
i.signValidation = true
i.positive = false
return i
}
// Min is called when we want to set a minimum value for an integer value in validation.
func (i *IntegerField) Min(value int) *IntegerField {
i.min = value
i.minValidation = true
return i
}
// Max is called when we want to set a maximum value for an integer value in validation.
func (i *IntegerField) Max(value int) *IntegerField {
i.max = value
i.maxValidation = true
return i
}
// Range is called when we want to define valid ranges for an integer value in validation.
func (i *IntegerField) Range(start, end int) *IntegerField {
i.ranges = append(i.ranges, intRange{start: start, end: end})
i.rangeValidation = true
return i
}
func (i *IntegerField) MarshalJSON() ([]byte, error) {
ranges := make([]IntRangeSpec, 0, len(i.ranges))
for _, r := range i.ranges {
ranges = append(ranges, IntRangeSpec{
Start: r.start,
End: r.end,
})
}
return json.Marshal(IntegerFieldSpec{
Name: i.name,
Required: i.required,
Min: i.min,
Max: i.max,
Positive: i.positive,
Ranges: ranges,
Type: integerType,
})
}
// Integer is the constructor of an integer field
func Integer(name string) *IntegerField {
return &IntegerField{
name: name,
required: false,
min: 0,
minValidation: false,
max: 0,
maxValidation: false,
signValidation: false,
positive: false,
rangeValidation: false,
ranges: []intRange{},
}
}