json-schema 0.2.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,6 +57,7 @@ module JSON
57
57
 
58
58
  def validate(current_schema, data, fragments)
59
59
  current_schema.schema.each do |attr_name,attribute|
60
+
60
61
  if @attributes.has_key?(attr_name.to_s)
61
62
  @attributes[attr_name.to_s].validate(current_schema, data, fragments, self)
62
63
  end
@@ -72,7 +73,8 @@ module JSON
72
73
  @@schemas = {}
73
74
  @@cache_schemas = false
74
75
  @@default_opts = {
75
- :list => false
76
+ :list => false,
77
+ :version => nil
76
78
  }
77
79
  @@validators = {}
78
80
  @@default_validator = nil
@@ -81,6 +83,23 @@ module JSON
81
83
 
82
84
  def initialize(schema_data, data, opts={})
83
85
  @options = @@default_opts.clone.merge(opts)
86
+
87
+ # I'm not a fan of this, but it's quick and dirty to get it working for now
88
+ if @options[:version]
89
+ @options[:version] = case @options[:version].to_s
90
+ when "draft3"
91
+ "draft-03"
92
+ when "draft2"
93
+ "draft-02"
94
+ when "draft1"
95
+ "draft-01"
96
+ else
97
+ raise JSON::Schema::SchemaError.new("The requested JSON schema version is not supported")
98
+ end
99
+ u = URI.parse("http://json-schema.org/#{@options[:version]}/schema#")
100
+ validator = JSON::Validator.validators["#{u.scheme}://#{u.host}#{u.path}"]
101
+ @options[:version] = validator
102
+ end
84
103
  @base_schema = initialize_schema(schema_data)
85
104
  @data = initialize_data(data)
86
105
  build_schemas(@base_schema)
@@ -122,13 +141,13 @@ module JSON
122
141
 
123
142
  if Validator.schemas[uri.to_s].nil?
124
143
  begin
125
- schema = JSON::Schema.new(JSON::Validator.parse(open(uri.to_s).read), uri)
144
+ schema = JSON::Schema.new(JSON::Validator.parse(open(uri.to_s).read), uri, @options[:version])
126
145
  Validator.add_schema(schema)
127
146
  build_schemas(schema)
128
147
  rescue JSON::ParserError
129
148
  # Don't rescue this error, we want JSON formatting issues to bubble up
130
149
  raise $!
131
- rescue
150
+ rescue Exception
132
151
  # Failures will occur when this URI cannot be referenced yet. Don't worry about it,
133
152
  # the proper error will fall out if the ref isn't ever defined
134
153
  end
@@ -184,7 +203,7 @@ module JSON
184
203
  load_ref_schema(parent_schema, obj['$ref'])
185
204
  else
186
205
  schema_uri = parent_schema.uri.clone
187
- schema = JSON::Schema.new(obj,schema_uri)
206
+ schema = JSON::Schema.new(obj,schema_uri,@options[:version])
188
207
  if obj['id']
189
208
  Validator.add_schema(schema)
190
209
  end
@@ -288,12 +307,12 @@ module JSON
288
307
  if schema.is_a?(String)
289
308
  begin
290
309
  # Build a fake URI for this
291
- schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema)}")
310
+ schema_uri = URI.parse(UUID.create_v5(schema,UUID::Nil).to_s)
292
311
  schema = JSON::Validator.parse(schema)
293
312
  if @options[:list]
294
313
  schema = {"type" => "array", "items" => schema}
295
314
  end
296
- schema = JSON::Schema.new(schema,schema_uri)
315
+ schema = JSON::Schema.new(schema,schema_uri,@options[:version])
297
316
  Validator.add_schema(schema)
298
317
  rescue
299
318
  # Build a uri for it
@@ -311,7 +330,7 @@ module JSON
311
330
  if @options[:list]
312
331
  schema = {"type" => "array", "items" => schema}
313
332
  end
314
- schema = JSON::Schema.new(schema,schema_uri)
333
+ schema = JSON::Schema.new(schema,schema_uri,@options[:version])
315
334
  Validator.add_schema(schema)
316
335
  else
317
336
  schema = Validator.schemas[schema_uri.to_s]
@@ -321,8 +340,8 @@ module JSON
321
340
  if @options[:list]
322
341
  schema = {"type" => "array", "items" => schema}
323
342
  end
