json-schema 2.1.3 → 2.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NWQ0NmRiOWRjODkxOWJiYTU4MmVhMTI4NmI1MDQyZDkzMzU2ZWQ1Zg==
5
+ data.tar.gz: !binary |-
6
+ MDcwZGNmZjk4YmY1ZjVjZjIwNGFlNGQ4MjNmYTllOGY5MzRjZWE0YQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YzFkOGY5ZWZlOGIxOTVkYzUyOTU3MDRiYTljN2IwN2UyOGI4MTY2YmVkM2Iy
10
+ ZmU3MzZkODI2ZDlkYmQ4YTZjOWE2MTYwZTc2Mjc1MjhkN2QxZDA5ZTliNmMz
11
+ YTc1YzMzYzEzNzMxNDQ1ZTU2YWU2NDEyZGRkMThjMTMyMjI1Mzc=
12
+ data.tar.gz: !binary |-
13
+ YWIzMjE4ZGZkYTA3NjIzODg2OGNmMWZmNDJkM2VjNzhhYzZlZDZjMTU1Yzgw
14
+ NzA0NzlkYjFhNjJjZDZjMmQ5OTBiYjUzMWVmOTQyM2U3YzIzMzQyOTIyNDUz
15
+ OTM1MzhiNTkxODlmMmVlMjEzYWQ4MWIzYWRhZjM5OTQxMTEwOWQ=
@@ -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.1.3.gem
25
+ $ gem install json-schema-2.1.4.gem
26
26
  </pre>
27
27
 
28
28
 
@@ -20,5 +20,5 @@ require 'rubygems'
20
20
  require 'schema'
21
21
  require 'validator'
22
22
  Dir[File.join(File.dirname(__FILE__), "json-schema/attributes/*.rb")].each {|file| require file }
23
- Dir[File.join(File.dirname(__FILE__), "json-schema/validators/*.rb")].each {|file| require file }
23
+ Dir[File.join(File.dirname(__FILE__), "json-schema/validators/*.rb")].sort!.each {|file| require file }
24
24
  require 'uri/file'
@@ -26,7 +26,7 @@ module JSON
26
26
  end
27
27
  end
28
28
 
29
- if !valid
29
+ if !valid || !errors.empty?
30
30
  message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match all of the required schemas"
31
31
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
32
32
  validation_errors(processor).last.sub_errors = errors
@@ -34,4 +34,4 @@ module JSON
34
34
  end
35
35
  end
36
36
  end
37
- end
37
+ end
@@ -9,7 +9,7 @@ module JSON
9
9
  if data.is_a?(String)
10
10
  error_message = "The property '#{build_fragment(fragments)}' must be a date/time in the ISO-8601 format of YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss.ssZ"
11
11
  validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if !data.is_a?(String)
12
- r = Regexp.new('^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)([\.,]\d+)?(Z|[+-](\d\d)(:\d\d)?)?$')
12
+ r = Regexp.new('^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)([\.,]\d+)?(Z|[+-](\d\d)(:?\d\d)?)?$')
13
13
  if (m = r.match(data))
14
14
  parts = data.split("T")
15
15
  begin
@@ -2,34 +2,31 @@ module JSON
2
2
  class Schema
3
3
  class OneOfAttribute < Attribute
4
4
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
5
- matched = false
6
- valid = false
7
-
5
+ validation_errors = 0
8
6
  current_schema.schema['oneOf'].each do |element|
9
7
  schema = JSON::Schema.new(element,current_schema.uri,validator)
10
8
 
11
9
  begin
12
- schema.validate(data,fragments,processor,options)
13
- if matched
14
- valid = false
15
- else
16
- matched = true
17
- valid = true
18
- end
10
+ # need to raise exceptions on error because
11
+ # schema.validate doesn't reliably return true/false
12
+ schema.validate(data,fragments,processor,options.merge(:record_errors => false))
19
13
  rescue ValidationError
14
+ validation_errors += 1
20
15
  end
21
16
 
22
17
  end
23
18
 
