json-schema 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,7 +18,7 @@ From the git repo:
18
18
 
19
19
  <pre>
20
20
  $ gem build json-schema.gemspec
21
- $ gem install json-schema-1.0.7.gem
21
+ $ gem install json-schema-1.0.8.gem
22
22
  </pre>
23
23
 
24
24
 
@@ -21,26 +21,7 @@ module JSON
21
21
 
22
22
  types.each do |type|
23
23
  if type.is_a?(String)
24
- case type
25
- when "string"
26
- valid = data.is_a?(String)
27
- when "number"
28
- valid = data.is_a?(Numeric)
29
- when "integer"
30
- valid = data.is_a?(Integer)
31
- when "boolean"
32
- valid = (data.is_a?(TrueClass) || data.is_a?(FalseClass))
33
- when "object"
34
- valid = data.is_a?(Hash)
35
- when "array"
36
- valid = data.is_a?(Array)
37
- when "null"
38
- valid = data.is_a?(NilClass)
39
- when "any"
40
- valid = true
41
- else
42
- valid = true
43
- end
24
+ valid = data_valid_for_type?(data, type)
44
25
  elsif type.is_a?(Hash) && union
45
26
  # Validate as a schema
46
27
  schema = JSON::Schema.new(type,current_schema.uri,validator)
@@ -89,6 +70,22 @@ module JSON
89
70
  end
90
71
  end
91
72
  end
73
+
74
+ TYPE_CLASS_MAPPINGS = {
75
+ "string" => String,
76
+ "number" => Numeric,
77
+ "integer" => Integer,
78
+ "boolean" => [TrueClass, FalseClass],
79
+ "object" => Hash,
80
+ "array" => Array,
81
+ "null" => NilClass,
82
+ "any" => Object
83
+ }
84
+
85
+ def self.data_valid_for_type?(data, type)
86
+ valid_classes = TYPE_CLASS_MAPPINGS.fetch(type) { return true }
87
+ Array(valid_classes).any? { |c| data.is_a?(c) }
88
+ end
92
89
  end
93
90
  end
94
91
  end
@@ -107,7 +107,7 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
107
107
  data['a'] = true
108
108
  assert(!JSON::Validator.validate(schema,data))
109
109
 
110
- assert(JSON::Validator.validate({'type' => 'objec'}, {'a' => true}))
110
+ assert(JSON::Validator.validate({'type' => 'object'}, {'a' => true}))
111
111
  assert(JSON::Validator.validate({'type' => 'object'}, {}))
112
112
  assert(!JSON::Validator.validate({'type' => 'object'}, []))
113
113
  assert(!JSON::Validator.validate({'type' => 'object'}, 3))
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: 1.0.7
4
+ version: 1.0.8
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: 2012-05-29 00:00:00.000000000 Z
12
+ date: 2012-06-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: hoxworth@gmail.com