json-schema 2.4.0 → 2.4.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
- ZjMyNzMwODM4YmUyNjNkZjBhNzkzN2E4M2MxYTM2YzhiMzg4MzZlYw==
4
+ ZTc1NDAxODkxMmNiYTQ4YzdiMzNkMWEyYjlhYTQ5MTU5MWExMTFhZQ==
5
5
  data.tar.gz: !binary |-
6
- ZWY0NWQ1ZWRlMzVlNTI4N2NhYjk0ZGM4OGIxMzMzNzU5NGE2ZmEwYg==
6
+ ZmQxNjIyOTU5ZGQyZGQ5ZDIxYmYxMGQwMGIzYjliMDMyYjQ5Y2EwMA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MDE3Yzc0MmYwNmFkNjUzYmNiYTFhYjg4ODY5MzZjODgyZWIyODBkYWUwZjJh
10
- NjJjODk4MGFhNTFlOTkyOTZkYTQ5ODQ2YjYxYjJjZGNjZWIxOTcxNzEyMmYy
11
- NzI2MDcwNGU5Mzg0N2IyYzU5NzU0NThkMTNjZjFmZDVhZGNiNjc=
9
+ YjVlZjkzNWE5YTA3MjhlNTY0ZmMyMGRiNGE0YzYwODE1N2I3ZTFhMTAzYjQ1
10
+ NTJjZGU2ZTE1OTg3NDJjNThhYzAwMGRiYzZiMzIzZWJiODRmNzA5NjVjMzAw
11
+ ZTE4MjdjYzE3NGQ0ZjlhYmQ3NWViZjVmOWNhYzc2YjY2NWNlYTU=
12
12
  data.tar.gz: !binary |-
13
- ZDJjNzIyZDg0NDUxODQyZjM4MzZkNWUxOWViYTA3MDM4MjdmN2Q2ZjBkOTBi
14
- NzllYzUyNDc5MDZkZmM0M2U3NmFiZDc5Njk3YTYxNzBiMjYwYjIwMjZmZWY0
15
- M2QxMzIwN2EzOTUxNWRjZmQ5ZGQwZTEwNjBjMDVjZTBlNTAxZWE=
13
+ ODA4M2U2YTkyMmM1NjhlMTUyN2M3NjJiODI3ZmUzMzNhYTFkNTE1YmRkYWM4
14
+ MWNiODZlYmQ3YjdkY2M4ZDk3ZDVkNTZlYmJkNmNjNWVjODhjOWQxNzA5MmU2
15
+ YjQwMDBmMjZmODdkOTI5YjBjMzUxMGJiOWNjY2JmNzZlZWZhYjY=
data/README.textile CHANGED
@@ -25,7 +25,7 @@ From the git repo:
25
25
 
26
26
  <pre>
27
27
  $ gem build json-schema.gemspec
28
- $ gem install json-schema-2.4.0.gem
28
+ $ gem install json-schema-2.4.1.gem
29
29
  </pre>
30
30
 
31
31
 
data/lib/json-schema.rb CHANGED
@@ -8,7 +8,6 @@ if Gem::Specification::find_all_by_name('multi_json').any?
8
8
  MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine
9
9
  end
10
10
 
11
- require 'json-schema/util/hash'
12
11
  require 'json-schema/util/array_set'
13
12
  require 'json-schema/schema'
14
13
  require 'json-schema/validator'
@@ -6,15 +6,15 @@ module JSON
6
6
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
7
  if data.is_a?(Hash)
8
8
  current_schema.schema['dependencies'].each do |property,dependency_value|
9
- if data.has_key?(property.to_s) || data.has_key?(property.to_sym)
9
+ if data.has_key?(property.to_s)
10
10
  if dependency_value.is_a?(String)
11
- if !data.has_key?(dependency_value.to_s) && !data.has_key?(dependency_value.to_sym)
11
+ if !data.has_key?(dependency_value.to_s)
12
12
  message = "The property '#{build_fragment(fragments)}' has a property '#{property}' that depends on a missing property '#{dependency_value}'"