324
- schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema.inspect)}")
325
- schema = JSON::Schema.new(schema,schema_uri)
343
+ schema_uri = URI.parse(UUID.create_v5(schema.inspect,UUID::Nil).to_s)
344
+ schema = JSON::Schema.new(schema,schema_uri,@options[:version])
326
345
  Validator.add_schema(schema)
327
346
  else
328
347
  raise "Invalid schema - must be either a string or a hash"
@@ -0,0 +1,32 @@
1
+ module JSON
2
+ class Schema
3
+
4
+ class Draft1 < Validator
5
+ def initialize
6
+ super
7
+ @attributes = {
8
+ "type" => JSON::Schema::TypeAttribute,
9
+ "disallow" => JSON::Schema::DisallowAttribute,
10
+ "format" => JSON::Schema::FormatAttribute,
11
+ "maximum" => JSON::Schema::MaximumInclusiveAttribute,
12
+ "minimum" => JSON::Schema::MinimumInclusiveAttribute,
13
+ "minItems" => JSON::Schema::MinItemsAttribute,
14
+ "maxItems" => JSON::Schema::MaxItemsAttribute,
15
+ "minLength" => JSON::Schema::MinLengthAttribute,
16
+ "maxLength" => JSON::Schema::MaxLengthAttribute,
17
+ "maxDecimal" => JSON::Schema::MaxDecimalAttribute,
18
+ "enum" => JSON::Schema::EnumAttribute,
19
+ "properties" => JSON::Schema::PropertiesOptionalAttribute,
20
+ "pattern" => JSON::Schema::PatternAttribute,
21
+ "additionalProperties" => JSON::Schema::AdditionalPropertiesAttribute,
22
+ "items" => JSON::Schema::ItemsAttribute,
23
+ "extends" => JSON::Schema::ExtendsAttribute
24
+ }
25
+ @uri = URI.parse("http://json-schema.org/draft-01/schema#")
26
+ end
27
+
28
+ JSON::Validator.register_validator(self.new)
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ module JSON
2
+ class Schema
3
+
4
+ class Draft2 < Validator
5
+ def initialize
6
+ super
7
+ @attributes = {
8
+ "type" => JSON::Schema::TypeAttribute,
9
+ "disallow" => JSON::Schema::DisallowAttribute,
10
+ "format" => JSON::Schema::FormatAttribute,
11
+ "maximum" => JSON::Schema::MaximumInclusiveAttribute,
12
+ "minimum" => JSON::Schema::MinimumInclusiveAttribute,
13
+ "minItems" => JSON::Schema::MinItemsAttribute,
14
+ "maxItems" => JSON::Schema::MaxItemsAttribute,
15
+ "uniqueItems" => JSON::Schema::UniqueItemsAttribute,
16
+ "minLength" => JSON::Schema::MinLengthAttribute,
17
+ "maxLength" => JSON::Schema::MaxLengthAttribute,
18
+ "divisibleBy" => JSON::Schema::DivisibleByAttribute,
19
+ "enum" => JSON::Schema::EnumAttribute,
20
+ "properties" => JSON::Schema::PropertiesOptionalAttribute,
21
+ "pattern" => JSON::Schema::PatternAttribute,
22
+ "additionalProperties" => JSON::Schema::AdditionalPropertiesAttribute,
23
+ "items" => JSON::Schema::ItemsAttribute,
24
+ "extends" => JSON::Schema::ExtendsAttribute
25
+ }
26
+ @uri = URI.parse("http://json-schema.org/draft-02/schema#")
27
+ end
28
+
29
+ JSON::Validator.register_validator(self.new)
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,760 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class JSONSchemaDraft1Test < Test::Unit::TestCase
5
+ def test_types
6
+ # Set up the default datatype
7
+ schema = {
8
+ "properties" => {
9
+ "a" => {}
10
+ }
11
+ }
12
+ data = {
13
+ "a" => nil
14
+ }
15
+
16
+ # Test integers
17
+ schema["properties"]["a"]["type"] = "integer"
18
+ data["a"] = 5
19
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
20
+
21
+ data["a"] = 5.2
22
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
23
+
24
+ data['a'] = 'string'
25
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
26
+
27
+ data['a'] = true
28
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
29
+
30
+
31
+ # Test numbers
32
+ schema["properties"]["a"]["type"] = "number"
33
+ data["a"] = 5
34
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
35
+
36
+ data["a"] = 5.2
37
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
38
+
39
+ data['a'] = 'string'
40
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
41
+
42
+ data['a'] = true
43
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
44
+
45
+
46
+ # Test strings
47
+ schema["properties"]["a"]["type"] = "string"
48
+ data["a"] = 5
49
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
50
+
51
+ data["a"] = 5.2
52
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
53
+
54
+ data['a'] = 'string'
55
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
56
+
57
+ data['a'] = true
58
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
59
+
60
+
61
+ # Test booleans
62
+ schema["properties"]["a"]["type"] = "boolean"
63
+ data["a"] = 5
64
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
65
+
66
+ data["a"] = 5.2
67
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
68
+
69
+ data['a'] = 'string'
70
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
71
+
72
+ data['a'] = true
73
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
74
+
75
+ data['a'] = false
76
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
77
+
78
+
79
+ # Test object
80
+ schema["properties"]["a"]["type"] = "object"
81
+ data["a"] = {}
82
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
83
+
84
+ data["a"] = 5.2
85
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
86
+
87
+ data['a'] = 'string'
88
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
89
+
90
+ data['a'] = true
91
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
92
+
93
+
94
+ # Test array
95
+ schema["properties"]["a"]["type"] = "array"
96
+ data["a"] = []
97
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
98
+
99
+ data["a"] = 5.2
100
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
101
+
102
+ data['a'] = 'string'
103
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
104
+
105
+ data['a'] = true
106
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
107
+
108
+
109
+ # Test null
110
+ schema["properties"]["a"]["type"] = "null"
111
+ data["a"] = nil
112
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
113
+
114
+ data["a"] = 5.2
115
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
116
+
117
+ data['a'] = 'string'
118
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
119
+
120
+ data['a'] = true
121
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
122
+
123
+
124
+ # Test any
125
+ schema["properties"]["a"]["type"] = "any"
126
+ data["a"] = 5
127
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
128
+
129
+ data["a"] = 5.2
130
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
131
+
132
+ data['a'] = 'string'
133
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
134
+
135
+ data['a'] = true
136
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
137
+
138
+
139
+ # Test a union type
140
+ schema["properties"]["a"]["type"] = ["integer","string"]
141
+ data["a"] = 5
142
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
143
+
144
+ data["a"] = 'boo'
145
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
146
+
147
+ data["a"] = false
148
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
149
+
150
+ # Test a union type with schemas
151
+ schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}]
152
+
153
+ data["a"] = "test"
154
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
155
+
156
+ data["a"] = 5
157
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
158
+
159
+ data["a"] = {"b" => 5}
160
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
161
+
162
+ data["a"] = {"b" => "taco"}
163
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
164
+ end
165
+
166
+
167
+
168
+ def test_optional
169
+ # Set up the default datatype
170
+ schema = {
171
+ "properties" => {
172
+ "a" => {"type" => "string"}
173
+ }
174
+ }
175
+ data = {}
176
+
177
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
178
+ data['a'] = "Hello"
179
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
180
+
181
+ schema = {
182
+ "properties" => {
183
+ "a" => {"type" => "integer", "optional" => "true"}
184
+ }
185
+ }
186
+
187
+ data = {}
188
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
189
+
190
+ end
191
+
192
+
193
+
194
+ def test_minimum
195
+ # Set up the default datatype
196
+ schema = {
197
+ "properties" => {
198
+ "a" => {"minimum" => 5}
199
+ }
200
+ }
201
+
202
+ data = {
203
+ "a" => nil
204
+ }
205
+
206
+
207
+ # Test an integer
208
+ data["a"] = 5
209
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
210
+
211
+ data["a"] = 4
212
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
213
+
214
+ # Test a float
215
+ data["a"] = 5.0
216
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
217
+
218
+ data["a"] = 4.9
219
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
220
+
221
+ # Test a non-number
222
+ data["a"] = "a string"
223
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
224
+
225
+ # Test exclusiveMinimum
226
+ schema["properties"]["a"]["minimumCanEqual"] = false
227
+
228
+ data["a"] = 6
229
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
230
+
231
+ data["a"] = 5
232
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
233
+
234
+ # Test with float
235
+ data["a"] = 5.00000001
236
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
237
+
238
+ data["a"] = 5.0
239
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
240
+ end
241
+
242
+
243
+
244
+ def test_maximum
245
+ # Set up the default datatype
246
+ schema = {
247
+ "properties" => {
248
+ "a" => {"maximum" => 5}
249
+ }
250
+ }
251
+
252
+ data = {
253
+ "a" => nil
254
+ }
255
+
256
+
257
+ # Test an integer
258
+ data["a"] = 5
259
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
260
+
261
+ data["a"] = 6
262
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
263
+
264
+ # Test a float
265
+ data["a"] = 5.0
266
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
267
+
268
+ data["a"] = 5.1
269
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
270
+
271
+ # Test a non-number
272
+ data["a"] = "a string"
273
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
274
+
275
+ # Test exclusiveMinimum
276
+ schema["properties"]["a"]["maximumCanEqual"] = false
277
+
278
+ data["a"] = 4
279
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
280
+
281
+ data["a"] = 5
282
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
283
+
284
+ # Test with float
285
+ data["a"] = 4.9999999
286
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
287
+
288
+ data["a"] = 5.0
289
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
290
+ end
291
+
292
+
293
+ def test_min_items
294
+ # Set up the default datatype
295
+ schema = {
296
+ "properties" => {
297
+ "a" => {"minItems" => 1}
298
+ }
299
+ }
300
+
301
+ data = {
302
+ "a" => nil
303
+ }
304
+
305
+ # Test with an array
306
+ data["a"] = ["boo"]
307
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
308
+
309
+ data["a"] = []
310
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
311
+
312
+ # Test with a non-array
313
+ data["a"] = "boo"
314
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
315
+ end
316
+
317
+
318
+
319
+ def test_max_items
320
+ # Set up the default datatype
321
+ schema = {
322
+ "properties" => {
323
+ "a" => {"maxItems" => 1}
324
+ }
325
+ }
326
+
327
+ data = {
328
+ "a" => nil
329
+ }
330
+
331
+ # Test with an array
332
+ data["a"] = ["boo"]
333
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
334
+
335
+ data["a"] = ["boo","taco"]
336
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
337
+
338
+ # Test with a non-array
339
+ data["a"] = "boo"
340
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
341
+ end
342
+
343
+
344
+ def test_pattern
345
+ # Set up the default datatype
346
+ schema = {
347
+ "properties" => {
348
+ "a" => {"pattern" => "\\d+ taco"}
349
+ }
350
+ }
351
+
352
+ data = {
353
+ "a" => nil
354
+ }
355
+
356
+ # Test strings
357
+ data["a"] = "156 taco bell"
358
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
359
+
360
+ # Test a non-string
361
+ data["a"] = 5
362
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
363
+
364
+ data["a"] = "taco"
365
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
366
+ end
367
+
368
+
369
+ def test_min_length
370
+ # Set up the default datatype
371
+ schema = {
372
+ "properties" => {
373
+ "a" => {"minLength" => 1}
374
+ }
375
+ }
376
+
377
+ data = {
378
+ "a" => nil
379
+ }
380
+
381
+ # Try out strings
382
+ data["a"] = "t"
383
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
384
+
385
+ data["a"] = ""
386
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
387
+
388
+ # Try out non-string
389
+ data["a"] = 5
390
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
391
+ end
392
+
393
+
394
+ def test_max_length
395
+ # Set up the default datatype
396
+ schema = {
397
+ "properties" => {
398
+ "a" => {"maxLength" => 1}
399
+ }
400
+ }
401
+
402
+ data = {
403
+ "a" => nil
404
+ }
405
+
406
+ # Try out strings
407
+ data["a"] = "t"
408
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
409
+
410
+ data["a"] = "tt"
411
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
412
+
413
+ # Try out non-string
414
+ data["a"] = 5
415
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
416
+ end
417
+
418
+
419
+ def test_enum
420
+ # Set up the default datatype
421
+ schema = {
422
+ "properties" => {
423
+ "a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}], "optional" => true}
424
+ }
425
+ }
426
+
427
+ data = {
428
+ "a" => nil
429
+ }
430
+
431
+ # Make sure all of the above are valid...
432
+ data["a"] = 1
433
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
434
+
435
+ data["a"] = 'boo'
436
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
437
+
438
+ data["a"] = [1,2,3]
439
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
440
+
441
+ data["a"] = {"a" => "b"}
442
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
443
+
444
+ # Test something that doesn't exist
445
+ data["a"] = 'taco'
446
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
447
+
448
+ # Try it without the key
449
+ data = {}
450
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
451
+ end
452
+
453
+
454
+ def test_max_decimal
455
+ # Set up the default datatype
456
+ schema = {
457
+ "properties" => {
458
+ "a" => {"maxDecimal" => 2}
459
+ }
460
+ }
461
+
462
+ data = {
463
+ "a" => nil
464
+ }
465
+
466
+ data["a"] = 3.35
467
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
468
+
469
+ data["a"] = 3.455
470
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
471
+
472
+
473
+ schema["properties"]["a"]["maxDecimal"] = 0
474
+
475
+ data["a"] = 4.0
476
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
477
+
478
+ data["a"] = 'boo'
479
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
480
+
481
+ data["a"] = 5
482
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
483
+ end
484
+
485
+
486
+
487
+ def test_disallow
488
+ # Set up the default datatype
489
+ schema = {
490
+ "properties" => {
491
+ "a" => {"disallow" => "integer"}
492
+ }
493
+ }
494
+
495
+ data = {
496
+ "a" => nil
497
+ }
498
+
499
+
500
+ data["a"] = 'string'
501
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
502
+
503
+ data["a"] = 5
504
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
505
+
506
+
507
+ schema["properties"]["a"]["disallow"] = ["integer","string"]
508
+ data["a"] = 'string'
509
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
510
+
511
+ data["a"] = 5
512
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
513
+
514
+ data["a"] = false
515
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
516
+
517
+ end
518
+
519
+
520
+
521
+ def test_additional_properties
522
+ # Test no additional properties allowed
523
+ schema = {
524
+ "properties" => {
525
+ "a" => { "type" => "integer" }
526
+ },
527
+ "additionalProperties" => false
528
+ }
529
+
530
+ data = {
531
+ "a" => 10
532
+ }
533
+
534
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
535
+ data["b"] = 5
536
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
537
+
538
+ # Test additional properties match a schema
539
+ schema["additionalProperties"] = { "type" => "string" }
540
+ data["b"] = "taco"
541
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
542
+ data["b"] = 5
543
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
544
+ end
545
+
546
+
547
+ def test_items
548
+ schema = {
549
+ "items" => { "type" => "integer" }
550
+ }
551
+
552
+ data = [1,2,4]
553
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
554
+ data = [1,2,"string"]
555
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
556
+
557
+ schema = {
558
+ "items" => [
559
+ {"type" => "integer"},
560
+ {"type" => "string"}
561
+ ]
562
+ }
563
+
564
+ data = [1,"string"]
565
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
566
+ data = [1,"string",3]
567
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
568
+ data = ["string",1]
569
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
570
+
571
+ end
572
+
573
+
574
+ def test_format_ipv4
575
+ schema = {
576
+ "type" => "object",
577
+ "properties" => { "a" => {"type" => "string", "format" => "ip-address"}}
578
+ }
579
+
580
+ data = {"a" => "1.1.1.1"}
581
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
582
+ data = {"a" => "1.1.1"}
583
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
584
+ data = {"a" => "1.1.1.300"}
585
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
586
+ data = {"a" => 5}
587
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
588
+ data = {"a" => "1.1.1"}
589
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
590
+ data = {"a" => "1.1.1.1b"}
591
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
592
+ data = {"a" => "b1.1.1.1"}
593
+ end
594
+
595
+
596
+ def test_format_ipv6
597
+ schema = {
598
+ "type" => "object",
599
+ "properties" => { "a" => {"type" => "string", "format" => "ipv6"}}
600
+ }
601
+
602
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"}
603
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
604
+ data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"}
605
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
606
+ data = {"a" => "1111:2222:8888::eeee:ffff"}
607
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
608
+ data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"}
609
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
610
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"}
611
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
612
+ data = {"a" => "1111:2222::9999::cccc:eeee:ffff"}
613
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
614
+ data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
615
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
616
+ end
617
+
618
+ def test_format_time
619
+ schema = {
620
+ "type" => "object",
621
+ "properties" => { "a" => {"type" => "string", "format" => "time"}}
622
+ }
623
+
624
+ data = {"a" => "12:00:00"}
625
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
626
+ data = {"a" => "12:00"}
627
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
628
+ data = {"a" => "12:00:60"}
629
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
630
+ data = {"a" => "12:60:00"}
631
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
632
+ data = {"a" => "24:00:00"}
633
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
634
+ data = {"a" => "0:00:00"}
635
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
636
+ data = {"a" => "-12:00:00"}
637
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
638
+ data = {"a" => "12:00:00b"}
639
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
640
+ end
641
+
642
+
643
+ def test_format_date
644
+ schema = {
645
+ "type" => "object",
646
+ "properties" => { "a" => {"type" => "string", "format" => "date"}}
647
+ }
648
+
649
+ data = {"a" => "2010-01-01"}
650
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
651
+ data = {"a" => "2010-01-32"}
652
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
653
+ data = {"a" => "n2010-01-01"}
654
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
655
+ data = {"a" => "2010-1-01"}
656
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
657
+ data = {"a" => "2010-01-1"}
658
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
659
+ data = {"a" => "2010-01-01n"}
660
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
661
+ end
662
+
663
+ def test_format_datetime
664
+ schema = {
665
+ "type" => "object",
666
+ "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
667
+ }
668
+
669
+ data = {"a" => "2010-01-01T12:00:00Z"}
670
+ assert(JSON::Validator.validate(schema,data,:version => :draft1))
671
+ data = {"a" => "2010-01-32T12:00:00Z"}
672
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
673
+ data = {"a" => "2010-13-01T12:00:00Z"}
674
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
675
+ data = {"a" => "2010-01-01T24:00:00Z"}
676
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
677
+ data = {"a" => "2010-01-01T12:60:00Z"}
678
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
679
+ data = {"a" => "2010-01-01T12:00:60Z"}
680
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
681
+ data = {"a" => "2010-01-01T12:00:00"}
682
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
683
+ data = {"a" => "2010-01-01T12:00:00z"}
684
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
685
+ data = {"a" => "2010-01-0112:00:00Z"}
686
+ assert(!JSON::Validator.validate(schema,data,:version => :draft1))
687
+ end
688
+
689
+
690
+ def test_format_strings
691
+ data1 = {"a" => "boo"}
692
+ data2 = {"a" => 5}
693
+
694
+ schema = {
695
+ "type" => "object",
696
+ "properties" => { "a" => {"format" => "regex"}}
697
+ }
698
+ assert(JSON::Validator.validate(schema,data1,:version => :draft1))
699
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft1))
700
+
701
+ schema = {
702
+ "type" => "object",
703
+ "properties" => { "a" => {"format" => "color"}}
704
+ }
705
+ assert(JSON::Validator.validate(schema,data1))
706
+ assert(!JSON::Validator.validate(schema,data2))
707
+
708
+ schema = {
709
+ "type" => "object",
710
+ "properties" => { "a" => {"format" => "style"}}
711
+ }
712
+ assert(JSON::Validator.validate(schema,data1,:version => :draft1))
713
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft1))
714
+
715
+ schema = {
716
+ "type" => "object",
717
+ "properties" => { "a" => {"format" => "phone"}}
718
+ }
719
+ assert(JSON::Validator.validate(schema,data1,:version => :draft1))
720
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft1))
721
+
722
+ schema = {
723
+ "type" => "object",
724
+ "properties" => { "a" => {"format" => "uri"}}
725
+ }
726
+ assert(JSON::Validator.validate(schema,data1,:version => :draft1))
727
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft1))
728
+
729
+ schema = {
730
+ "type" => "object",
731
+ "properties" => { "a" => {"format" => "email"}}
732
+ }
733
+ assert(JSON::Validator.validate(schema,data1,:version => :draft1))
734
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft1))
735
+
736
+ schema = {
737
+ "type" => "object",
738
+ "properties" => { "a" => {"format" => "host-name"}}
739
+ }
740
+ assert(JSON::Validator.validate(schema,data1,:version => :draft1))
741
+ assert(!JSON::Validator.validate(schema,data2,:version => :draft1))
742
+ end
743
+
744
+
745
+ def test_format_numeric
746
+ data1 = {"a" => "boo"}
747
+ data2 = {"a" => 5}
748
+ data3 = {"a" => 5.4}
749
+
750
+ schema = {
751
+ "type" => "object",
752
+ "properties" => { "a" => {"format" => "utc-millisec"}}
753
+ }
754
+ assert(!JSON::Validator.validate(schema,data1,:version => :draft1))
755
+ assert(JSON::Validator.validate(schema,data2,:version => :draft1))
756
+ assert(JSON::Validator.validate(schema,data3,:version => :draft1))
757
+ end
758
+
759
+ end
760
+