-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_validation.go
More file actions
190 lines (166 loc) · 5.06 KB
/
schema_validation.go
File metadata and controls
190 lines (166 loc) · 5.06 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package rigging
import (
"fmt"
"reflect"
"strconv"
"sync"
)
var schemaValidationCache sync.Map
func configType[T any]() reflect.Type {
return reflect.TypeOf((*T)(nil)).Elem()
}
func cloneFieldErrors(errors []FieldError) []FieldError {
if len(errors) == 0 {
return nil
}
cloned := make([]FieldError, len(errors))
copy(cloned, errors)
return cloned
}
func getSchemaValidationErrors(rootType reflect.Type) []FieldError {
rootType = dereferenceType(rootType)
if rootType == nil || rootType.Kind() != reflect.Struct {
return nil
}
if cached, ok := schemaValidationCache.Load(rootType); ok {
if errors, ok := cached.([]FieldError); ok {
return cloneFieldErrors(errors)
}
}
errors := validateSchemaType(rootType, "")
schemaValidationCache.Store(rootType, cloneFieldErrors(errors))
return cloneFieldErrors(errors)
}
func validateSchemaType(rootType reflect.Type, parentFieldPath string) []FieldError {
rootType = dereferenceType(rootType)
if rootType == nil || rootType.Kind() != reflect.Struct {
return nil
}
var errors []FieldError
for _, meta := range getStructFieldMeta(rootType) {
field := meta.field
fieldType := field.Type
fieldPath := field.Name
if parentFieldPath != "" {
fieldPath = parentFieldPath + "." + field.Name
}
errors = append(errors, validateTagSchema(fieldType, fieldPath, meta.tagCfg)...)
if isOptionalType(fieldType) {
innerType := fieldType.Field(0).Type
if innerType.Kind() == reflect.Struct && innerType.PkgPath() != "time" {
errors = append(errors, validateSchemaType(innerType, fieldPath)...)
}
continue
}
if fieldType.Kind() == reflect.Struct && fieldType.PkgPath() != "time" {
errors = append(errors, validateSchemaType(fieldType, fieldPath)...)
}
}
return errors
}
func validateTagSchema(fieldType reflect.Type, fieldPath string, tags tagConfig) []FieldError {
var errors []FieldError
for _, msg := range tags.parseErrors {
errors = append(errors, FieldError{
FieldPath: fieldPath,
Code: ErrCodeInvalidTag,
Message: msg,
})
}
errors = append(errors, validateMinMaxTagSchema(fieldType, fieldPath, tags)...)
return errors
}
func validateMinMaxTagSchema(fieldType reflect.Type, fieldPath string, tags tagConfig) []FieldError {
if fieldType == nil {
return nil
}
if isOptionalType(fieldType) {
fieldType = fieldType.Field(0).Type
}
var errors []FieldError
switch fieldType.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if tags.min != "" {
if _, err := strconv.ParseInt(tags.min, 10, 64); err != nil {
errors = append(errors, FieldError{
FieldPath: fieldPath,
Code: ErrCodeInvalidTag,
Message: fmt.Sprintf("invalid min constraint %q: must be a valid integer", tags.min),
})
}
}
if tags.max != "" {
if _, err := strconv.ParseInt(tags.max, 10, 64); err != nil {
errors = append(errors, FieldError{
FieldPath: fieldPath,
Code: ErrCodeInvalidTag,
Message: fmt.Sprintf("invalid max constraint %q: must be a valid integer", tags.max),
})
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if tags.min != "" {
if _, err := strconv.ParseUint(tags.min, 10, 64); err != nil {
errors = append(errors, FieldError{
FieldPath: fieldPath,
Code: ErrCodeInvalidTag,
Message: fmt.Sprintf("invalid min constraint %q: must be a valid unsigned integer", tags.min),
})
}
}
if tags.max != "" {
if _, err := strconv.ParseUint(tags.max, 10, 64); err != nil {
errors = append(errors, FieldError{
FieldPath: fieldPath,
Code: ErrCodeInvalidTag,
Message: fmt.Sprintf("invalid max constraint %q: must be a valid unsigned integer", tags.max),
})
}
}
case reflect.Float32, reflect.Float64:
if tags.min != "" {
if _, err := strconv.ParseFloat(tags.min, 64); err != nil {
errors = append(errors, FieldError{
FieldPath: fieldPath,
Code: ErrCodeInvalidTag,
Message: fmt.Sprintf("invalid min constraint %q: must be a valid number", tags.min),
})
}
}
if tags.max != "" {
if _, err := strconv.ParseFloat(tags.max, 64); err != nil {
errors = append(errors, FieldError{
FieldPath: fieldPath,
Code: ErrCodeInvalidTag,
Message: fmt.Sprintf("invalid max constraint %q: must be a valid number", tags.max),
})
}
}
case reflect.String:
if tags.min != "" {
if _, err := strconv.Atoi(tags.min); err != nil {
errors = append(errors, FieldError{
FieldPath: fieldPath,
Code: ErrCodeInvalidTag,
Message: fmt.Sprintf("invalid min constraint %q: must be a valid integer", tags.min),
})
}
}
if tags.max != "" {
if _, err := strconv.Atoi(tags.max); err != nil {
errors = append(errors, FieldError{
FieldPath: fieldPath,
Code: ErrCodeInvalidTag,
Message: fmt.Sprintf("invalid max constraint %q: must be a valid integer", tags.max),
})
}
}
}
return errors
}
func dereferenceType(t reflect.Type) reflect.Type {
for t != nil && t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t
}