13
13
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
14
14
  end
15
15
  elsif dependency_value.is_a?(Array)
16
16
  dependency_value.each do |value|
17
- if !data.has_key?(value.to_s) && !data.has_key?(value.to_sym)
17
+ if !data.has_key?(value.to_s)
18
18
  message = "The property '#{build_fragment(fragments)}' has a property '#{property}' that depends on a missing property '#{value}'"
19
19
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
20
20
  end
@@ -6,10 +6,10 @@ module JSON
6
6
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
7
  if data.is_a?(Hash)
8
8
  current_schema.schema['dependencies'].each do |property,dependency_value|
9
- if (data.has_key?(property.to_s) || data.has_key?(property.to_sym))
9
+ if data.has_key?(property.to_s)
10
10
  if dependency_value.is_a?(Array)
11
11
  dependency_value.each do |value|
12
- if !data.has_key?(value.to_s) && !data.has_key?(value.to_sym)
12
+ if !data.has_key?(value.to_s)
13
13
  message = "The property '#{build_fragment(fragments)}' has a property '#{property}' that depends on a missing property '#{value}'"
14
14
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
15
15
  end
@@ -7,7 +7,6 @@ module JSON
7
7
  if data.is_a?(Hash)
8
8
  current_schema.schema['properties'].each do |property,property_schema|
9
9
  if !data.has_key?(property.to_s) &&
10
- !data.has_key?(property.to_sym) &&
11
10
  property_schema['default'] &&
12
11
  !property_schema['readonly'] &&
13
12
  options[:insert_defaults]
@@ -15,13 +14,12 @@ module JSON
15
14
  data[property.to_s] = (default.is_a?(Hash) ? default.clone : default)
16
15
  end
17
16
 
18
- if property_schema.fetch('required') { options[:strict] } &&
19
- !data.has_key?(property.to_s) && !data.has_key?(property.to_sym)
17
+ if property_schema.fetch('required') { options[:strict] } && !data.has_key?(property.to_s)
20
18
  message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
21
19
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
22
20
  end
23
21
 
24
- if data.has_key?(property.to_s) || data.has_key?(property.to_sym)
22
+ if data.has_key?(property.to_s)
25
23
  schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
26
24
  fragments << property.to_s
27
25
  schema.validate(data[property.to_s],fragments,processor,options)
@@ -42,9 +40,9 @@ module JSON
42
40
  end
43
41
  end
44
42
 
45
- !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym) && !match
43
+ !current_schema.schema['properties'].has_key?(k.to_s) && !match
46
44
  else
47
- !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym)
45
+ !current_schema.schema['properties'].has_key?(k.to_s)
48
46
  end
49
47
  end
50
48
 
@@ -6,12 +6,12 @@ module JSON
6
6
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
7
  if data.is_a?(Hash)
8
8
  current_schema.schema['properties'].each do |property,property_schema|
9
- if ((property_schema['optional'].nil? || property_schema['optional'] == false) && !data.has_key?(property.to_s) && !data.has_key?(property.to_sym))
9
+ if (property_schema['optional'].nil? || property_schema['optional'] == false) && !data.has_key?(property.to_s)
10
10
  message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
11
11
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
12
12
  end
13
13
 
14
- if data.has_key?(property.to_s) || data.has_key?(property.to_sym)
14
+ if data.has_key?(property.to_s)
15
15
  schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
16
16
  fragments << property
17
17
  schema.validate(data[property],fragments,processor,options)
@@ -7,7 +7,6 @@ module JSON
7
7
  if data.is_a?(Hash)
8
8
  current_schema.schema['properties'].each do |property,property_schema|
9
9
  if !data.has_key?(property.to_s) &&
10
- !data.has_key?(property.to_sym) &&
11
10
  property_schema['default'] &&
12
11
  !property_schema['readonly'] &&
13
12
  options[:insert_defaults]
@@ -15,12 +14,12 @@ module JSON
15
14
  data[property.to_s] = (default.is_a?(Hash) ? default.clone : default)
16
15
  end
17
16
 
