-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.rkt
407 lines (340 loc) · 11.7 KB
/
schema.rkt
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#lang racket/base
(require (only-in (file "regexp.rkt")
ecma-262-regexp?)
(only-in (file "json.rkt")
json-non-negative-integer?
has-property?
property-value
object-properties
object-values)
"equal.rkt"
(only-in (file "util.rkt")
intersection
complain-and-die
file-content/bytes
bytes->string)
json
(only-in net/url-string
string->url)
(only-in net/url-structs
url-fragment)
(only-in racket/cmdline
command-line)
(only-in racket/list
empty?
check-duplicates)
(only-in (file "format.rkt")
uri-reference?
json-pointer?)
racket/contract
racket/port
(only-in sugar
members-unique?))
(module+ test
(require rackunit))
(define/contract json-schema-types
(listof string?)
(list "object"
"array"
"string"
"boolean"
"integer"
"number"
"null"))
(define/contract (json-schema-type? thing)
(any/c . -> . boolean?)
(and (string? thing)
(list? (member thing json-schema-types string=?))))
(define/contract (acceptable-value-for-type? value)
(jsexpr? . -> . boolean?)
(or (and (string? value)
(json-schema-type? value))
(and (list? value)
(members-unique? value)
(andmap json-schema-type? value))))
(define/contract (acceptable-value-for-multipleOf? value)
(jsexpr? . -> . boolean?)
(and (real? value)
(> value 0)))
(define (acceptable-value-for-maximum? value)
(real? value))
(define (acceptable-value-for-exclusiveMaximum? value)
(real? value))
(define (acceptable-value-for-minimum? value)
(real? value))
(define (acceptable-value-for-exclusiveMinimum? value)
(real? value))
(define (acceptable-value-for-maxLength? value)
(json-non-negative-integer? value))
(define (acceptable-value-for-minLength? value)
(json-non-negative-integer? value))
(define (acceptable-value-for-pattern? value)
(and (string? value)
(ecma-262-regexp? value)))
(define (acceptable-value-for-items? value)
(cond ((list? value)
(andmap json-schema? value))
((hash? value)
(json-schema? value))
(else
#f)))
(define (acceptable-value-for-addditionalItems? value)
(json-schema? value))
(define (acceptable-value-for-maxItems? value)
(json-non-negative-integer? value))
(define (acceptable-value-for-minItems? value)
(json-non-negative-integer? value))
(define (acceptable-value-for-uniqueItems? value)
(boolean? value))
(define (acceptable-value-for-contains? value)
(json-schema? value))
(define (acceptable-value-for-maxProperties? value)
(json-non-negative-integer? value))
(define (acceptable-value-for-minProperties? value)
(json-non-negative-integer? value))
(define (acceptable-value-for-required? value)
(and (list? value)
(andmap string? value)
(members-unique? value)))
(define (acceptable-value-for-properties? value)
(and (hash? value)
(andmap json-schema? (object-values value))))
(define (acceptable-value-for-patternProperties? value)
(and (hash? value)
(andmap ecma-262-regexp? (map symbol->string (object-properties value)))
(andmap json-schema? (object-values value))))
(define (acceptable-value-for-additionalProperties? value)
(json-schema? value))
(define (acceptable-value-for-dependencies? value)
(and (hash? value)
(andmap (lambda (x)
(or (json-schema? x)
(and (list? x)
(andmap string? x)
(members-unique? x))))
(object-values value))))
(define (acceptable-value-for-propertyNames? value)
(json-schema? value))
(define (acceptable-value-for-enum? value)
(and (list? value)
(not (empty? value))
(not (check-duplicates value equal-jsexprs?))))
(define (acceptable-value-for-const? value)
(jsexpr? value))
(define (acceptable-value-for-allOf? value)
(and (list? value)
(not (empty? value))
(andmap json-schema? value)))
(define (acceptable-value-for-anyOf? value)
(and (list? value)
(not (empty? value))
(andmap json-schema? value)))
(define (acceptable-value-for-oneOf? value)
(and (list? value)
(not (empty? value))
(andmap json-schema? value)))
(define (acceptable-value-for-not? value)
(json-schema? value))
(define (acceptable-value-for-definitions? value)
(and (hash? value)
(andmap json-schema?
(object-values value))))
(define (acceptable-value-for-title? value)
(string? value))
(define (acceptable-value-for-description? value)
(string? value))
(define (acceptable-value-for-default? value)
#t)
(define (acceptable-value-for-examples? value)
(list? value))
(define (acceptable-value-for-format? value)
(member value
(list "date-time"
"email"
"hostname"
"ipv4"
"ipv6"
"uri"
"uri-reference"
"uri-template"
"json-pointer")))
(define (acceptable-value-for-$ref? value)
(and (string? value)
(let* ([u (string->url value)]
[f (url-fragment u)])
(or (eq? #f f)
(json-pointer? f)))))
(module+ test
(check-true (acceptable-value-for-$ref? "#/definitions/positiveInteger")))
(define (acceptable-value-for-$schema? value)
(string? value))
(define json-schema-validators
(hasheq 'multipleOf acceptable-value-for-multipleOf?
'maximum acceptable-value-for-maximum?
'exclusiveMaximum acceptable-value-for-exclusiveMaximum?
'minimum acceptable-value-for-minimum?
'exclusiveMinimum acceptable-value-for-exclusiveMinimum?
'minLength acceptable-value-for-minLength?
'maxLength acceptable-value-for-maxLength?
'pattern acceptable-value-for-pattern?
'items acceptable-value-for-items?
'additionalItems acceptable-value-for-addditionalItems?
'maxItems acceptable-value-for-maxItems?
'minItems acceptable-value-for-minItems?
'uniqueItems acceptable-value-for-uniqueItems?
'contains acceptable-value-for-contains?
'maxProperties acceptable-value-for-maxProperties?
'minProperties acceptable-value-for-minProperties?
'required acceptable-value-for-required?
'properties acceptable-value-for-properties?
'patternProperties acceptable-value-for-patternProperties?
'additionalProperties acceptable-value-for-additionalProperties?
'dependencies acceptable-value-for-dependencies?
'propertyNames acceptable-value-for-propertyNames?
'enum acceptable-value-for-enum?
'const acceptable-value-for-const?
'type acceptable-value-for-type?
'allOf acceptable-value-for-allOf?
'anyOf acceptable-value-for-anyOf?
'oneOf acceptable-value-for-oneOf?
'not acceptable-value-for-not?
;; metadata validators
'definitions acceptable-value-for-definitions?
'title acceptable-value-for-title?
'description acceptable-value-for-description?
'default acceptable-value-for-default?
'examples acceptable-value-for-examples?
;; semantic validation
'format acceptable-value-for-format?
;; referring to other schemas
'$ref acceptable-value-for-$ref?
'$schema acceptable-value-for-$schema?
))
(define json-schema-keywords
(hash-keys json-schema-validators))
(module+ test
(define keywords
(list 'multipleOf
'maximum
'exclusiveMaximum
'minimum
'exclusiveMinimum
'maxLength
'minLength
'pattern
'items
'additionalItems
'maxItems
'minItems
'uniqueItems
'contains
'maxProperties
'minProperties
'required
'properties
'patternProperties
'additionalProperties
'dependencies
'propertyNames
'enum
'const
'type
'allOf
'anyOf
'oneOf
'not
'definitions
'title
'description
'default
'examples
'format
'$schema
'$ref))
(for ([keyword keywords])
(check-not-false (member keyword json-schema-keywords)))
(for ([keyword json-schema-keywords])
(check-not-false (member keyword keywords))))
(define (json-schema? thing)
(cond ((not (jsexpr? thing))
#f)
((boolean? thing)
#t)
((hash? thing)
(let* ([properties (object-properties thing)]
[checkable (intersection properties
json-schema-keywords)])
(andmap (lambda (keyword)
((hash-ref json-schema-validators keyword)
(hash-ref thing keyword)))
checkable)))
(else
#f)))
(provide json-schema?)
(module+ test
(test-case "Silly JSON schema type checks"
(check-false (json-schema? 4)))
(test-case "Boolean JSON schema checks"
(check-true (json-schema? #t))
(check-true (json-schema? #f)))
(test-case "JSON schema checks")
(check-false (json-schema? (hasheq "type" "object")))
(check-false (json-schema? (hasheq 'type #f)))
(let ([js (hasheq 'type "object")])
(check-true (hash? js))
(check-true (has-property? js 'type))
(check-true (string? (property-value js 'type)))
(check-true (json-schema? js)))
(check-false (json-schema? (hasheq 'type "foo")))
;; funky edge case: empty array as type
(check-true (json-schema? (hasheq 'type
(list))))
(check-false (json-schema? (hasheq 'type
(list "foo"))))
;; single correct value
(check-true (json-schema? (hasheq 'type
(list "object"))))
;; typo
(check-false (json-schema? (hasheq 'type
(list "objet"))))
;; two legimate values
(check-true (json-schema? (hasheq 'type
(list "object" "integer"))))
;; extreme case: all the types
(check-true (json-schema? (hasheq 'type
json-schema-types)))
;; duplicate values (though each, separately, is valid)
(check-false (json-schema? (hasheq 'type
(list "object" "number" "object"))))
;; bogus value along with legit value
(check-false (json-schema? (hasheq 'type
(list "number" "real"))))
(define schema-1/jsexper
(hasheq
'type "object"
'items (hasheq
'foo (hasheq
'type "integer"))
'additionalItems #t))
(test-case
"Simple check"
(check-true (jsexpr? schema-1/jsexper))
(check-true (json-schema? (hasheq)))))
(module+ test
(check-true (json-schema? (hasheq 'type "string"))))
;; Command line application for testing whether a file is
;; a JSON Schema at all
(module+ main
(define schema-path
(command-line
#:program "schema"
#:args (schema-path)
schema-path))
(unless (file-exists? schema-path)
(complain-and-die (format "Schema file \"~a\" does not exist." schema-path)))
(define valid?
(call-with-input-file schema-path
(lambda (in)
(with-handlers ([exn:fail:read? (lambda (_) #f)])
(json-schema? (bytes->jsexpr (port->bytes in)))))))
(exit (if valid? 0 1)))