json-schema 2.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NmM0OTNmZTRkM2JmNmNlYWM2MDA0ZjNlZmFlMzZkM2JjOWRmZGQ1ZQ==
4
+ YTUxNTA3MTA3MmZmODJlZDJhYjk5NzIxM2FjNmY4MjdlODgzNDc0NQ==
5
5
  data.tar.gz: !binary |-
6
- MTdmODg5Njc3NmRjZDliZGU1YjYxMGQwNWQ1ZGY4NDFhMWEyZjcxZA==
6
+ ODJlOGI0OTMwNjU5OTU2OTFmMjg5MTE1ODlkYTFhNjI1NGY3ZjNjNA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MjUxNmNmNWUzZWQ1MzgyMDk1YjZiNTkyODM1NzFiNmU2MWI0NDMxY2NkN2Rj
10
- M2VhNDBkODlkZDg0MGEyMDZhMWFmZWU2MzIwYTBjNjliYWVkM2Y5YTZhNGM2
11
- Y2Y5ZjgzNzJmYzhkZjhlMWI0ODljMzA3Mjk5N2JkNjlhZmIzNWQ=
9
+ ZmE4OTAxOWJkNTc3ZTcyNWU3MTRkMDdkMzAxMjk2ZDc5ZTBiYTVmY2E5ZDY1
10
+ NWNjYWM1ZTMxMjU4ZjcyZmMwMWNjNjQ4ZTk1Y2QzNjBkZjRiYThlOTg1NjZk
11
+ YWI3MDEwYjRmNjBkZWNjZjIwNjBiZWJmNzZjNTVhODY2NDZlNmM=
12
12
  data.tar.gz: !binary |-
13
- MzQ3NWRlNzJhZWY5NWFjZWRmN2U4N2MxMTlkMjgyNDU3OWMxMWFjOWY2MDk5
14
- Njk3YjZjZGQyNTk2MjFhZjczZTRkZDgwZmQ3MWFlZDM3YTVkMTAyYmNlMWI1
15
- ZWQ4YTkyZDRlNzY5NGYzMDY3MmZkY2ExYmRiMzdkY2U4MzFjNDU=
13
+ MGJlODg2YTY5MTgyOTRmYTRmNTMwMzNhOGI1ZDVkY2RhN2VjNzNiYTRjNmMz
14
+ NjA3NjI1ZDlkNzgxNzVkYzdiNGUzM2IwMTAwZTNjNzlmMWQyYjE2NjIzOTA1
15
+ NWEzNWU1ZWM0NGM2MjFiMzk3MDA1ODUyZDI2M2IyYjkyNWJmYTQ=
@@ -22,7 +22,7 @@ From the git repo:
22
22
 
23
23
  <pre>
24
24
  $ gem build json-schema.gemspec
25
- $ gem install json-schema-2.2.0.gem
25
+ $ gem install json-schema-2.2.1.gem
26
26
  </pre>
27
27
 
28
28
 
@@ -82,8 +82,17 @@ With the <code>:strict</code>code> option, validation fails when an object conta
82
82
  require 'rubygems'
83
83
  require 'json-schema'
84
84
 
85
- data = ['user','user','user']
86
- JSON::Validator.validate('user.json', data, :list => true)
85
+ schema = {
86
+ "type" => "object",
87
+ "properties" => {
88
+ "a" => {"type" => "integer"},
89
+ "b" => {"type" => "integer"}
90
+ }
91
+ }
92
+
93
+ JSON::Validator.validate(schema, {"a" => 1, "b" => 2}, :strict => true) # ==> true
94
+ JSON::Validator.validate(schema, {"a" => 1, "b" => 2, "c" => 3}, :strict => true) # ==> false
95
+ JSON::Validator.validate(schema, {"a" => 1}, :strict => true) # ==> false
87
96
  </pre>
88
97
 
89
98
  h3. Catch a validation error and print it out
@@ -30,7 +30,20 @@ module JSON
30
30
  # When strict is true, ensure no undefined properties exist in the data
31
31
  if (options[:strict] == true && !current_schema.schema.has_key?('additionalProperties'))