18
- if (options[:strict] == true && !data.has_key?(property.to_s) && !data.has_key?(property.to_sym))
17
+ if options[:strict] == true && !data.has_key?(property.to_s)
19
18
  message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
20
19
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
21
20
  end
22
21
 
23
- if data.has_key?(property.to_s) || data.has_key?(property.to_sym)
22
+ if data.has_key?(property.to_s)
24
23
  schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
25
24
  fragments << property.to_s
26
25
  schema.validate(data[property.to_s],fragments,processor,options)
@@ -42,9 +41,9 @@ module JSON
42
41
  end
43
42
  end
44
43
 
45
- !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym) && !match
44
+ !current_schema.schema['properties'].has_key?(k.to_s) && !match
46
45
  else
47
- !current_schema.schema['properties'].has_key?(k.to_s) && !current_schema.schema['properties'].has_key?(k.to_sym)
46
+ !current_schema.schema['properties'].has_key?(k.to_s)
48
47
  end
49
48
  end
50
49
 
@@ -6,7 +6,7 @@ module JSON
6
6
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
7
  if data.is_a?(Hash)
8
8
  current_schema.schema['required'].each do |property,property_schema|
9
- if !data.has_key?(property.to_s) && !data.has_key?(property.to_sym)
9
+ if !data.has_key?(property.to_s)
10
10
  prop_defaults = options[:insert_defaults] &&
11
11
  current_schema.schema['properties'] &&
12
12
  current_schema.schema['properties'][property] &&
@@ -6,11 +6,9 @@ module JSON
6
6
  attr_accessor :schema, :uri, :validator
7
7
 
8
8
  def initialize(schema,uri,parent_validator=nil)
9
- @schema = schema
9
+ @schema = self.class.stringify(schema)
10
10
  @uri = uri
11
11
 
12
- self.class.add_indifferent_access(@schema)
13
-
14
12
  # If there is an ID on this schema, use it to generate the URI
15
13
  if @schema['id'] && @schema['id'].kind_of?(String)
16
14
  temp_uri = URI.parse(@schema['id'])
@@ -36,26 +34,18 @@ module JSON
36
34
  @validator.validate(self, data, fragments, processor, options)
37
35
  end
38
36
 
39
- def self.add_indifferent_access(schema)
40
- if schema.is_a?(Hash)
41
- schema.default_proc = proc do |hash,key|
42
- if hash.has_key?(key)
43
- hash[key]
44
- else
45
- key = case key
46
- when Symbol then key.to_s
47
- when String then key.to_sym
48
- end
49
- hash.has_key?(key) ? hash[key] : nil
50
- end
51
- end
52
- schema.keys.each do |key|
53
- add_indifferent_access(schema[key])
54
- end
55
- elsif schema.is_a?(Array)
56
- schema.each do |schema_item|
57
- add_indifferent_access(schema_item)
37
+ def self.stringify(schema)
38
+ case schema
39
+ when Hash then
40
+ Hash[schema.map { |key, value| [key.to_s, stringify(schema[key])] }]
41
+ when Array then
42
+ schema.map do |schema_item|
43
+ stringify(schema_item)
58
44
  end
45
+ when Symbol then
46
+ schema.to_s
47
+ else
48
+ schema
59
49
  end
60
50
  end
61
51
 
@@ -45,6 +45,7 @@ module JSON
45
45
  @validation_options[:strict] = true if @options[:strict] == true
46
46
 
47
47
  @@mutex.synchronize { @base_schema = initialize_schema(schema_data) }
48
+ @original_data = data
48
49
  @data = initialize_data(data)
49
50
  @@mutex.synchronize { build_schemas(@base_schema) }
50
51
 
@@ -106,21 +107,18 @@ module JSON
106
107
 
107
108
  # Run a simple true/false validation of data against a schema
108
109
  def validate()
