-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.go
57 lines (48 loc) · 1.22 KB
/
validators.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
package model
import (
"errors"
"github.com/cheekybits/m"
)
var errRequired = errors.New("is required")
// IsRequired checks to make sure key is present and the value
// is non-nil.
func IsRequired(data map[string]interface{}, keypath string) error {
var v interface{}
var ok bool
if v, ok = m.GetOK(data, keypath); !ok {
return errRequired
}
if v == nil {
return errRequired
}
return nil
}
type errUnexpectedField string
func (f errUnexpectedField) Error() string {
return "unexpected '" + string(f) + "'"
}
// Strict is a Before function that ensures there are no extra data
// fields in the model.
func Strict(m M, data map[string]interface{}) error {
for k := range data {
if _, present := m[k]; !present {
return errUnexpectedField(k)
}
}
return nil
}
var errNotNumber = errors.New("must be a number")
// IsNumber checks to make sure the value is a number.
// Remains silent if the data is not present.
func IsNumber(data map[string]interface{}, keypath string) error {
if v, ok := m.GetOK(data, keypath); ok {
switch v.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64, complex64, complex128:
return nil
}
return errNotNumber
}
return nil
}