24
- if !valid
25
- if matched
26
- message = "The property '#{build_fragment(fragments)}' of type #{data.class} matched more than one of the required schemas"
27
- else
28
- message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match any of the required schemas"
29
- end
30
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
19
+ case validation_errors
20
+ when current_schema.schema['oneOf'].length - 1 # correct, matched only one
21
+ message = nil
22
+ when current_schema.schema['oneOf'].length # didn't match any
23
+ message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match any of the required schemas"
24
+ else # too many matches
25
+ message = "The property '#{build_fragment(fragments)}' of type #{data.class} matched more than one of the required schemas"
31
26
  end
27
+
28
+ validation_error(processor, message, fragments, current_schema, self, options[:record_errors]) if message
32
29
  end
33
30
  end
34
31
  end
35
- end
32
+ end
@@ -1,3 +1,4 @@
1
+ require 'rbconfig'
1
2
  require 'uri'
2
3
 
3
4
  module URI
@@ -5,28 +6,31 @@ module URI
5
6
  # Ruby does not have built-in support for filesystem URIs, and definitely does not have built-in support for
6
7
  # using open-uri with filesystem URIs
7
8
  class File < Generic
8
-
9
+
9
10
  COMPONENT = [
10
- :scheme,
11
- :path,
11
+ :scheme,
12
+ :path,
12
13
  :fragment,
13
14
  :host
14
15
  ].freeze
15
-
16
+
16
17
  def initialize(*arg)
17
- arg[2] = ""
18
+ # arg[2] is the 'host'; this logic to set it to "" causes file schemes with UNC to break
19
+ # so don't do it on windows platforms
20
+ is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
21
+ arg[2] = "" unless is_windows
18
22
  super(*arg)
19
23
  end
20
-
24
+
21
25
  def self.build(args)
22
26
  tmp = Util::make_components_hash(self, args)
23
27
  return super(tmp)
24
28
  end
25
-
29
+
26
30
  def open(*rest, &block)
27
31
  ::File.open(self.path, *rest, &block)
28
32
  end
29
-
33
+
30
34
  @@schemes['FILE'] = File
31
35
  end
32
- end
36
+ end
@@ -189,21 +189,28 @@ module JSON
189
189
  end
190
190
 
191
191
  fragments.each do |f|
192
- if base_schema.is_a?(Hash)
192
+ if base_schema.is_a?(JSON::Schema) #test if fragment is a JSON:Schema instance
193
+ if !base_schema.schema.has_key?(f)
194
+ raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
195
+ end
196
+ base_schema = base_schema.schema[f]
197
+ elsif base_schema.is_a?(Hash)
193
198
  if !base_schema.has_key?(f)
194
199
  raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
195
200
  end
196
- base_schema = base_schema[f]
201
+ base_schema = initialize_schema(base_schema[f]) #need to return a Schema instance for validation to work
197
202
  elsif base_schema.is_a?(Array)
198
203
  if base_schema[f.to_i].nil?
199
204
  raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
200
205
  end
201
- base_schema = base_schema[f.to_i]
206
+ base_schema = initialize_schema(base_schema[f.to_i])
202
207
  else
203
208
  raise JSON::Schema::SchemaError.new("Invalid schema encountered when resolving :fragment option")
204
209
  end
205
210
  end
206
-
211
+ if @options[:list] #check if the schema is validating a list
212
+ base_schema.schema = schema_to_list(base_schema.schema)
213
+ end
207
214
  base_schema
208
215
  end
209
216
 
@@ -553,18 +560,23 @@ module JSON
553
560
  @@fake_uri_generator.call(schema)
554
561
  end
555
562
 
563
+ def schema_to_list(schema)
564
+ new_schema = {"type" => "array", "items" => schema}
565
+ if !schema["$schema"].nil?
566
+ new_schema["$schema"] = schema["$schema"]
567
+ end
568
+
569
+ new_schema
570
+ end
571
+
556
572
  def initialize_schema(schema)
557
573
  if schema.is_a?(String)
558
574
  begin
559
575
  # Build a fake URI for this
560
576
  schema_uri = URI.parse(fake_uri(schema))
561
577
  schema = JSON::Validator.parse(schema)
562
- if @options[:list]
563
- new_schema = {"type" => "array", "items" => schema}
564
- if !schema["$schema"].nil?
565
- new_schema["$schema"] = schema["$schema"]
566
- end
567
- schema = new_schema
578
+ if @options[:list] && @options[:fragment].nil?
579
+ schema = schema_to_list(schema)
568
580
  end
569
581
  schema = JSON::Schema.new(schema,schema_uri,@options[:version])
