-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequired.go
166 lines (148 loc) · 3.71 KB
/
required.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package validator
import (
"errors"
"fmt"
"reflect"
"regexp"
"github.com/gocraft/dbr"
)
var (
rSpace = regexp.MustCompile("^[[:space:]]*$")
RequiredDefinitionEmptyError = errors.New("the required value should have at least one element")
RequiredDefinitionDuplicationError = errors.New("the required value should not be duplicated")
)
type RequiredValidator struct {
definition RequiredValidatorDefinition
}
type RequiredValidatorDefinition struct {
Required []string `json:"pattern"`
}
type RequiredValidationError struct {
Input interface{} `json:"input"`
Definition RequiredValidatorDefinition `json:"definition"`
}
func (r RequiredValidationError) Error() string {
return fmt.Sprintf("input struct does not satisfy required values '%v'\n", r.Definition.Required)
}
func NewRequiredValidator(definition RequiredValidatorDefinition) (RequiredValidator, error) {
required := definition.Required
len := len(required)
if len == 0 {
return RequiredValidator{}, RequiredDefinitionEmptyError
}
for i := 0; i < len-1; i++ {
key := required[i]
for j := i + 1; j < len; j++ {
if required[j] == key {
return RequiredValidator{}, RequiredDefinitionDuplicationError
}
}
}
return RequiredValidator{definition}, nil
}
// Validate returns whether input is valid against required keys.
// The fields of input must be public.
func (r RequiredValidator) Validate(input interface{}) error {
v, ok := convertToConcreteValue(reflect.ValueOf(input))
if !ok {
return &InvalidTypeError{
Definition: r.definition,
Input: input,
}
}
if v.Kind() != reflect.Struct {
return &InvalidTypeError{
Definition: r.definition,
Input: input,
}
}
for _, key := range r.definition.Required {
e, ok := getFieldByName(v, key)
if !ok {
return &InvalidFieldTypeError{
Definition: r.definition,
Input: input,
}
}
c, ok := convertToConcreteValue(e)
if !ok {
return &InvalidFieldTypeError{
Definition: r.definition,
Input: input,
}
}
if ok = isValid(c.Interface()); !ok {
return &RequiredValidationError{
Definition: r.definition,
Input: input,
}
}
}
return nil
}
// convertToConcreteValue returns a concrete value that stored in the pointer.
// The ok reports whether conversion was successful.
func convertToConcreteValue(input reflect.Value) (value reflect.Value, ok bool) {
if input.Kind() != reflect.Ptr {
return input, true
}
if input.IsNil() {
return reflect.Value{}, false
}
return input.Elem(), true
}
// getFieldByName returns the struct field with the given name.
// The ok reports whether field with key was found in value.
func getFieldByName(s reflect.Value, key string) (value reflect.Value, ok bool) {
value = s.FieldByName(key)
if (value == reflect.Value{}) {
return value, false
}
return value, true
}
func isPresentString(s string) bool {
return !rSpace.MatchString(s)
}
func isPresentArray(v reflect.Value) bool {
if v.Len() == 0 {
return false
}
return true
}
func isPresentStruct(v reflect.Value) bool {
if v.NumField() == 0 {
return false
}
return true
}
// isValid returns whether i is valid.
// The type of i should be dbr.Null* or primitive.
func isValid(i interface{}) (ok bool) {
switch t := i.(type) {
case dbr.NullString:
if !t.Valid {
return false
}
return isPresentString(t.String)
case dbr.NullInt64:
return t.Valid
case dbr.NullFloat64:
return t.Valid
case dbr.NullBool:
return t.Valid
case dbr.NullTime:
return t.Valid
case string:
return isPresentString(t)
default:
v := reflect.ValueOf(t)
switch v.Kind() {
case reflect.Struct:
return isPresentStruct(v)
case reflect.Array, reflect.Slice:
return isPresentArray(v)
default:
return true
}
}
}