json-schema 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.0.3.gem
25
+ $ gem install json-schema-2.0.4.gem
26
26
  </pre>
27
27
 
28
28
 
@@ -56,13 +56,13 @@ module JSON
56
56
  end
57
57
  elsif !valid
58
58
  if union
59
- message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match one or more of the following types:"
59
+ message = "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} did not match one or more of the following types:"
60
60
  types.each {|type| message += type.is_a?(String) ? " #{type}," : " (schema)," }
61
61
  message.chop!
62
62
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
63
63
  validation_errors(processor).last.sub_errors = union_errors
64
64
  else
65
- message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match the following type:"
65
+ message = "The property '#{build_fragment(fragments)}' of type #{type_of_data(data)} did not match the following type:"
66
66
  types.each {|type| message += type.is_a?(String) ? " #{type}," : " (schema)," }
67
67
  message.chop!
68
68
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
@@ -85,6 +85,18 @@ module JSON
85
85
  valid_classes = TYPE_CLASS_MAPPINGS.fetch(type) { return true }
86
86
  Array(valid_classes).any? { |c| data.is_a?(c) }
87
87
  end
88
+
89
+ # Lookup Schema type of given class instance
90
+ def self.type_of_data(data)
91
+ type, klass = TYPE_CLASS_MAPPINGS.map { |k,v| [k,v] }.sort_by { |i|
92
+ k,v = i
93
+ -Array(v).map { |klass| klass.ancestors.size }.max
94
+ }.find { |i|
95
+ k,v = i
96
+ Array(v).any? { |klass| data.kind_of?(klass) }
97
+ }
98
+ type
99
+ end
88
100
  end
89
101
  end
90
- end
102
+ end
@@ -10,7 +10,7 @@ module JSON
10
10
  @uri = uri
11
11
 
12
12
  # If there is an ID on this schema, use it to generate the URI
13
- if @schema['id']
13
+ if @schema['id'] && @schema['id'].kind_of?(String)
14
14
  temp_uri = URI.parse(@schema['id'])
15
15
  if temp_uri.relative?
16
16
  uri = uri.merge(@schema['id'])
@@ -0,0 +1,21 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class TestSchemaTypeAttribute < Test::Unit::TestCase
5
+ def test_type_of_data
6
+ assert_equal(type_of_data(String.new), 'string')
7
+ assert_equal(type_of_data(Numeric.new), 'number')
8
+ assert_equal(type_of_data(1), 'integer')
9
+ assert_equal(type_of_data(true), 'boolean')
10
+ assert_equal(type_of_data(false), 'boolean')
11
+ assert_equal(type_of_data(Hash.new), 'object')
12
+ assert_equal(type_of_data(nil), 'null')
13
+ assert_equal(type_of_data(Object.new), 'any')
14
+ end
15
+
16
+ private
17
+
18
+ def type_of_data(data)
19
+ JSON::Schema::TypeAttribute.type_of_data(data)
20
+ end
21
+ 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.0.3
4
+ version: 2.0.4
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-06-26 00:00:00.000000000 Z
12
+ date: 2013-07-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: hoxworth@gmail.com
@@ -78,6 +78,7 @@ files:
78
78
  - test/test_jsonschema_draft2.rb
79
79
  - test/test_jsonschema_draft3.rb
80
80
  - test/test_jsonschema_draft4.rb
81
+ - test/test_schema_type_attribute.rb
81
82
  - test/test_schema_validation.rb
82
83
  - test/data/bad_data_1.json
83
84
  - test/data/good_data_1.json
@@ -123,6 +124,7 @@ test_files:
123
124
  - test/test_jsonschema_draft2.rb
124
125
  - test/test_jsonschema_draft3.rb
125
126
  - test/test_jsonschema_draft4.rb
127
+ - test/test_schema_type_attribute.rb
126
128
  - test/test_schema_validation.rb
127
129
  - test/data/bad_data_1.json
128
130
  - test/data/good_data_1.json