570
582
  Validator.add_schema(schema)
@@ -581,12 +593,8 @@ module JSON
581
593
  end
582
594
  if Validator.schemas[schema_uri.to_s].nil?
583
595
  schema = JSON::Validator.parse(open(schema_uri.to_s).read)
584
- if @options[:list]
585
- new_schema = {"type" => "array", "items" => schema}
586
- if !schema["$schema"].nil?
587
- new_schema["$schema"] = schema["$schema"]
588
- end
589
- schema = new_schema
596
+ if @options[:list] && @options[:fragment].nil?
597
+ schema = schema_to_list(schema)
590
598
  end
591
599
  schema = JSON::Schema.new(schema,schema_uri,@options[:version])
592
600
  Validator.add_schema(schema)
@@ -595,12 +603,8 @@ module JSON
595
603
  end
596
604
  end
597
605
  elsif schema.is_a?(Hash)
598
- if @options[:list]
599
- new_schema = {"type" => "array", "items" => schema}
600
- if !schema["$schema"].nil?
601
- new_schema["$schema"] = schema["$schema"]
602
- end
603
- schema = new_schema
606
+ if @options[:list] && @options[:fragment].nil?
607
+ schema = schema_to_list(schema)
604
608
  end
605
609
  schema_uri = URI.parse(fake_uri(serialize(schema)))
606
610
  schema = JSON::Schema.new(schema,schema_uri,@options[:version])
@@ -0,0 +1,3 @@
1
+ {
2
+ "name" : "john"
3
+ }
@@ -0,0 +1,5 @@
1
+ { "links":
2
+ [{ "rel" : ["self"] , "href":"http://api.example.com/api/object/3" }
3
+ ,{ "rel" : ["up"] , "href":"http://api.example.com/api/object" }
4
+ ]
5
+ }
@@ -0,0 +1,4 @@
1
+ {"type": "object",
2
+ "properties" : {
3
+ "name" : { "type": "integer" }
4
+ }
@@ -0,0 +1,7 @@
1
+ { "$schema" : "http://json-schema.org/draft-04/schema#",
2
+ "type" : "object",
3
+ "allOf" :
4
+ [ { "$ref" : "all_of_ref_base_schema.json" }
5
+ ]
6
+
7
+ }
@@ -0,0 +1,16 @@
1
+ { "$schema": "http://json-schema.org/draft-04/schema#"
2
+ , "type": "object"
3
+ , "properties":
4
+ { "links" :
5
+ { "type" : "array"
6
+ , "items" :
7
+ { "type" : "object"
8
+ , "oneOf" :
9
+ [ { "$ref" : "self_link_schema.json"}
10
+ , { "$ref" : "up_link_schema.json" }
11
+ ]
12
+ }
13
+ }
14
+ }
15
+ }
16
+
@@ -0,0 +1,17 @@
1
+ { "$schema": "http://json-schema.org/draft-04/schema#"
2
+ , "type": "object"
3
+ , "properties" :
4
+ { "rel" :
5
+ { "type" : "array"
6
+ , "items" :
7
+ [ { "type" : "string"
8
+ , "pattern" : "self"
9
+ }
10
+ ]
11
+ }
12
+ , "href" :
13
+ { "type" : "string"
14
+ }
15
+ }
16
+ }
17
+
@@ -0,0 +1,17 @@
1
+ { "$schema": "http://json-schema.org/draft-04/schema#"
2
+ , "type": "object"
3
+ , "properties" :
4
+ { "rel" :
5
+ { "type" : "array"
6
+ , "items" :
7
+ [ { "type" : "string"
8
+ , "pattern" : "up"
9
+ }
10
+ ]
11
+ }
12
+ , "href" :
13
+ { "type" : "string"
14
+ }
15
+ }
16
+ }
17
+
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class AnyOfRefSchemaTest < Test::Unit::TestCase
5
+ def test_all_of_ref_schema
6
+ schema = File.join(File.dirname(__FILE__),"schemas/all_of_ref_schema.json")
7
+ data = File.join(File.dirname(__FILE__),"data/all_of_ref_data.json")
8
+ errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true)
9
+ assert(!errors.empty?, "should have failed to validate")
10
+ end
11
+ end
@@ -2,7 +2,7 @@ require 'test/unit'
2
2
  require File.dirname(__FILE__) + '/../lib/json-schema'
