json-schema 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.textile CHANGED
@@ -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.1.gem
25
+ $ gem install json-schema-2.1.2.gem
26
26
  </pre>
27
27
 
28
28
 
@@ -4,15 +4,15 @@ module JSON
4
4
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
5
5
  if data.is_a?(Hash)
6
6
  current_schema.schema['dependencies'].each do |property,dependency_value|
7
- if data.has_key?(property)
7
+ if data.has_key?(property.to_s) || data.has_key?(property.to_sym)
8
8
  if dependency_value.is_a?(String)
9
- if !data.has_key?(dependency_value)
9
+ if !data.has_key?(dependency_value.to_s) && !data.has_key?(dependency_value.to_sym)
10
10
  message = "The property '#{build_fragment(fragments)}' has a property '#{property}' that depends on a missing property '#{dependency_value}'"
11
11
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
12
12
  end
13
13
  elsif dependency_value.is_a?(Array)
14
14
  dependency_value.each do |value|
15
- if !data.has_key?(value)
15
+ if !data.has_key?(value.to_s) && !data.has_key?(value.to_sym)
16
16
  message = "The property '#{build_fragment(fragments)}' has a property '#{property}' that depends on a missing property '#{value}'"
17
17
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
18
18
  end
@@ -4,9 +4,9 @@ module JSON
4
4
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
5
5
  if data.is_a?(Hash)
6
6
  current_schema.schema['dependencies'].each do |property,dependency_value|
7
- if data.has_key?(property) && dependency_value.is_a?(Array)
7
+ if (data.has_key?(property.to_s) || data.has_key?(property.to_sym)) && dependency_value.is_a?(Array)
8
8
  dependency_value.each do |value|
9
- if !data.has_key?(value)
9
+ if !data.has_key?(value.to_s)
10
10
  message = "The property '#{build_fragment(fragments)}' has a property '#{property}' that depends on a missing property '#{value}'"
11
11
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
12
12
  end
@@ -4,17 +4,21 @@ module JSON
4
4
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
5
5
  if data.is_a?(Hash)
6
6
  current_schema.schema['properties'].each do |property,property_schema|
7
- if !data.has_key?(property) and property_schema['default'] and !property_schema['readonly'] and options[:insert_defaults]
7
+ if !data.has_key?(property.to_s) &&
8
+ !data.has_key?(property.to_sym) &&
9
+ property_schema['default'] &&
10
+ !property_schema['readonly'] &&
11
+ options[:insert_defaults]
8
12
  default = property_schema['default']
9
13
  data[property] = (default.is_a?(Hash) ? default.clone : default)
10
14
  end
11
15
 
12
- if property_schema['required'] and !data.has_key?(property)
16
+ if property_schema['required'] && !data.has_key?(property.to_s)
13
17
  message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
14
18
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
15
19
  end
16
20
 
17
- if data.has_key?(property)
21
+ if data.has_key?(property.to_s) || data.has_key?(property.to_sym)
18
22
  schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
19
23
  fragments << property
20
24
  schema.validate(data[property],fragments,processor,options)
@@ -4,12 +4,12 @@ module JSON
4
4
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
5
5
  if data.is_a?(Hash)
6
6
  current_schema.schema['properties'].each do |property,property_schema|
7
- if ((property_schema['optional'].nil? || property_schema['optional'] == false) && !data.has_key?(property))
7
+ if ((property_schema['optional'].nil? || property_schema['optional'] == false) && !data.has_key?(property.to_s) && !data.has_key?(property.to_sym))
8
8
  message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
9
9
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
10
10
  end
11
-
12
- if data.has_key?(property)
11
+
12
+ if data.has_key?(property.to_s) || data.has_key?(property.to_sym)
13
13
  schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
14
14
  fragments << property
15
15
  schema.validate(data[property],fragments,processor,options)
@@ -4,12 +4,16 @@ module JSON
4
4
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
5
5
  if data.is_a?(Hash)
6
6
  current_schema.schema['properties'].each do |property,property_schema|
7
- if !data.has_key?(property) and property_schema['default'] and !property_schema['readonly'] and options[:insert_defaults]
7
+ if !data.has_key?(property.to_s) &&
8
+ !data.has_key?(property.to_sym) &&
9
+ property_schema['default'] &&
10
+ !property_schema['readonly'] &&
11
+ options[:insert_defaults]
8
12
  default = property_schema['default']
9
13
  data[property] = (default.is_a?(Hash) ? default.clone : default)
