-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.go
41 lines (34 loc) · 978 Bytes
/
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
package valider
import "strconv"
type Float struct {
value float64
field string
errors Errors
}
func (v *Validator) Float(value float64, field string) *Float {
return &Float{value, field, v.Errors}
}
func (f *Float) Required() *Float {
if f.value == 0 {
f.errors[f.field] = append(f.errors[f.field], Error{ErrRequired, CodeRequired, nil})
}
return f
}
func (f *Float) Len(num int) *Float {
if f.value != 0 && len(strconv.FormatFloat(f.value, 'f', -1, 64)) != num {
f.errors[f.field] = append(f.errors[f.field], Error{ErrLen, CodeLen, num})
}
return f
}
func (f *Float) Equal(eq float64) *Float {
if f.value != 0 && f.value != eq {
f.errors[f.field] = append(f.errors[f.field], Error{ErrNotEqual, CodeNotEqual, eq})
}
return f
}
func (f *Float) Range(min, max float64) *Float {
if f.value != 0 && (f.value < min || f.value > max) {
f.errors[f.field] = append(f.errors[f.field], Error{ErrOutRange, CodeOutRange, []float64{min, max}})
}
return f
}