109
- begin
110
- @base_schema.validate(@data,[],self,@validation_options)
111
- if @validation_options[:clear_cache] == true
112
- Validator.clear_cache
113
- end
114
- if @options[:errors_as_objects]
115
- return @errors.map{|e| e.to_hash}
116
- else
117
- return @errors.map{|e| e.to_string}
118
- end
119
- rescue JSON::Schema::ValidationError
120
- if @validation_options[:clear_cache] == true
121
- Validator.clear_cache
122
- end
123
- raise $!
110
+ @base_schema.validate(@data,[],self,@validation_options)
111
+ if @options[:errors_as_objects]
112
+ return @errors.map{|e| e.to_hash}
113
+ else
114
+ return @errors.map{|e| e.to_string}
115
+ end
116
+ ensure
117
+ if @validation_options[:clear_cache] == true
118
+ Validator.clear_cache
119
+ end
120
+ if @validation_options[:insert_defaults]
121
+ JSON::Validator.merge_missing_values(@data, @original_data)
124
122
  end
125
123
  end
126
124
 
@@ -425,6 +423,25 @@ module JSON
425
423
  end
426
424
  end
427
425
 
426
+ def merge_missing_values(source, destination)
427
+ case destination
428
+ when Hash
429
+ source.each do |key, source_value|
430
+ destination_value = destination[key] || destination[key.to_sym]
431
+ if destination_value.nil?
432
+ destination[key] = source_value
433
+ else
434
+ merge_missing_values(source_value, destination_value)
435
+ end
436
+ end
437
+ when Array
438
+ source.each_with_index do |source_value, i|
439
+ destination_value = destination[i]
440
+ merge_missing_values(source_value, destination_value)
441
+ end
442
+ end
443
+ end
444
+
428
445
  if !defined?(MultiJson)
429
446
  if Gem::Specification::find_all_by_name('json').any?
430
447
  require 'json'
@@ -560,8 +577,7 @@ module JSON
560
577
  end
561
578
  end
562
579
  end
563
- JSON::Schema.add_indifferent_access(data)
564
- data
580
+ JSON::Schema.stringify(data)
565
581
  end
566
582
 
567
583
  def normalized_uri(data)
@@ -1210,11 +1210,12 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1210
1210
  }
1211
1211
  }
1212
1212
 
1213
- data = {"b" => 2}
1213
+ data = {:b => 2}
1214
1214
  assert(JSON::Validator.validate(schema,data))
1215
1215
  assert_nil(data["a"])
1216
1216
  assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
1217
1217
  assert_equal(42, data["a"])
1218
+ assert_equal(2, data[:b])
1218
1219
 
1219
1220
  schema = {
1220
1221
  "$schema" => "http://json-schema.org/draft-03/schema#",
@@ -1225,21 +1226,12 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1225
1226
  }
1226
1227
  }
1227
1228
 
1228
- data = {"b" => 2}
1229
+ data = {:b => 2}
1229
1230
  assert(!JSON::Validator.validate(schema,data))
1230
1231
  assert_nil(data["a"])
1231
1232
  assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
1232
1233
  assert_equal(42, data["a"])
1233
-
1234
- schema = {
1235
- "$schema" => "http://json-schema.org/draft-03/schema#",
1236
- "type" => "object",
1237
- "properties" => {
1238
- "a" => {"type" => "integer", "default" => 42, "required" => true},
1239
- "b" => {"type" => "integer"}
1240
- }
1241
- }
1242
-
1234
+ assert_equal(2, data[:b])
1243
1235
 
1244
1236
  schema = {
1245
1237
  "$schema" => "http://json-schema.org/draft-03/schema#",
@@ -1250,11 +1242,12 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1250
1242
  }
1251
1243
  }
1252
1244
 
1253
- data = {"b" => 2}
1245
+ data = {:b => 2}
1254
1246
  assert(!JSON::Validator.validate(schema,data))
1255
1247
  assert_nil(data["a"])
1256
1248
  assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1257
1249
  assert_nil(data["a"])
1250
+ assert_equal(2, data[:b])
1258
1251
 
1259
1252
  schema = {
1260
1253
  "$schema" => "http://json-schema.org/draft-03/schema#",
@@ -1265,11 +1258,12 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
1265
1258
  }
1266
1259
  }
