-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloat.go
More file actions
143 lines (121 loc) · 2.79 KB
/
float.go
File metadata and controls
143 lines (121 loc) · 2.79 KB
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
package check
import "fmt"
// FloatSchema validates float64 values.
type FloatSchema struct {
optional bool
defaultVal *float64
min *float64
max *float64
positive bool
negative bool
customFns []func(float64) error
}
func NewFloat() *FloatSchema {
return &FloatSchema{}
}
// clone returns a copy of this schema to preserve immutability in chains.
func (s *FloatSchema) clone() *FloatSchema {
copy := *s
if s.customFns != nil {
copyCustom := make([]func(float64) error, len(s.customFns))
for i, v := range s.customFns {
copyCustom[i] = v
}
copy.customFns = copyCustom
}
return ©
}
func (s *FloatSchema) Optional() *FloatSchema {
s = s.clone()
s.optional = true
return s
}
func (s *FloatSchema) Default(v float64) *FloatSchema {
s = s.clone()
s.optional = true
s.defaultVal = &v
return s
}
func (s *FloatSchema) Min(n float64) *FloatSchema {
s = s.clone()
s.min = &n
return s
}
func (s *FloatSchema) Max(n float64) *FloatSchema {
s = s.clone()
s.max = &n
return s
}
func (s *FloatSchema) Positive() *FloatSchema {
s = s.clone()
s.positive = true
return s
}
func (s *FloatSchema) Negative() *FloatSchema {
s = s.clone()
s.negative = true
return s
}
func (s *FloatSchema) Custom(fn func(float64) error) *FloatSchema {
s = s.clone()
s.customFns = append(s.customFns, fn)
return s
}
func (s *FloatSchema) IsOptional() bool { return s.optional }
func (s *FloatSchema) Parse(value any) (any, error) {
errs := s.Validate(value)
if len(errs) > 0 {
return nil, ValidationErrors(errs)
}
if value == nil && s.defaultVal != nil {
return *s.defaultVal, nil
}
if value == nil {
return nil, nil
}
f, _ := toFloat64(value)
return f, nil
}
func (s *FloatSchema) Validate(value any) []ValidationError {
if value == nil {
if s.optional {
return nil
}
return []ValidationError{{Message: "required value is missing"}}
}
f, ok := toFloat64(value)
if !ok {
return []ValidationError{{Message: fmt.Sprintf("expected number, got %T", value), Value: value}}
}
var errs []ValidationError
if s.min != nil && f < *s.min {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("must be at least %g (got %g)", *s.min, f),
Value: f,
})
}
if s.max != nil && f > *s.max {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("must be at most %g (got %g)", *s.max, f),
Value: f,
})
}
if s.positive && f <= 0 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("must be positive (got %g)", f),
Value: f,
})
}
if s.negative && f >= 0 {
errs = append(errs, ValidationError{
Message: fmt.Sprintf("must be negative (got %g)", f),
Value: f,
})
}
for _, fn := range s.customFns {
if err := fn(f); err != nil {
errs = append(errs, ValidationError{Message: err.Error(), Value: f})
}
}
return errs
}