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
191
192
193
194
195
196
197
package apigen
import (
"cmp"
"fmt"
"math"
"regexp"
"slices"
"sort"
"strings"
"unicode/utf8"
)
type Issue struct {
Path string
Message string
}
func (i Issue) String() string {
path := i.Path
path = cmp.Or(path, "(root)")
return path + ": " + i.Message
}
const (
msgMissing = "missing required field"
msgUnknown = "unknown field"
)
func (m *Model) Validate(typeName string, value any) []Issue {
s, ok := m.Named[typeName]
if !ok {
return []Issue{{Message: fmt.Sprintf("no type named %s", typeName)}}
}
var out []Issue
check(s, value, "", &out)
return out
}
func joinPath(path, key string) string {
if path == "" {
return key
}
return path + "." + key
}
func describe(v any) string {
switch v.(type) {
case nil:
return "null"
case map[string]any:
return "an object"
case []any:
return "an array"
case string:
return "a string"
case bool:
return "a boolean"
case float64:
return "a number"
default:
return fmt.Sprintf("%T", v)
}
}
func expected(want string, got any) string {
return fmt.Sprintf("expected %s, got %s", want, describe(got))
}
func check(s *Schema, value any, path string, out *[]Issue) {
add := func(p, msg string) { *out = append(*out, Issue{Path: p, Message: msg}) }
switch s.Type {
case TypeObject:
obj, ok := value.(map[string]any)
if !ok {
add(path, expected("an object", value))
return
}
if s.PreserveUnknown {
return
}
if s.AdditionalProperties != nil {
keys := make([]string, 0, len(obj))
for k := range obj {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
check(s.AdditionalProperties, obj[k], joinPath(path, k), out)
}
return
}
if !s.Closed {
return
}
declared := map[string]bool{}
for _, p := range s.Properties {
declared[p.Name] = true
v, present := obj[p.Name]
if !present {
if p.Required {
add(joinPath(path, p.Name), msgMissing)
}
continue
}
check(p.Schema, v, joinPath(path, p.Name), out)
}
var unknown []string
for k := range obj {
if !declared[k] {
unknown = append(unknown, k)
}
}
sort.Strings(unknown)
for _, k := range unknown {
add(joinPath(path, k), msgUnknown)
}
case TypeArray:
arr, ok := value.([]any)
if !ok {
add(path, expected("an array", value))
return
}
for i, v := range arr {
check(s.Items, v, fmt.Sprintf("%s[%d]", path, i), out)
}
checkList(s, arr, path, add)
case TypeString:
str, ok := value.(string)
if !ok {
add(path, expected("a string", value))
return
}
if s.MaxLength != nil && int64(utf8.RuneCountInString(str)) > *s.MaxLength {
add(path, fmt.Sprintf("%d characters is over the maxLength %d", utf8.RuneCountInString(str), *s.MaxLength))
}
if s.Pattern != "" {
if re, err := regexp.Compile(s.Pattern); err == nil && !re.MatchString(str) {
add(path, fmt.Sprintf("%q does not match the pattern %s", str, s.Pattern))
}
}
if len(s.Enum) == 0 {
return
}
if slices.Contains(s.Enum, str) {
return
}
add(path, fmt.Sprintf("%q is not one of %s", str, strings.Join(s.Enum, ", ")))
case TypeInteger:
n, ok := value.(float64)
if !ok || n != math.Trunc(n) || math.IsInf(n, 0) {
add(path, expected("an integer", value))
return
}
if s.Minimum != nil && n < float64(*s.Minimum) {
add(path, fmt.Sprintf("%v is below the minimum %d", n, *s.Minimum))
}
if s.Maximum != nil && n > float64(*s.Maximum) {
add(path, fmt.Sprintf("%v is above the maximum %d", n, *s.Maximum))
}
case TypeBoolean:
if _, ok := value.(bool); !ok {
add(path, expected("a boolean", value))
}
default:
add(path, fmt.Sprintf("schema type %q has no validator", s.Type))
}
}
func checkList(s *Schema, arr []any, path string, add func(p, msg string)) {
switch s.ListType {
case ListTypeSet:
seen := map[any]bool{}
for i, v := range arr {
if seen[v] {
add(fmt.Sprintf("%s[%d]", path, i), "duplicate entry in a set")
}
seen[v] = true
}
case ListTypeMap:
seen := map[string]bool{}
for i, v := range arr {
obj, ok := v.(map[string]any)
if !ok {
continue
}
var key strings.Builder
for _, k := range s.ListMapKeys {
key.WriteString(fmt.Sprintf("%v\x00", obj[k]))
}
if seen[key.String()] {
add(fmt.Sprintf("%s[%d]", path, i), "duplicate map key "+strings.Join(s.ListMapKeys, ","))
}
seen[key.String()] = true
}
}
}