1267
1260
 
1268
- data = {"b" => 2}
1261
+ data = {:b => 2}
1269
1262
  assert(JSON::Validator.validate(schema,data))
1270
1263
  assert_nil(data["a"])
1271
1264
  assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1272
1265
  assert_equal("42",data["a"])
1266
+ assert_equal(2, data[:b])
1273
1267
 
1274
1268
  end
1275
1269
 
@@ -1096,11 +1096,12 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
1096
1096
  }
1097
1097
  }
1098
1098
 
1099
- data = {"b" => 2}
1099
+ data = {:b => 2}
1100
1100
  assert(JSON::Validator.validate(schema,data))
1101
1101
  assert_nil(data["a"])
1102
1102
  assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
1103
1103
  assert_equal(42, data["a"])
1104
+ assert_equal(2, data[:b])
1104
1105
 
1105
1106
  schema = {
1106
1107
  "$schema" => "http://json-schema.org/draft-04/schema#",
@@ -1112,22 +1113,12 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
1112
1113
  }
1113
1114
  }
1114
1115
 
1115
- data = {"b" => 2}
1116
+ data = {:b => 2}
1116
1117
  assert(!JSON::Validator.validate(schema,data))
1117
1118
  assert_nil(data["a"])
1118
1119
  assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
1119
1120
  assert_equal(42, data["a"])
1120
-
1121
- schema = {
1122
- "$schema" => "http://json-schema.org/draft-04/schema#",
1123
- "type" => "object",
1124
- "required" => ["a"],
1125
- "properties" => {
1126
- "a" => {"type" => "integer", "default" => 42},
1127
- "b" => {"type" => "integer"}
1128
- }
1129
- }
1130
-
1121
+ assert_equal(2, data[:b])
1131
1122
 
1132
1123
  schema = {
1133
1124
  "$schema" => "http://json-schema.org/draft-04/schema#",
@@ -1139,11 +1130,12 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
1139
1130
  }
1140
1131
  }
1141
1132
 
1142
- data = {"b" => 2}
1133
+ data = {:b => 2}
1143
1134
  assert(!JSON::Validator.validate(schema,data))
1144
1135
  assert_nil(data["a"])
1145
1136
  assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1146
1137
  assert_nil(data["a"])
1138
+ assert_equal(2, data[:b])
1147
1139
 
1148
1140
  schema = {
1149
1141
  "$schema" => "http://json-schema.org/draft-04/schema#",
@@ -1154,11 +1146,12 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
1154
1146
  }
1155
1147
  }
1156
1148
 
1157
- data = {"b" => 2}
1149
+ data = {:b => 2}
1158
1150
  assert(JSON::Validator.validate(schema,data))
1159
1151
  assert_nil(data["a"])
1160
1152
  assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
1161
1153
  assert_equal("42",data["a"])
1154
+ assert_equal(2, data[:b])
1162
1155
 
1163
1156
  end
1164
1157
 
@@ -1318,7 +1311,6 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
1318
1311
 
1319
1312
  data = {"a" => 1}
1320
1313
  errors = JSON::Validator.fully_validate(schema,data)
1321
- puts errors
1322
1314
  assert_equal(0, errors.length)
1323
1315
 
1324
1316
  data = {"a" => "taco"}
