jsonschema 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +94 -0
- data/Rakefile +94 -0
- data/lib/jsonschema.rb +432 -0
- data/test/jsonschema_test.rb +802 -0
- metadata +63 -0
@@ -0,0 +1,802 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/jsonschema'
|
3
|
+
|
4
|
+
class JSONSchemaTest < Test::Unit::TestCase
|
5
|
+
def test_maximum
|
6
|
+
schema = {
|
7
|
+
"type" => "object",
|
8
|
+
"properties" => {
|
9
|
+
"prop01" => {
|
10
|
+
"type" => "number",
|
11
|
+
"maximum" => 10
|
12
|
+
},
|
13
|
+
"prop02" => {
|
14
|
+
"type" => "integer",
|
15
|
+
"maximum" => 20
|
16
|
+
}
|
17
|
+
}
|
18
|
+
}
|
19
|
+
data1 = {
|
20
|
+
"prop01"=> 5,
|
21
|
+
"prop02"=> 10
|
22
|
+
}
|
23
|
+
data2 = {
|
24
|
+
"prop01"=> 10,
|
25
|
+
"prop02"=> 20
|
26
|
+
}
|
27
|
+
data3 = {
|
28
|
+
"prop01"=> 11,
|
29
|
+
"prop02"=> 19
|
30
|
+
}
|
31
|
+
data4 = {
|
32
|
+
"prop01"=> 9,
|
33
|
+
"prop02"=> 21
|
34
|
+
}
|
35
|
+
assert_nothing_raised{
|
36
|
+
JSON::Schema.validate(data1, schema)
|
37
|
+
}
|
38
|
+
assert_nothing_raised{
|
39
|
+
JSON::Schema.validate(data2, schema)
|
40
|
+
}
|
41
|
+
assert_raise(JSON::Schema::ValueError){
|
42
|
+
JSON::Schema.validate(data3, schema)
|
43
|
+
}
|
44
|
+
assert_raise(JSON::Schema::ValueError){
|
45
|
+
JSON::Schema.validate(data4, schema)
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_minimum
|
50
|
+
schema = {
|
51
|
+
"type" => "object",
|
52
|
+
"properties" => {
|
53
|
+
"prop01" => {
|
54
|
+
"type" => "number",
|
55
|
+
"minimum" => 10
|
56
|
+
},
|
57
|
+
"prop02" => {
|
58
|
+
"type" => "integer",
|
59
|
+
"minimum" => 20
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
data1 = {
|
64
|
+
"prop01"=> 21,
|
65
|
+
"prop02"=> 21
|
66
|
+
}
|
67
|
+
data2 = {
|
68
|
+
"prop01"=> 10,
|
69
|
+
"prop02"=> 20
|
70
|
+
}
|
71
|
+
data3 = {
|
72
|
+
"prop01"=> 9,
|
73
|
+
"prop02"=> 21
|
74
|
+
}
|
75
|
+
data4 = {
|
76
|
+
"prop01"=> 10,
|
77
|
+
"prop02"=> 19
|
78
|
+
}
|
79
|
+
assert_nothing_raised{
|
80
|
+
JSON::Schema.validate(data1, schema)
|
81
|
+
}
|
82
|
+
assert_nothing_raised{
|
83
|
+
JSON::Schema.validate(data2, schema)
|
84
|
+
}
|
85
|
+
assert_raise(JSON::Schema::ValueError){
|
86
|
+
JSON::Schema.validate(data3, schema)
|
87
|
+
}
|
88
|
+
assert_raise(JSON::Schema::ValueError){
|
89
|
+
JSON::Schema.validate(data4, schema)
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_minItems
|
94
|
+
schema1 = {
|
95
|
+
"type" => "array",
|
96
|
+
"minItems" => 4
|
97
|
+
}
|
98
|
+
schema2 = {
|
99
|
+
"minItems" => 4
|
100
|
+
}
|
101
|
+
data1 = [1, 2, "3", 4.0]
|
102
|
+
data2 = [1, 2, "3", 4.0, 5.00]
|
103
|
+
data3 = "test"
|
104
|
+
data4 = [1, 2, "3"]
|
105
|
+
assert_nothing_raised{
|
106
|
+
JSON::Schema.validate(data1, schema1)
|
107
|
+
}
|
108
|
+
assert_nothing_raised{
|
109
|
+
JSON::Schema.validate(data2, schema1)
|
110
|
+
}
|
111
|
+
assert_nothing_raised{
|
112
|
+
JSON::Schema.validate(data3, schema2)
|
113
|
+
}
|
114
|
+
assert_nothing_raised{
|
115
|
+
JSON::Schema.validate(data2, schema2)
|
116
|
+
}
|
117
|
+
assert_raise(JSON::Schema::ValueError){
|
118
|
+
JSON::Schema.validate(data4, schema1)
|
119
|
+
}
|
120
|
+
assert_raise(JSON::Schema::ValueError){
|
121
|
+
JSON::Schema.validate(data4, schema2)
|
122
|
+
}
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_maxItems
|
126
|
+
schema1 = {
|
127
|
+
"type" => "array",
|
128
|
+
"maxItems" => 4
|
129
|
+
}
|
130
|
+
schema2 = {
|
131
|
+
"maxItems" => 4
|
132
|
+
}
|
133
|
+
data1 = [1, 2, "3", 4.0]
|
134
|
+
data2 = [1, 2, "3"]
|
135
|
+
data3 = "test"
|
136
|
+
data4 = [1, 2, "3", 4.0, 5.00]
|
137
|
+
assert_nothing_raised{
|
138
|
+
JSON::Schema.validate(data1, schema1)
|
139
|
+
}
|
140
|
+
assert_nothing_raised{
|
141
|
+
JSON::Schema.validate(data2, schema1)
|
142
|
+
}
|
143
|
+
assert_nothing_raised{
|
144
|
+
JSON::Schema.validate(data3, schema2)
|
145
|
+
}
|
146
|
+
assert_nothing_raised{
|
147
|
+
JSON::Schema.validate(data2, schema2)
|
148
|
+
}
|
149
|
+
assert_raise(JSON::Schema::ValueError){
|
150
|
+
JSON::Schema.validate(data4, schema1)
|
151
|
+
}
|
152
|
+
assert_raise(JSON::Schema::ValueError){
|
153
|
+
JSON::Schema.validate(data4, schema2)
|
154
|
+
}
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_minLength
|
158
|
+
schema = {
|
159
|
+
"minLength" => 4
|
160
|
+
}
|
161
|
+
data1 = "test"
|
162
|
+
data2 = "string"
|
163
|
+
data3 = 123
|
164
|
+
data4 = [1, 2, "3"]
|
165
|
+
data5 = "car"
|
166
|
+
assert_nothing_raised{
|
167
|
+
JSON::Schema.validate(data1, schema)
|
168
|
+
}
|
169
|
+
assert_nothing_raised{
|
170
|
+
JSON::Schema.validate(data2, schema)
|
171
|
+
}
|
172
|
+
assert_nothing_raised{
|
173
|
+
JSON::Schema.validate(data3, schema)
|
174
|
+
}
|
175
|
+
assert_nothing_raised{
|
176
|
+
JSON::Schema.validate(data4, schema)
|
177
|
+
}
|
178
|
+
assert_raise(JSON::Schema::ValueError){
|
179
|
+
JSON::Schema.validate(data5, schema)
|
180
|
+
}
|
181
|
+
end
|
182
|
+
def test_maxLength
|
183
|
+
schema = {
|
184
|
+
"maxLength" => 4
|
185
|
+
}
|
186
|
+
data1 = "test"
|
187
|
+
data2 = "car"
|
188
|
+
data3 = 12345
|
189
|
+
data4 = [1, 2, "3", 4, 5]
|
190
|
+
data5 = "string"
|
191
|
+
assert_nothing_raised{
|
192
|
+
JSON::Schema.validate(data1, schema)
|
193
|
+
}
|
194
|
+
assert_nothing_raised{
|
195
|
+
JSON::Schema.validate(data2, schema)
|
196
|
+
}
|
197
|
+
assert_nothing_raised{
|
198
|
+
JSON::Schema.validate(data3, schema)
|
199
|
+
}
|
200
|
+
assert_nothing_raised{
|
201
|
+
JSON::Schema.validate(data4, schema)
|
202
|
+
}
|
203
|
+
assert_raise(JSON::Schema::ValueError){
|
204
|
+
JSON::Schema.validate(data5, schema)
|
205
|
+
}
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_maxDecimal
|
209
|
+
schema = {
|
210
|
+
"type" => "number",
|
211
|
+
"maxDecimal" => 3
|
212
|
+
}
|
213
|
+
data1 = 10.20
|
214
|
+
data2 = 10.204
|
215
|
+
data3 = 10
|
216
|
+
data4 = 10.04092
|
217
|
+
assert_nothing_raised{
|
218
|
+
JSON::Schema.validate(data1, schema)
|
219
|
+
}
|
220
|
+
assert_nothing_raised{
|
221
|
+
JSON::Schema.validate(data2, schema)
|
222
|
+
}
|
223
|
+
assert_nothing_raised{
|
224
|
+
JSON::Schema.validate(data3, schema)
|
225
|
+
}
|
226
|
+
assert_raise(JSON::Schema::ValueError){
|
227
|
+
JSON::Schema.validate(data4, schema)
|
228
|
+
}
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_properties
|
232
|
+
schema = {
|
233
|
+
"type"=>"object",
|
234
|
+
"properties"=>{
|
235
|
+
"prop01"=>{
|
236
|
+
"type"=>"string",
|
237
|
+
},
|
238
|
+
"prop02"=>{
|
239
|
+
"type"=>"number",
|
240
|
+
"optional"=>true
|
241
|
+
},
|
242
|
+
"prop03"=>{
|
243
|
+
"type"=>"integer",
|
244
|
+
},
|
245
|
+
"prop04"=>{
|
246
|
+
"type"=>"boolean",
|
247
|
+
},
|
248
|
+
"prop05"=>{
|
249
|
+
"type"=>"object",
|
250
|
+
"optional"=>true,
|
251
|
+
"properties"=>{
|
252
|
+
"subprop01"=>{
|
253
|
+
"type"=>"string",
|
254
|
+
},
|
255
|
+
"subprop02"=>{
|
256
|
+
"type"=>"string",
|
257
|
+
"optional"=>false
|
258
|
+
}
|
259
|
+
}
|
260
|
+
}
|
261
|
+
}
|
262
|
+
}
|
263
|
+
data1 = {
|
264
|
+
"prop01"=>"test",
|
265
|
+
"prop02"=>1.20,
|
266
|
+
"prop03"=>1,
|
267
|
+
"prop04"=>true,
|
268
|
+
"prop05"=>{
|
269
|
+
"subprop01"=>"test",
|
270
|
+
"subprop02"=>"test2"
|
271
|
+
}
|
272
|
+
}
|
273
|
+
assert_nothing_raised{
|
274
|
+
JSON::Schema.validate(data1, schema)
|
275
|
+
}
|
276
|
+
data2 = {
|
277
|
+
"prop01"=>"test",
|
278
|
+
"prop02"=>1.20,
|
279
|
+
"prop03"=>1,
|
280
|
+
"prop04"=>true
|
281
|
+
}
|
282
|
+
assert_nothing_raised{
|
283
|
+
JSON::Schema.validate(data2, schema)
|
284
|
+
}
|
285
|
+
data3 = {
|
286
|
+
"prop02"=>1.60,
|
287
|
+
"prop05"=>{
|
288
|
+
"subprop01"=>"test"
|
289
|
+
}
|
290
|
+
}
|
291
|
+
assert_raise(JSON::Schema::ValueError){
|
292
|
+
JSON::Schema.validate(data3, schema)
|
293
|
+
}
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_title
|
297
|
+
schema1 = {
|
298
|
+
"title"=>"My Title for My Schema"
|
299
|
+
}
|
300
|
+
schema2 = {
|
301
|
+
"title"=>1233
|
302
|
+
}
|
303
|
+
data = "whatever"
|
304
|
+
assert_nothing_raised{
|
305
|
+
JSON::Schema.validate(data, schema1)
|
306
|
+
}
|
307
|
+
assert_raise(JSON::Schema::ValueError){
|
308
|
+
JSON::Schema.validate(data, schema2)
|
309
|
+
}
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_requires
|
313
|
+
schema = {
|
314
|
+
"type"=>"object",
|
315
|
+
"properties"=>{
|
316
|
+
"prop01"=>{
|
317
|
+
"type"=>"string",
|
318
|
+
"optional"=>true
|
319
|
+
},
|
320
|
+
"prop02"=>{
|
321
|
+
"type"=>"number",
|
322
|
+
"optional"=>true,
|
323
|
+
"requires"=>"prop01"
|
324
|
+
}
|
325
|
+
}
|
326
|
+
}
|
327
|
+
data1 = {}
|
328
|
+
data2 = {
|
329
|
+
"prop01"=>"test",
|
330
|
+
"prop02"=>2
|
331
|
+
}
|
332
|
+
assert_nothing_raised{
|
333
|
+
JSON::Schema.validate(data1, schema)
|
334
|
+
}
|
335
|
+
assert_nothing_raised{
|
336
|
+
JSON::Schema.validate(data2, schema)
|
337
|
+
}
|
338
|
+
data3 = {
|
339
|
+
"prop02"=>2
|
340
|
+
}
|
341
|
+
assert_raise(JSON::Schema::ValueError){
|
342
|
+
JSON::Schema.validate(data3, schema)
|
343
|
+
}
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_pattern
|
347
|
+
schema = {
|
348
|
+
"pattern"=>"^[A-Za-z0-9][A-Za-z0-9\.]*@([A-Za-z0-9]+\.)+[A-Za-z0-9]+$"
|
349
|
+
}
|
350
|
+
data1 = "my.email01@gmail.com"
|
351
|
+
assert_nothing_raised{
|
352
|
+
JSON::Schema.validate(data1, schema)
|
353
|
+
}
|
354
|
+
data2 = 123
|
355
|
+
assert_nothing_raised{
|
356
|
+
JSON::Schema.validate(data2, schema)
|
357
|
+
}
|
358
|
+
data3 = "whatever"
|
359
|
+
assert_raise(JSON::Schema::ValueError){
|
360
|
+
JSON::Schema.validate(data3, schema)
|
361
|
+
}
|
362
|
+
end
|
363
|
+
|
364
|
+
def test_optional
|
365
|
+
schema = {
|
366
|
+
"type"=>"object",
|
367
|
+
"properties"=>{
|
368
|
+
"prop01"=>{
|
369
|
+
"type"=>"string"
|
370
|
+
},
|
371
|
+
"prop02"=>{
|
372
|
+
"type"=>"number",
|
373
|
+
"optional"=>true
|
374
|
+
},
|
375
|
+
"prop03"=>{
|
376
|
+
"type"=>"integer"
|
377
|
+
},
|
378
|
+
"prop04"=>{
|
379
|
+
"type"=>"boolean",
|
380
|
+
"optional"=>false
|
381
|
+
}
|
382
|
+
}
|
383
|
+
}
|
384
|
+
data1 = {
|
385
|
+
"prop01"=>"test",
|
386
|
+
"prop03"=>1,
|
387
|
+
"prop04"=>false
|
388
|
+
}
|
389
|
+
assert_nothing_raised{
|
390
|
+
JSON::Schema.validate(data1, schema)
|
391
|
+
}
|
392
|
+
data2 = {
|
393
|
+
"prop02"=>"blah"
|
394
|
+
}
|
395
|
+
assert_raise(JSON::Schema::ValueError){
|
396
|
+
JSON::Schema.validate(data2, schema)
|
397
|
+
}
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_default
|
401
|
+
schema1 = {
|
402
|
+
"properties"=>{
|
403
|
+
"test"=>{
|
404
|
+
"optional"=>true,
|
405
|
+
"default"=>10
|
406
|
+
},
|
407
|
+
}
|
408
|
+
}
|
409
|
+
schema2 = {
|
410
|
+
"properties"=>{
|
411
|
+
"test"=>{
|
412
|
+
"optional"=>true,
|
413
|
+
"default"=>10,
|
414
|
+
"readonly"=>true
|
415
|
+
}
|
416
|
+
}
|
417
|
+
}
|
418
|
+
data1 = {}
|
419
|
+
assert_nothing_raised{
|
420
|
+
JSON::Schema.validate(data1, schema1)
|
421
|
+
}
|
422
|
+
assert_equal(10, data1["test"])
|
423
|
+
|
424
|
+
data2 = {}
|
425
|
+
assert_nothing_raised{
|
426
|
+
JSON::Schema.validate(data2, schema2)
|
427
|
+
}
|
428
|
+
assert_not_equal(10, data2["test"])
|
429
|
+
|
430
|
+
data3 = {}
|
431
|
+
assert_nothing_raised{
|
432
|
+
JSON::Schema.validate(data3, schema1, true)
|
433
|
+
}
|
434
|
+
assert_equal(10, data3["test"])
|
435
|
+
|
436
|
+
data4 = {}
|
437
|
+
assert_nothing_raised{
|
438
|
+
JSON::Schema.validate(data4, schema1, false)
|
439
|
+
}
|
440
|
+
assert_not_equal(10, data4["test"])
|
441
|
+
end
|
442
|
+
|
443
|
+
def test_description
|
444
|
+
schema1 = {
|
445
|
+
"description"=>"My Description for My Schema"
|
446
|
+
}
|
447
|
+
schema2 = {
|
448
|
+
"description"=>1233
|
449
|
+
}
|
450
|
+
data = "whatever"
|
451
|
+
assert_nothing_raised{
|
452
|
+
JSON::Schema.validate(data, schema1)
|
453
|
+
}
|
454
|
+
assert_raise(JSON::Schema::ValueError){
|
455
|
+
JSON::Schema.validate(data, schema2)
|
456
|
+
}
|
457
|
+
end
|
458
|
+
|
459
|
+
def test_type
|
460
|
+
# schema
|
461
|
+
schema1 = {
|
462
|
+
"type"=>[
|
463
|
+
{
|
464
|
+
"type"=>"array",
|
465
|
+
"minItems"=>10
|
466
|
+
},
|
467
|
+
{
|
468
|
+
"type"=>"string",
|
469
|
+
"pattern"=>"^0+$"
|
470
|
+
}
|
471
|
+
]
|
472
|
+
}
|
473
|
+
data1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
474
|
+
data2 = "0"
|
475
|
+
data3 = 1203
|
476
|
+
assert_nothing_raised{
|
477
|
+
JSON::Schema.validate(data1, schema1)
|
478
|
+
}
|
479
|
+
assert_nothing_raised{
|
480
|
+
JSON::Schema.validate(data2, schema1)
|
481
|
+
}
|
482
|
+
assert_raise(JSON::Schema::ValueError){
|
483
|
+
JSON::Schema.validate(data3, schema1)
|
484
|
+
}
|
485
|
+
|
486
|
+
# integer phase
|
487
|
+
[1, 89, 48, 32, 49, 42].each do |item|
|
488
|
+
assert_nothing_raised{
|
489
|
+
JSON::Schema.validate(item, {"type"=>"integer"})
|
490
|
+
}
|
491
|
+
end
|
492
|
+
[1.2, "bad", {"test" => "blah"}, [32, 49], nil, true].each do |item|
|
493
|
+
assert_raise(JSON::Schema::ValueError){
|
494
|
+
JSON::Schema.validate(item, {"type"=>"integer"})
|
495
|
+
}
|
496
|
+
end
|
497
|
+
|
498
|
+
# string phase
|
499
|
+
["surrender?", "nuts!", "ok", "@hsuha", "\'ok?\'", "blah"].each do |item|
|
500
|
+
assert_nothing_raised{
|
501
|
+
JSON::Schema.validate(item, {"type"=>"string"})
|
502
|
+
}
|
503
|
+
end
|
504
|
+
[1.2, 1, {"test" => "blah"}, [32, 49], nil, true].each do |item|
|
505
|
+
assert_raise(JSON::Schema::ValueError){
|
506
|
+
JSON::Schema.validate(item, {"type"=>"string"})
|
507
|
+
}
|
508
|
+
end
|
509
|
+
|
510
|
+
# number phase
|
511
|
+
[1.2, 89.42, 48.5224242, 32, 49, 42.24324].each do |item|
|
512
|
+
assert_nothing_raised{
|
513
|
+
JSON::Schema.validate(item, {"type"=>"number"})
|
514
|
+
}
|
515
|
+
end
|
516
|
+
["bad", {"test"=>"blah"}, [32.42, 494242], nil, true].each do |item|
|
517
|
+
assert_raise(JSON::Schema::ValueError){
|
518
|
+
JSON::Schema.validate(item, {"type"=>"number"})
|
519
|
+
}
|
520
|
+
end
|
521
|
+
|
522
|
+
# boolean phase
|
523
|
+
[true, false].each do |item|
|
524
|
+
assert_nothing_raised{
|
525
|
+
JSON::Schema.validate(item, {"type"=>"boolean"})
|
526
|
+
}
|
527
|
+
end
|
528
|
+
[1.2, "False", {"test" => "blah"}, [32, 49], nil, 1, 0].each do |item|
|
529
|
+
assert_raise(JSON::Schema::ValueError){
|
530
|
+
JSON::Schema.validate(item, {"type"=>"boolean"})
|
531
|
+
}
|
532
|
+
end
|
533
|
+
|
534
|
+
# object phase
|
535
|
+
[{"blah"=>"test"}, {"this"=>{"blah"=>"test"}}, {1=>2, 10=>20}].each do |item|
|
536
|
+
assert_nothing_raised{
|
537
|
+
JSON::Schema.validate(item, {"type"=>"object"})
|
538
|
+
}
|
539
|
+
end
|
540
|
+
[1.2, "bad", 123, [32, 49], nil, true].each do |item|
|
541
|
+
assert_raise(JSON::Schema::ValueError){
|
542
|
+
JSON::Schema.validate(item, {"type"=>"object"})
|
543
|
+
}
|
544
|
+
end
|
545
|
+
|
546
|
+
# array phase
|
547
|
+
[[1, 89], [48, {"test"=>"blah"}, "49", 42]].each do |item|
|
548
|
+
assert_nothing_raised{
|
549
|
+
JSON::Schema.validate(item, {"type"=>"array"})
|
550
|
+
}
|
551
|
+
end
|
552
|
+
[1.2, "bad", {"test"=>"blah"}, 1234, nil, true].each do |item|
|
553
|
+
assert_raise(JSON::Schema::ValueError){
|
554
|
+
JSON::Schema.validate(item, {"type"=>"array"})
|
555
|
+
}
|
556
|
+
end
|
557
|
+
|
558
|
+
# null phase
|
559
|
+
assert_nothing_raised{
|
560
|
+
JSON::Schema.validate(nil, {"type"=>"null"})
|
561
|
+
}
|
562
|
+
[1.2, "bad", {"test"=>"blah"}, [32, 49], 1284, true].each do |item|
|
563
|
+
assert_raise(JSON::Schema::ValueError){
|
564
|
+
JSON::Schema.validate(item, {"type"=>"null"})
|
565
|
+
}
|
566
|
+
end
|
567
|
+
|
568
|
+
# any phase
|
569
|
+
[1.2, "bad", {"test"=>"blah"}, [32, 49], nil, 1284, true].each do |item|
|
570
|
+
assert_nothing_raised{
|
571
|
+
JSON::Schema.validate(item, {"type"=>"any"})
|
572
|
+
}
|
573
|
+
end
|
574
|
+
|
575
|
+
end
|
576
|
+
|
577
|
+
def test_items
|
578
|
+
schema1 = {
|
579
|
+
"type"=>"array",
|
580
|
+
"items"=>{
|
581
|
+
"type"=>"string"
|
582
|
+
}
|
583
|
+
}
|
584
|
+
schema2 = {
|
585
|
+
"type"=>"array",
|
586
|
+
"items"=>[
|
587
|
+
{"type"=>"integer"},
|
588
|
+
{"type"=>"string"},
|
589
|
+
{"type"=>"boolean"}
|
590
|
+
]
|
591
|
+
}
|
592
|
+
data1 = ["string", "another string", "mystring"]
|
593
|
+
data2 = ["JSON Schema is cool", "yet another string"]
|
594
|
+
assert_nothing_raised{
|
595
|
+
JSON::Schema.validate(data1, schema1)
|
596
|
+
}
|
597
|
+
assert_nothing_raised{
|
598
|
+
JSON::Schema.validate(data2, schema1)
|
599
|
+
}
|
600
|
+
data3 = ["string", "another string", 1]
|
601
|
+
assert_raise(JSON::Schema::ValueError){
|
602
|
+
JSON::Schema.validate(data3, schema1)
|
603
|
+
}
|
604
|
+
data4 = [1, "More strings?", true]
|
605
|
+
data5 = [12482, "Yes, more strings", false]
|
606
|
+
assert_nothing_raised{
|
607
|
+
JSON::Schema.validate(data4, schema2)
|
608
|
+
}
|
609
|
+
assert_nothing_raised{
|
610
|
+
JSON::Schema.validate(data5, schema2)
|
611
|
+
}
|
612
|
+
data6 = [1294, "Ok. I give up"]
|
613
|
+
assert_raise(JSON::Schema::ValueError){
|
614
|
+
JSON::Schema.validate(data6, schema2)
|
615
|
+
}
|
616
|
+
data7 = [1294, "Ok. I give up", "Not a boolean"]
|
617
|
+
assert_raise(JSON::Schema::ValueError){
|
618
|
+
JSON::Schema.validate(data7, schema2)
|
619
|
+
}
|
620
|
+
end
|
621
|
+
|
622
|
+
def test_enum
|
623
|
+
schema = {
|
624
|
+
"enum"=>["test", true, 123, ["???"]]
|
625
|
+
}
|
626
|
+
["test", true, 123, ["???"]].each do |item|
|
627
|
+
assert_nothing_raised{
|
628
|
+
JSON::Schema.validate(item, schema)
|
629
|
+
}
|
630
|
+
end
|
631
|
+
assert_raise(JSON::Schema::ValueError){
|
632
|
+
JSON::Schema.validate("unknown", schema)
|
633
|
+
}
|
634
|
+
end
|
635
|
+
|
636
|
+
|
637
|
+
def test_additionalProperties
|
638
|
+
schema1 = {
|
639
|
+
"additionalProperties"=>{
|
640
|
+
"type" => "integer"
|
641
|
+
}
|
642
|
+
}
|
643
|
+
[1, 89, 48, 32, 49, 42].each do |item|
|
644
|
+
assert_nothing_raised{
|
645
|
+
JSON::Schema.validate({"prop" => item}, schema1)
|
646
|
+
}
|
647
|
+
end
|
648
|
+
[1.2, "bad", {"test"=>"blah"}, [32, 49], nil, true].each do |item|
|
649
|
+
assert_raise(JSON::Schema::ValueError){
|
650
|
+
JSON::Schema.validate({"prop" => item}, schema1)
|
651
|
+
}
|
652
|
+
end
|
653
|
+
schema2 = {
|
654
|
+
"properties" => {
|
655
|
+
"prop1" => {"type" => "integer"},
|
656
|
+
"prop2" => {"type" => "string" }
|
657
|
+
},
|
658
|
+
"additionalProperties" => {
|
659
|
+
"type" => ["string", "number"]
|
660
|
+
}
|
661
|
+
}
|
662
|
+
[1, "test", 48, "ok", 4.9, 42].each do |item|
|
663
|
+
assert_nothing_raised{
|
664
|
+
JSON::Schema.validate({
|
665
|
+
"prop1" => 123,
|
666
|
+
"prop2" => "this is prop2",
|
667
|
+
"prop3" => item
|
668
|
+
}, schema2)
|
669
|
+
}
|
670
|
+
end
|
671
|
+
[{"test"=>"blah"}, [32, 49], nil, true].each do |item|
|
672
|
+
assert_raise(JSON::Schema::ValueError){
|
673
|
+
JSON::Schema.validate({
|
674
|
+
"prop1" => 123,
|
675
|
+
"prop2" => "this is prop2",
|
676
|
+
"prop3" => item
|
677
|
+
}, schema2)
|
678
|
+
}
|
679
|
+
end
|
680
|
+
schema3 = {
|
681
|
+
"additionalProperties" => true
|
682
|
+
}
|
683
|
+
[1.2, 1, {"test"=>"blah"}, [32, 49], nil, true, "blah"].each do |item|
|
684
|
+
assert_nothing_raised{
|
685
|
+
JSON::Schema.validate({"prop" => item}, schema3)
|
686
|
+
}
|
687
|
+
end
|
688
|
+
schema4 = {
|
689
|
+
"additionalProperties" => false
|
690
|
+
}
|
691
|
+
["bad", {"test"=>"blah"}, [32.42, 494242], nil, true, 1.34].each do |item|
|
692
|
+
assert_raise(JSON::Schema::ValueError){
|
693
|
+
JSON::Schema.validate({"prop" => item}, schema4)
|
694
|
+
}
|
695
|
+
end
|
696
|
+
end
|
697
|
+
|
698
|
+
def test_disallow
|
699
|
+
# multi phase
|
700
|
+
schema = {"disallow"=>["null","integer","string"]}
|
701
|
+
[nil, 183, "mystring"].each do |item|
|
702
|
+
assert_raise(JSON::Schema::ValueError){
|
703
|
+
JSON::Schema.validate(item, schema)
|
704
|
+
}
|
705
|
+
end
|
706
|
+
[1.2, {"test"=>"blah"}, [32, 49], true].each do |item|
|
707
|
+
assert_nothing_raised{
|
708
|
+
JSON::Schema.validate(item, schema)
|
709
|
+
}
|
710
|
+
end
|
711
|
+
|
712
|
+
# any phase
|
713
|
+
[1.2, "bad", {"test"=>"blah"}, [32, 49], nil, 1284, true].each do |item|
|
714
|
+
assert_raise(JSON::Schema::ValueError){
|
715
|
+
JSON::Schema.validate(item, {"disallow"=>"any"})
|
716
|
+
}
|
717
|
+
end
|
718
|
+
|
719
|
+
# null phase
|
720
|
+
assert_raise(JSON::Schema::ValueError){
|
721
|
+
JSON::Schema.validate(nil, {"disallow"=>"null"})
|
722
|
+
}
|
723
|
+
[1.2, "bad", {"test"=>"blah"}, [32, 49], 1284, true].each do |item|
|
724
|
+
assert_nothing_raised{
|
725
|
+
JSON::Schema.validate(item, {"disallow"=>"null"})
|
726
|
+
}
|
727
|
+
end
|
728
|
+
|
729
|
+
# array phase
|
730
|
+
[[1, 89], [48, {"test"=>"blah"}, "49", 42]].each do |item|
|
731
|
+
assert_raise(JSON::Schema::ValueError){
|
732
|
+
JSON::Schema.validate(item, {"disallow"=>"array"})
|
733
|
+
}
|
734
|
+
end
|
735
|
+
[1.2, "bad", {"test"=>"blah"}, 1234, nil, true].each do |item|
|
736
|
+
assert_nothing_raised{
|
737
|
+
JSON::Schema.validate(item, {"disallow"=>"array"})
|
738
|
+
}
|
739
|
+
end
|
740
|
+
|
741
|
+
# object phase
|
742
|
+
[{"blah"=>"test"}, {"this"=>{"blah"=>"test"}}, {1=>2, 10=>20}].each do |item|
|
743
|
+
assert_raise(JSON::Schema::ValueError){
|
744
|
+
JSON::Schema.validate(item, {"disallow"=>"object"})
|
745
|
+
}
|
746
|
+
end
|
747
|
+
[1.2, "bad", 123, [32, 49], nil, true].each do |item|
|
748
|
+
assert_nothing_raised{
|
749
|
+
JSON::Schema.validate(item, {"disallow"=>"object"})
|
750
|
+
}
|
751
|
+
end
|
752
|
+
|
753
|
+
# boolean phase
|
754
|
+
[true, false].each do |item|
|
755
|
+
assert_raise(JSON::Schema::ValueError){
|
756
|
+
JSON::Schema.validate(item, {"disallow"=>"boolean"})
|
757
|
+
}
|
758
|
+
end
|
759
|
+
[1.2, "False", {"test" => "blah"}, [32, 49], nil, 1, 0].each do |item|
|
760
|
+
assert_nothing_raised{
|
761
|
+
JSON::Schema.validate(item, {"disallow"=>"boolean"})
|
762
|
+
}
|
763
|
+
end
|
764
|
+
|
765
|
+
# number phase
|
766
|
+
[1.2, 89.42, 48.5224242, 32, 49, 42.24324].each do |item|
|
767
|
+
assert_raise(JSON::Schema::ValueError){
|
768
|
+
JSON::Schema.validate(item, {"disallow"=>"number"})
|
769
|
+
}
|
770
|
+
end
|
771
|
+
["bad", {"test"=>"blah"}, [32.42, 494242], nil, true].each do |item|
|
772
|
+
assert_nothing_raised{
|
773
|
+
JSON::Schema.validate(item, {"disallow"=>"number"})
|
774
|
+
}
|
775
|
+
end
|
776
|
+
|
777
|
+
# integer phase
|
778
|
+
[1, 89, 48, 32, 49, 42].each do |item|
|
779
|
+
assert_raise(JSON::Schema::ValueError){
|
780
|
+
JSON::Schema.validate(item, {"disallow"=>"integer"})
|
781
|
+
}
|
782
|
+
end
|
783
|
+
[1.2, "bad", {"test" => "blah"}, [32, 49], nil, true].each do |item|
|
784
|
+
assert_nothing_raised{
|
785
|
+
JSON::Schema.validate(item, {"disallow"=>"integer"})
|
786
|
+
}
|
787
|
+
end
|
788
|
+
|
789
|
+
# string phase
|
790
|
+
["surrender?", "nuts!", "ok", "@hsuha", "\'ok?\'", "blah"].each do |item|
|
791
|
+
assert_raise(JSON::Schema::ValueError){
|
792
|
+
JSON::Schema.validate(item, {"disallow"=>"string"})
|
793
|
+
}
|
794
|
+
end
|
795
|
+
[1.2, 1, {"test" => "blah"}, [32, 49], nil, true].each do |item|
|
796
|
+
assert_nothing_raised{
|
797
|
+
JSON::Schema.validate(item, {"disallow"=>"string"})
|
798
|
+
}
|
799
|
+
end
|
800
|
+
end
|
801
|
+
end
|
802
|
+
|