10
14
  end
11
15
 
12
- if data.has_key?(property)
16
+ if data.has_key?(property.to_s)
13
17
  schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
14
18
  fragments << property
15
19
  schema.validate(data[property],fragments,processor,options)
@@ -4,7 +4,7 @@ module JSON
4
4
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
5
5
  if data.is_a?(Hash)
6
6
  current_schema.schema['required'].each do |property,property_schema|
7
- if !data.has_key?(property)
7
+ if !data.has_key?(property.to_s) && !data.has_key?(property.to_sym)
8
8
  prop_defaults = options[:insert_defaults] &&
9
9
  current_schema.schema['properties'] &&
10
10
  current_schema.schema['properties'][property] &&
@@ -9,6 +9,8 @@ module JSON
9
9
  @schema = schema
10
10
  @uri = uri
11
11
 
12
+ self.class.add_indifferent_access(@schema)
13
+
12
14
  # If there is an ID on this schema, use it to generate the URI
13
15
  if @schema['id'] && @schema['id'].kind_of?(String)
14
16
  temp_uri = URI.parse(@schema['id'])
@@ -38,6 +40,25 @@ module JSON
38
40
  @validator.validate(self, data, fragments, processor, options)
39
41
  end
40
42
 
43
+ def self.add_indifferent_access(schema)
44
+ if schema.is_a?(Hash)
45
+ schema.default_proc = proc do |hash,key|
46
+ if hash.has_key?(key)
47
+ hash[key]
48
+ else
49
+ key = case key
50
+ when Symbol then key.to_s
51
+ when String then key.to_sym
52
+ end
53
+ hash.has_key?(key) ? hash[key] : nil
54
+ end
55
+ end
56
+ schema.keys.each do |key|
57
+ add_indifferent_access(schema[key])
58
+ end
59
+ end
60
+ end
61
+
41
62
  def base_uri
42
63
  parts = @uri.to_s.split('/')
43
64
  parts.pop
@@ -636,6 +636,7 @@ module JSON
636
636
  end
637
637
  end
638
638
  end
639
+ JSON::Schema.add_indifferent_access(data)
639
640
  data
640
641
  end
641
642
 
@@ -0,0 +1,38 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class RubySchemaTest < Test::Unit::TestCase
5
+ def test_string_keys
6
+ schema = {
7
+ "type" => 'object',
8
+ "required" => ["a"],
9
+ "properties" => {
10
+ "a" => {"type" => "integer", "default" => 42},
11
+ "b" => {"type" => "integer"}
12
+ }
13
+ }
14
+
15
+ data = {
16
+ "a" => 5
17
+ }
18
+
19
+ assert(JSON::Validator.validate(schema, data))
20
+ end
21
+
22
+ def test_symbol_keys
23
+ schema = {
24
+ type: 'object',
25
+ required: ["a"],
26
+ properties: {
27
+ a: {type: "integer", default: 42},
28
+ b: {type: "integer"}
29
+ }
30
+ }
31
+
32
+ data = {
33
+ a: 5
34
+ }
35
+
36
+ assert(JSON::Validator.validate(schema, data))
37
+ end
38
+ 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.1.1
4
+ version: 2.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-03 00:00:00.000000000 Z
12
+ date: 2013-07-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: hoxworth@gmail.com
@@ -79,6 +79,7 @@ files:
79
79
  - test/test_jsonschema_draft2.rb
80
80
  - test/test_jsonschema_draft3.rb
81
81
  - test/test_jsonschema_draft4.rb
82
+ - test/test_ruby_schema.rb
82
83
  - test/test_schema_type_attribute.rb
83
84
  - test/test_schema_validation.rb
84
85
  - test/data/bad_data_1.json
@@ -112,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
113
  version: '0'
113
114
  requirements: []
114
115
  rubyforge_project:
115
- rubygems_version: 1.8.25
116
+ rubygems_version: 1.8.24
116
117
  signing_key:
117
118
  specification_version: 3
118
119
  summary: Ruby JSON Schema Validator
@@ -126,6 +127,7 @@ test_files:
126
127
  - test/test_jsonschema_draft2.rb
127
128
  - test/test_jsonschema_draft3.rb
128
129
  - test/test_jsonschema_draft4.rb
130
+ - test/test_ruby_schema.rb
129
131
  - test/test_schema_type_attribute.rb
130
132
  - test/test_schema_validation.rb
131
133
  - test/data/bad_data_1.json