-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharray.go
114 lines (97 loc) · 2.74 KB
/
array.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
package vjson
import (
"encoding/json"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
)
// ArrayField is the type for validating arrays in a JSON
type ArrayField struct {
name string
required bool
items Field
minLength int
minLengthValidation bool
maxLength int
maxLengthValidation bool
}
// To Force Implementing Field interface by ArrayField
var _ Field = (*ArrayField)(nil)
// GetName returns name of the field
func (a *ArrayField) GetName() string {
return a.name
}
// Validate is used for validating a value. it returns an error if the value is invalid.
func (a *ArrayField) Validate(v interface{}) error {
if v == nil {
if !a.required {
return nil
}
return errors.Errorf("Value for %s field is required", a.name)
}
values, ok := v.([]interface{})
if !ok {
return errors.Errorf("Value of %s should be array", a.name)
}
var result error
if a.minLengthValidation {
if len(values) < a.minLength {
result = multierror.Append(result, errors.Errorf("length of %s array should be at least %d", a.name, a.minLength))
}
}
if a.maxLengthValidation {
if len(values) > a.maxLength {
result = multierror.Append(result, errors.Errorf("length of %s array should be at most %d", a.name, a.maxLength))
}
}
for _, value := range values {
err := a.items.Validate(value)
if err != nil {
result = multierror.Append(result, errors.Wrapf(err, "%v item is invalid in %s array", value, a.name))
}
}
return result
}
// Required is called to make a field required in a JSON
func (a *ArrayField) Required() *ArrayField {
a.required = true
return a
}
// MinLength is called to set minimum length for an array field in a JSON
func (a *ArrayField) MinLength(length int) *ArrayField {
a.minLength = length
a.minLengthValidation = true
return a
}
// MaxLength is called to set maximum length for an array field in a JSON
func (a *ArrayField) MaxLength(length int) *ArrayField {
a.maxLength = length
a.maxLengthValidation = true
return a
}
func (a *ArrayField) MarshalJSON() ([]byte, error) {
itemsRaw, err := json.Marshal(a.items)
if err != nil {
return nil, errors.Wrapf(err, "could not marshal items field of array field: %s", a.name)
}
items := make(map[string]interface{})
err = json.Unmarshal(itemsRaw, &items)
if err != nil {
return nil, errors.Wrapf(err, "could not unmarshal items field of array field: %s", a.name)
}
return json.Marshal(ArrayFieldSpec{
Name: a.name,
Type: arrayType,
Required: a.required,
Items: items,
MinLength: a.minLength,
MaxLength: a.maxLength,
})
}
// Array is the constructor of an array field.
func Array(name string, itemField Field) *ArrayField {
return &ArrayField{
name: name,
required: false,
items: itemField,
}
}