32
32
  diff = data.select do |k,v|
33
- !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym)
33
+ if current_schema.schema.has_key?('patternProperties')
34
+ match = false
35
+ current_schema.schema['patternProperties'].each do |property,property_schema|
36
+ r = Regexp.new(property)
37
+ if r.match(k)
38
+ match = true
39
+ break
40
+ end
41
+ end
42
+
43
+ !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym) && !match
44
+ else
45
+ !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym)
46
+ end
34
47
  end
35
48
 
36
49
  if diff.size > 0
@@ -30,7 +30,20 @@ module JSON
30
30
  # When strict is true, ensure no undefined properties exist in the data
31
31
  if (options[:strict] == true && !current_schema.schema.has_key?('additionalProperties'))
32
32
  diff = data.select do |k,v|
33
- !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym)
33
+ if current_schema.schema.has_key?('patternProperties')
34
+ match = false
35
+ current_schema.schema['patternProperties'].each do |property,property_schema|
36
+ r = Regexp.new(property)
37
+ if r.match(k)
38
+ match = true
39
+ break
40
+ end
41
+ end
42
+
43
+ !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym) && !match
44
+ else
45
+ !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym)
46
+ end
34
47
  end
35
48
 
36
49
  if diff.size > 0
@@ -528,7 +528,37 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
528
528
  assert(JSON::Validator.validate(schema,data,:strict => true))
529
529
  end
530
530
 
531
+ def test_strict_properties_pattern_props
532
+ schema = {
533
+ "$schema" => "http://json-schema.org/draft-03/schema#",
534
+ "properties" => {
535
+ "a" => {"type" => "string"},
536
+ "b" => {"type" => "string"}
537
+ },
538
+ "patternProperties" => {"\\d+ taco" => {"type" => "integer"}}
539
+ }
540
+
541
+ data = {"a" => "a"}
542
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
543
+
544
+ data = {"b" => "b"}
545
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
531
546
 
547
+ data = {"a" => "a", "b" => "b"}
548
+ assert(JSON::Validator.validate(schema,data,:strict => true))
549
+
550
+ data = {"a" => "a", "b" => "b", "c" => "c"}
551
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
552
+
553
+ data = {"a" => "a", "b" => "b", "c" => 3}
554
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
555
+
556
+ data = {"a" => "a", "b" => "b", "23 taco" => 3}
557
+ assert(JSON::Validator.validate(schema,data,:strict => true))
558
+
559
+ data = {"a" => "a", "b" => "b", "23 taco" => "cheese"}
560
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
561
+ end
532
562
 
533
563
  def test_pattern
534
564
  # Set up the default datatype
@@ -463,7 +463,37 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
463
463
  assert(JSON::Validator.validate(schema,data,:strict => true))
464
464
  end
465
465
 
466
+ def test_strict_properties_pattern_props
467
+ schema = {
468
+ "$schema" => "http://json-schema.org/draft-03/schema#",
469
+ "properties" => {
470
+ "a" => {"type" => "string"},
471
+ "b" => {"type" => "string"}
472
+ },
473
+ "patternProperties" => {"\\d+ taco" => {"type" => "integer"}}
474
+ }
466
475
 
476
+ data = {"a" => "a"}
477
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
478
+
479
+ data = {"b" => "b"}
480
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
481
+
482
+ data = {"a" => "a", "b" => "b"}
483
+ assert(JSON::Validator.validate(schema,data,:strict => true))
484
+
485
+ data = {"a" => "a", "b" => "b", "c" => "c"}
486
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
487
+
488
+ data = {"a" => "a", "b" => "b", "c" => 3}
489
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
490
+
491
+ data = {"a" => "a", "b" => "b", "23 taco" => 3}
492
+ assert(JSON::Validator.validate(schema,data,:strict => true))
493
+
494
+ data = {"a" => "a", "b" => "b", "23 taco" => "cheese"}
495
+ assert(!JSON::Validator.validate(schema,data,:strict => true))
496
+ end
467
497
 
468
498
  def test_unique_items
469
499
  # Set up the default datatype
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenny Hoxworth