@@ -0,0 +1,46 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class MergeMissingValuesTest < Test::Unit::TestCase
5
+ def test_merge_missing_values_for_string
6
+ original = 'foo'
7
+ updated = 'foo'
8
+ JSON::Validator.merge_missing_values(updated, original)
9
+ assert_equal('foo', original)
10
+ end
11
+
12
+ def test_merge_missing_values_for_empty_array
13
+ original = []
14
+ updated = []
15
+ JSON::Validator.merge_missing_values(updated, original)
16
+ assert_equal([], original)
17
+ end
18
+
19
+ def test_merge_missing_values_for_empty_hash
20
+ original = {}
21
+ updated = {}
22
+ JSON::Validator.merge_missing_values(updated, original)
23
+ assert_equal({}, original)
24
+ end
25
+
26
+ def test_merge_missing_values_for_new_values
27
+ original = {:hello => 'world'}
28
+ updated = {'hello' => 'world', 'foo' => 'bar'}
29
+ JSON::Validator.merge_missing_values(updated, original)
30
+ assert_equal({:hello => 'world', 'foo' => 'bar'}, original)
31
+ end
32
+
33
+ def test_merge_missing_values_for_nested_array
34
+ original = [:hello, 'world', 1, 2, 3, {:foo => :bar, 'baz' => 'qux'}]
35
+ updated = ['hello', 'world', 1, 2, 3, {'foo' => 'bar', 'baz' => 'qux', 'this_is' => 'new'}]
36
+ JSON::Validator.merge_missing_values(updated, original)
37
+ assert_equal([:hello, 'world', 1, 2, 3, {:foo => :bar, 'baz' => 'qux', 'this_is' => 'new'}], original)
38
+ end
39
+
40
+ def test_merge_missing_values_for_nested_hash
41
+ original = {:hello => 'world', :foo => ['bar', :baz, {:uno => {:due => 3}}]}
42
+ updated = {'hello' => 'world', 'foo' => ['bar', 'baz', {'uno' => {'due' => 3, 'this_is' => 'new'}}], 'ack' => 'sed'}
43
+ JSON::Validator.merge_missing_values(updated, original)
44
+ assert_equal({:hello => 'world', :foo => ['bar', :baz, {:uno => {:due => 3, 'this_is' => 'new'}}], 'ack' => 'sed'}, original)
45
+ end
46
+ end
@@ -47,6 +47,82 @@ class JSONSchemaValidation < Test::Unit::TestCase
47
47
  }
48
48
  end
49
49
 
50
+ def symbolized_schema
51
+ {
52
+ :type => :object,
53
+ :required => [
54
+ :id,
55
+ :name,
56
+ :real_name,
57
+ :role,
58
+ :website,
59
+ :biography,
60
+ :created_at,
61
+ :demographic
62
+ ],
63
+ :properties => {
64
+ :id => {
65
+ :type => [
66
+ :integer
67
+ ]
68
+ },
69
+ :name => {
70
+ :type => [
71
+ :string
72
+ ]
73
+ },
74
+ :real_name => {
75
+ :type => [
76
+ :string
77
+ ]
78
+ },
79
+ :role => {
80
+ :type => [
81
+ :string
82
+ ]
83
+ },
84
+ :website => {
85
+ :type => [
86
+ :string,
87
+ :null
88
+ ]
89
+ },
90
+ :created_at => {
91
+ :type => [
92
+ :string
93
+ ]
94
+ },
95
+ :biography => {
96
+ :type => [
97
+ :string,
98
+ :null
99
+ ]
100
+ }
101
+ },
102
+ :relationships => {
103
+ :demographic => {
104
+ :type => :object,
105
+ :required => [
106
+ :id,
107
+ :gender
108
+ ],
109
+ :properties => {
110
+ :id => {
111
+ :type => [
112
+ :integer
113
+ ]
114
+ },
115
+ :gender => {
116
+ :type => [
117
+ :string
118
+ ]
119
+ }
120
+ }
121
+ }
122
+ }
123
+ }
124
+ end
125
+
50
126
  def test_draft03_validation
51
127
  data = {"b" => {"a" => 5}}
52
128
  assert(JSON::Validator.validate(valid_schema_v3,data,:validate_schema => true, :version => :draft3))
@@ -93,4 +169,18 @@ class JSONSchemaValidation < Test::Unit::TestCase
93
169
  end
94
170
  end
95
171
  end
172
+
173
+ def test_validate_schema_with_symbol_keys
174
+ data = {
175
+ "created_at" => "2014-01-25T00:58:33-08:00",
176
+ "id" => 8517194300913402149003,
177
+ "name" => "chelsey",
178
+ "real_name" => "Mekhi Hegmann",
179
+ "website" => nil,
180
+ "role" => "user",
181
+ "biography" => nil,
182
+ "demographic" => nil
183
+ }
184
+ assert(JSON::Validator.validate!(symbolized_schema, data, :validate_schema => true))
185
+ end
96
186
  end