3
3
 
4
4
  class AnyOfRefSchemaTest < Test::Unit::TestCase
5
- def test_all_of_ref_schema
5
+ def test_any_of_ref_schema
6
6
  schema = File.join(File.dirname(__FILE__),"schemas/any_of_ref_schema.json")
7
7
  data = File.join(File.dirname(__FILE__),"data/any_of_ref_data.json")
8
8
  errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true)
@@ -5,6 +5,7 @@ class FragmentResolution < Test::Unit::TestCase
5
5
  def test_fragment_resolution
6
6
  schema = {
7
7
  "$schema" => "http://json-schema.org/draft-04/schema#",
8
+ "required" => ["a"],
8
9
  "properties" => {
9
10
  "a" => {
10
11
  "type" => "object",
@@ -970,7 +970,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
970
970
  data = {"a" => "2010-01-01T12:00:00+01:30"}
971
971
  assert(JSON::Validator.validate(schema,data))
972
972
  data = {"a" => "2010-01-01T12:00:00+0234"}
973
- assert(!JSON::Validator.validate(schema,data))
973
+ assert(JSON::Validator.validate(schema,data))
974
974
  data = {"a" => "2010-01-01T12:00:00+01:"}
975
975
  assert(!JSON::Validator.validate(schema,data))
976
976
  data = {"a" => "2010-01-01T12:00:00+0"}
@@ -893,6 +893,10 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
893
893
  assert(JSON::Validator.validate(schema,data))
894
894
  data = {"a" => "2010-01-01T12:00:00,1Z"}
895
895
  assert(JSON::Validator.validate(schema,data))
896
+ data = {"a" => "2010-01-01T12:00:00+0000"}
897
+ assert(JSON::Validator.validate(schema,data))
898
+ data = {"a" => "2010-01-01T12:00:00+00:00"}
899
+ assert(JSON::Validator.validate(schema,data))
896
900
  data = {"a" => "2010-01-32T12:00:00Z"}
897
901
  assert(!JSON::Validator.validate(schema,data))
898
902
  data = {"a" => "2010-13-01T12:00:00Z"}
@@ -0,0 +1,42 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class OneOfTest < Test::Unit::TestCase
5
+ def test_one_of_links_schema
6
+ schema = File.join(File.dirname(__FILE__),"schemas/one_of_ref_links_schema.json")
7
+ data = File.join(File.dirname(__FILE__),"data/one_of_ref_links_data.json")
8
+ errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true)
9
+ assert(errors.empty?, errors.map{|e| e[:message] }.join("\n"))
10
+ end
11
+
12
+
13
+ def test_one_of_with_string_patterns
14
+ schema = {
15
+ "$schema" => "http://json-schema.org/draft-04/schema#",
16
+ "oneOf" => [
17
+ {
18
+ "properties" => {"a" => {"type" => "string", "pattern" => "foo"}},
19
+ },
20
+ {
21
+ "properties" => {"a" => {"type" => "string", "pattern" => "bar"}},
22
+ },
23
+ {
24
+ "properties" => {"a" => {"type" => "string", "pattern" => "baz"}},
25
+ }
26
+ ]
27
+ }
28
+
29
+ data = {"a" => "foo"}
30
+ assert(JSON::Validator.validate(schema,data))
31
+
32
+ data = {"a" => "foobar"}
33
+ assert(!JSON::Validator.validate(schema,data))
34
+
35
+ data = {"a" => "baz"}
36
+ assert(JSON::Validator.validate(schema,data))
37
+
38
+ data = {"a" => 5}
39
+ assert(!JSON::Validator.validate(schema,data))
40
+ end
41
+
42
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.3
5
- prerelease:
4
+ version: 2.1.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Kenny Hoxworth
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-02 00:00:00.000000000 Z
11
+ date: 2013-12-31 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description:
15
14
  email: hoxworth@gmail.com
@@ -70,22 +69,28 @@ files:
70
69
  - resources/draft-04.json
71
70
  - README.textile
72
71
  - LICENSE.md
73
- - test/schemas/test_fragment_resolution.rb
72
+ - test/test_all_of_ref_schema.rb
74
73
  - test/test_any_of_ref_schema.rb
75
74
  - test/test_extended_schema.rb
76
75
  - test/test_extends_and_additionalProperties.rb
77
76
  - test/test_files_v3.rb
77
+ - test/test_fragment_resolution.rb
78
78
  - test/test_full_validation.rb
79
79
  - test/test_jsonschema_draft1.rb
80
80
  - test/test_jsonschema_draft2.rb
81
81
  - test/test_jsonschema_draft3.rb
82
82
  - test/test_jsonschema_draft4.rb
83
+ - test/test_one_of.rb
83
84
  - test/test_ruby_schema.rb
84
85
  - test/test_schema_type_attribute.rb
85
86
  - test/test_schema_validation.rb
87
+ - test/data/all_of_ref_data.json
86
88
  - test/data/any_of_ref_data.json
87
89
  - test/data/bad_data_1.json
88
90
  - test/data/good_data_1.json
91
+ - test/data/one_of_ref_links_data.json
92
+ - test/schemas/all_of_ref_base_schema.json
93
+ - test/schemas/all_of_ref_schema.json
89
94
  - test/schemas/any_of_ref_jane_schema.json
90
95
  - test/schemas/any_of_ref_jimmy_schema.json
91
96
  - test/schemas/any_of_ref_john_schema.json
@@ -99,47 +104,56 @@ files:
99
104
  - test/schemas/good_schema_extends1.json
100
105
  - test/schemas/good_schema_extends2.json
101
106
  - test/schemas/inner.schema.json
107
+ - test/schemas/one_of_ref_links_schema.json
108
+ - test/schemas/self_link_schema.json
109
+ - test/schemas/up_link_schema.json
102
110
  homepage: http://github.com/hoxworth/json-schema/tree/master
103
- licenses: []
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
104
114
  post_install_message:
105
115
  rdoc_options: []
106
116
  require_paths:
107
117
  - lib
108
118
  required_ruby_version: !ruby/object:Gem::Requirement
109
- none: false
110
119
  requirements:
111
120
  - - ! '>='
112
121
  - !ruby/object:Gem::Version
113
122
  version: '0'
114
123
  required_rubygems_version: !ruby/object:Gem::Requirement
115
- none: false
116
124
  requirements:
117
125
  - - ! '>='
118
126
  - !ruby/object:Gem::Version
119
127
  version: '0'
120
128
  requirements: []
121
129
  rubyforge_project:
122
- rubygems_version: 1.8.24
130
+ rubygems_version: 2.0.7
123
131
  signing_key:
124
- specification_version: 3
132
+ specification_version: 4
125
133
  summary: Ruby JSON Schema Validator
126
134
  test_files:
127
- - test/schemas/test_fragment_resolution.rb
135
+ - test/test_all_of_ref_schema.rb
128
136
  - test/test_any_of_ref_schema.rb
129
137
  - test/test_extended_schema.rb
130
138
  - test/test_extends_and_additionalProperties.rb
131
139
  - test/test_files_v3.rb
140
+ - test/test_fragment_resolution.rb
132
141
  - test/test_full_validation.rb
133
142
  - test/test_jsonschema_draft1.rb
134
143
  - test/test_jsonschema_draft2.rb
135
144
  - test/test_jsonschema_draft3.rb
136
145
  - test/test_jsonschema_draft4.rb
146
+ - test/test_one_of.rb
137
147
  - test/test_ruby_schema.rb
138
148
  - test/test_schema_type_attribute.rb
139
149
  - test/test_schema_validation.rb
150
+ - test/data/all_of_ref_data.json
140
151
  - test/data/any_of_ref_data.json
141
152
  - test/data/bad_data_1.json
142
153
  - test/data/good_data_1.json
154
+ - test/data/one_of_ref_links_data.json
155
+ - test/schemas/all_of_ref_base_schema.json
156
+ - test/schemas/all_of_ref_schema.json
143
157
  - test/schemas/any_of_ref_jane_schema.json
144
158
  - test/schemas/any_of_ref_jimmy_schema.json
145
159
  - test/schemas/any_of_ref_john_schema.json
@@ -153,3 +167,6 @@ test_files:
153
167
  - test/schemas/good_schema_extends1.json
154
168
  - test/schemas/good_schema_extends2.json
155
169
  - test/schemas/inner.schema.json
170
+ - test/schemas/one_of_ref_links_schema.json
171
+ - test/schemas/self_link_schema.json
172
+ - test/schemas/up_link_schema.json