forked from rs/rest-layer
/
dict.go
69 lines (65 loc) · 1.67 KB
/
dict.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
package schema
import (
"errors"
"fmt"
)
// Dict validates array values
type Dict struct {
// KeysValidator is the validator to apply on dict keys
KeysValidator FieldValidator
// ValuesValidator is the validator to apply on dict values
ValuesValidator FieldValidator
// MinLen defines the minimum number of fields (default 0)
MinLen int
// MaxLen defines the maximum number of fields (default no limit)
MaxLen int
}
// Compile implements Compiler interface
func (v *Dict) Compile() (err error) {
if c, ok := v.KeysValidator.(Compiler); ok {
if err = c.Compile(); err != nil {
return
}
}
if c, ok := v.ValuesValidator.(Compiler); ok {
if err = c.Compile(); err != nil {
return
}
}
return
}
// Validate implements FieldValidator interface
func (v Dict) Validate(value interface{}) (interface{}, error) {
dict, ok := value.(map[string]interface{})
if !ok {
return nil, errors.New("not a dict")
}
dest := map[string]interface{}{}
for key, val := range dict {
if v.KeysValidator != nil {
nkey, err := v.KeysValidator.Validate(key)
if err != nil {
return nil, fmt.Errorf("invalid key `%s': %s", key, err)
}
if key, ok = nkey.(string); !ok {
return nil, errors.New("key validator does not return string")
}
}
if v.ValuesValidator != nil {
var err error
val, err = v.ValuesValidator.Validate(val)
if err != nil {
return nil, fmt.Errorf("invalid value for key `%s': %s", key, err)
}
}
dest[key] = val
}
l := len(dest)
if l < v.MinLen {
return nil, fmt.Errorf("has fewer properties than %d", v.MinLen)
}
if v.MaxLen > 0 && l > v.MaxLen {
return nil, fmt.Errorf("has more properties than %d", v.MaxLen)
}
return dest, nil
}