@@ -0,0 +1,49 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class StringifyTest < Test::Unit::TestCase
5
+ def test_stringify_on_hash
6
+ hash = {
7
+ :a => 'foo',
8
+ 'b' => :bar
9
+ }
10
+ assert_equal({'a' => 'foo', 'b' => 'bar'}, JSON::Schema.stringify(hash), 'symbol keys should be converted to strings')
11
+ end
12
+
13
+ def test_stringify_on_array
14
+ array = [
15
+ :a,
16
+ 'b'
17
+ ]
18
+ assert_equal(['a', 'b'], JSON::Schema.stringify(array), 'symbols in an array should be converted to strings')
19
+ end
20
+
21
+ def test_stringify_on_hash_of_arrays
22
+ hash = {
23
+ :a => [:foo],
24
+ 'b' => :bar
25
+ }
26
+ assert_equal({'a' => ['foo'], 'b' => 'bar'}, JSON::Schema.stringify(hash), 'symbols in a nested array should be converted to strings')
27
+ end
28
+
29
+ def test_stringify_on_array_of_hashes
30
+ array = [
31
+ :a,
32
+ {
33
+ :b => :bar
34
+ }
35
+ ]
36
+ assert_equal(['a', {'b' => 'bar'}], JSON::Schema.stringify(array), 'symbols keys in a nested hash should be converted to strings')
37
+ end
38
+
39
+ def test_stringify_on_hash_of_hashes
40
+ hash = {
41
+ :a => {
42
+ :b => {
43
+ :foo => :bar
44
+ }
45
+ }
46
+ }
47
+ assert_equal({'a' => {'b' => {'foo' => 'bar'} } }, JSON::Schema.stringify(hash), 'symbols in a nested hash of hashes should be converted to strings')
48
+ end
49
+ end
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.4.0
4
+ version: 2.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenny Hoxworth
@@ -71,7 +71,6 @@ files:
71
71
  - lib/json-schema/schema.rb
72
72
  - lib/json-schema/uri/file.rb
73
73
  - lib/json-schema/util/array_set.rb
74
- - lib/json-schema/util/hash.rb
75
74
  - lib/json-schema/util/uuid.rb
76
75
  - lib/json-schema/validator.rb
77
76
  - lib/json-schema/validators/draft1.rb
@@ -103,11 +102,13 @@ files:
103
102
  - test/test_jsonschema_draft3.rb
104
103
  - test/test_jsonschema_draft4.rb
105
104
  - test/test_list_option.rb
105
+ - test/test_merge_misisng_values.rb
106
106
  - test/test_minitems.rb
107
107
  - test/test_one_of.rb
108
108
  - test/test_ruby_schema.rb
109
109
  - test/test_schema_type_attribute.rb
110
110
  - test/test_schema_validation.rb
111
+ - test/test_stringify.rb
111
112
  - test/data/all_of_ref_data.json
112
113
  - test/data/any_of_ref_data.json
113
114
  - test/data/bad_data_1.json
@@ -173,11 +174,13 @@ test_files:
173
174
  - test/test_jsonschema_draft3.rb
174
175
  - test/test_jsonschema_draft4.rb
175
176
  - test/test_list_option.rb
177
+ - test/test_merge_misisng_values.rb
176
178
  - test/test_minitems.rb
177
179
  - test/test_one_of.rb
178
180
  - test/test_ruby_schema.rb
179
181
  - test/test_schema_type_attribute.rb
180
182
  - test/test_schema_validation.rb
183
+ - test/test_stringify.rb
181
184
  - test/data/all_of_ref_data.json
182
185
  - test/data/any_of_ref_data.json
183
186
  - test/data/bad_data_1.json
@@ -1,8 +0,0 @@
1
- class Hash
2
- if !{}.respond_to?("default_proc=")
3
- def default_proc=(blk)
4
- self.replace(Hash.new(&blk).merge(self))
5
- end
6
- end
7
- end
8
-