/
validator.go
283 lines (238 loc) · 6.1 KB
/
validator.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package jstn
import (
"bytes"
"encoding/json"
"io"
"log"
)
// Valid indicates whether the JSON document in is considered valid with
// respect to the JSTN structure t.
func Valid(t Type, in json.RawMessage) bool {
d := json.NewDecoder(bytes.NewReader(in))
d.UseNumber()
// assert that the next json object matches the type
if ok := valid(d, t); !ok {
return false
}
// assert that all data has been parsed
if _, err := d.Token(); err != io.EOF {
return false
}
return true
}
// valid indicates whether the next JSON value in the Decoder has the structure
// described by t.
func valid(d *json.Decoder, t Type) bool {
switch t.Kind {
case String:
if ok := validString(d, t); !ok {
return false
}
case Number:
if ok := validNumber(d, t); !ok {
return false
}
case Boolean:
if ok := validBoolean(d, t); !ok {
return false
}
case Null:
if ok := validNull(d, t); !ok {
return false
}
case Array:
if ok := validArray(d, t); !ok {
return false
}
case Object:
if ok := validObject(d, t); !ok {
return false
}
default:
return false
}
return true
}
// validString indicates whether the next token in the JSON is a valid string.
func validString(d *json.Decoder, t Type) bool {
tok, err := d.Token()
if err != nil {
if t.Optional && err == io.EOF {
// no token at all, but it's optional so that's okay
return true
}
log.Printf("validation failed: string: got error: %s\n", err)
return false
}
switch tok.(type) {
case string:
return true
case nil:
if t.Optional {
return true
} else {
log.Println("validation failed: string: got nil for required value")
return false
}
default:
log.Printf("validation failed: string: unexpected type %T: %v\n", tok, tok)
return false
}
}
// validNumber indicates whether the next token in the JSON is a valid number.
func validNumber(d *json.Decoder, t Type) bool {
tok, err := d.Token()
if err != nil {
if t.Optional && err == io.EOF {
// no token at all, but it's optional so that's okay
return true
}
log.Printf("validation failed: number: got error: %s\n", err)
return false
}
switch tok.(type) {
case json.Number:
return true
case nil:
if t.Optional {
return true
} else {
log.Println("validation failed: number: got nil for required value")
return false
}
default:
log.Printf("validation failed: number: unexpected type %T: %v\n", tok, tok)
return false
}
}
// validBoolean indicates whether the next token in the JSON is a valid boolean.
func validBoolean(d *json.Decoder, t Type) bool {
tok, err := d.Token()
if err != nil {
if t.Optional && err == io.EOF {
// no token at all, but it's optional so that's okay
return true
}
log.Printf("validation failed: boolean: got error: %s\n", err)
return false
}
switch tok.(type) {
case bool:
return true
case nil:
if t.Optional {
return true
} else {
log.Println("validation failed: boolean: got nil for required value")
return false
}
default:
log.Printf("validation failed: boolean: unexpected type %T: %v\n", tok, tok)
return false
}
}
// validNull indicates whether the next token in the JSON is a valid null value.
func validNull(d *json.Decoder, t Type) bool {
tok, err := d.Token()
if err != nil {
if t.Optional && err == io.EOF {
// no token at all, but it's optional so that's okay
return true
}
log.Printf("validation failed: null: got error: %s\n", err)
return false
}
switch tok.(type) {
case nil:
return true
default:
log.Printf("validation failed: null: unexpected type %T: %v\n", tok, tok)
return false
}
}
// validArray indicates whether the next token in the JSON is a valid array.
func validArray(d *json.Decoder, t Type) bool {
tok, err := d.Token()
if err != nil {
if t.Optional && err == io.EOF {
// no token at all, but it's optional so that's okay
return true
}
log.Printf("validation failed: array: got error: %s\n", err)
return false
}
if delim, ok := tok.(json.Delim); !ok || delim != json.Delim('[') {
log.Printf("validation failed: array: unexpected delimiter: %s\n", delim)
return false
}
// schedule the consumption of the ending ']'
defer d.Token()
for d.More() {
if t.Items == nil {
log.Println("validation failed: array: array is not empty")
return false
}
if ok := valid(d, *t.Items); !ok {
log.Println("validation failed: array: invalid sub-element")
return false
}
}
return true
}
// validObject indicates whether the next token in the JSON is a valid object.
func validObject(d *json.Decoder, t Type) bool {
tok, err := d.Token()
if err != nil {
if t.Optional && err == io.EOF {
// no token at all, but it's optional so that's okay
return true
}
log.Printf("validation failed: object: got error: %s\n", err)
return false
}
if delim, ok := tok.(json.Delim); !ok || delim != json.Delim('{') {
log.Printf("validation failed: object: unexpected delimiter: %s\n", delim)
return false
}
// schedule the consumption of the ending '}'
defer d.Token()
// note which properties we need to find
necessaryProps := make(map[string]bool)
for k := range t.Properties {
necessaryProps[k] = true
}
for d.More() {
// parse out a key
keyTok, err := d.Token()
if err != nil {
log.Printf("validation failed: object: failed to read key: %s\n", err)
return false
}
// it better be a string
keyTokStr, keyOk := keyTok.(string)
if !keyOk {
log.Printf("validation failed: object: non-string key: %s\n", keyTok)
return false
}
// look up the type for this property
propType, ok := t.Properties[keyTokStr]
if !ok {
log.Printf("validation failed: object: contains undeclared property %q\n", keyTokStr)
return false
}
// mark this property as visited
delete(necessaryProps, keyTokStr)
if ok := valid(d, *propType); !ok {
log.Println("validation failed: object: invalid sub-element")
return false
}
}
// make sure that any not-located properties were optional
for k := range necessaryProps {
if opt := t.Properties[k].Optional; !opt {
log.Printf("validation failed: object: missing required property: %q\n", k)
return false
}
}
return true
}