forked from miladibra10/vjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.go
166 lines (138 loc) · 3.79 KB
/
float.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
package vjson
import (
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
)
type floatRange struct {
start float64
end float64
}
// FloatField is the type for validating floats in a JSON
type FloatField struct {
name string
required bool
min float64
minValidation bool
max float64
maxValidation bool
signValidation bool
positive bool
rangeValidation bool
ranges []floatRange
}
// To Force Implementing Field interface by IntegerField
var _ Field = (*FloatField)(nil)
// GetName returns true if field is required
func (f *FloatField) GetName() string {
return f.name
}
// GetType returns the Fields type
func (f *FloatField) GetType() string {
return "float"
}
// GetRequired returns the Fields type
func (f *FloatField) GetRequired() bool {
return f.required
}
// Validate is used for validating a value. it returns an error if the value is invalid.
func (f *FloatField) Validate(v interface{}) error {
if v == nil {
if !f.required {
return nil
}
return errors.Errorf("Value for %s field is required", f.name)
}
value, ok := v.(float64)
if !ok {
return errors.Errorf("Value for %s should be a float number", f.name)
}
var result error
if f.signValidation && f.positive {
if value < 0 {
result = multierror.Append(result, errors.Errorf("Value for %s should be a positive float", f.name))
}
} else if f.signValidation && !f.positive {
if value > 0 {
result = multierror.Append(result, errors.Errorf("Value for %s should be a negative float", f.name))
}
}
if f.minValidation {
if value < f.min {
result = multierror.Append(result, errors.Errorf("Value for %s should be at least %f", f.name, f.min))
}
}
if f.maxValidation {
if value > f.max {
result = multierror.Append(result, errors.Errorf("Value for %s should be at most %f", f.name, f.max))
}
}
if f.rangeValidation {
inRange := false
for _, r := range f.ranges {
if value >= r.start && value <= r.end {
inRange = true
break
}
}
if !inRange {
var ranges strings.Builder
for _, r := range f.ranges {
ranges.WriteString(fmt.Sprintf("[%f,%f] ", r.start, r.end))
}
result = multierror.Append(result, errors.Errorf("Value for %s should be in one of these ranges: %s", f.name, ranges.String()))
}
}
return result
}
// Required is called to make a field required in a JSON
func (f *FloatField) Required() *FloatField {
f.required = true
return f
}
// Positive is called when we want to force the value to be positive in validation.
func (f *FloatField) Positive() *FloatField {
f.signValidation = true
f.positive = true
return f
}
// Negative is called when we want to force the value to be negative in validation.
func (f *FloatField) Negative() *FloatField {
f.signValidation = true
f.positive = false
return f
}
// Min is called when we want to set a minimum value for a float value in validation.
func (f *FloatField) Min(value float64) *FloatField {
f.min = value
f.minValidation = true
return f
}
// Max is called when we want to set a maximum value for a float value in validation.
func (f *FloatField) Max(value float64) *FloatField {
f.max = value
f.maxValidation = true
return f
}
// Range is called when we want to define valid ranges for a float value in validation.
func (f *FloatField) Range(start, end float64) *FloatField {
f.ranges = append(f.ranges, floatRange{start: start, end: end})
f.rangeValidation = true
return f
}
// Float is the constructor of a float field
func Float(name string) *FloatField {
return &FloatField{
name: name,
required: false,
min: 0,
minValidation: false,
max: 0,
maxValidation: false,
signValidation: false,
positive: false,
rangeValidation: false,
ranges: []floatRange{},
}
}