json-schema 2.1.4 → 2.1.5

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.1.4.gem
25
+ $ gem install json-schema-2.1.5.gem
26
26
  </pre>
27
27
 
28
28
 
@@ -8,7 +8,7 @@ if begin
8
8
  Gem.available?('multi_json')
9
9
  end
10
10
  require 'multi_json'
11
-
11
+
12
12
  # Force MultiJson to load an engine before we define the JSON constant here; otherwise,
13
13
  # it looks for things that are under the JSON namespace that aren't there (since we have defined it here)
14
14
  MultiJson.respond_to?(:adapter) ? MultiJson.adapter : MultiJson.engine
@@ -17,6 +17,7 @@ end
17
17
  $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/json-schema"
18
18
 
19
19
  require 'rubygems'
20
+ require 'util/hash'
20
21
  require 'schema'
21
22
  require 'validator'
22
23
  Dir[File.join(File.dirname(__FILE__), "json-schema/attributes/*.rb")].each {|file| require file }
@@ -4,7 +4,7 @@ module JSON
4
4
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
5
5
  if !current_schema.schema['enum'].include?(data)
6
6
  message = "The property '#{build_fragment(fragments)}' value #{data.inspect} did not match one of the following values:"
7
- current_schema.schema['enum'].each {|val|
7
+ current_schema.schema['enum'].each_key {|val|
8
8
  if val.is_a?(NilClass)
9
9
  message += " null,"
10
10
  elsif val.is_a?(Array)
@@ -5,12 +5,18 @@ module JSON
5
5
 
6
6
  schema = JSON::Schema.new(current_schema.schema['not'],current_schema.uri,validator)
7
7
  failed = true
8
+ errors_copy = processor.validation_errors.clone
8
9
  begin
9
10
  schema.validate(data,fragments,processor,options)
10
- message = "The property '#{build_fragment(fragments)}' of type #{data.class} matched the disallowed schema"
11
- failed = false
11
+ # If we're recording errors, we don't throw an exception. Instead, check the errors array length
12
+ if options[:record_errors] && errors_copy.length != processor.validation_errors.length
13
+ processor.validation_errors.replace(errors_copy)
14
+ else
15
+ message = "The property '#{build_fragment(fragments)}' of type #{data.class} matched the disallowed schema"
16
+ failed = false
17
+ end
12
18
  rescue
13
- # Yay, we failed validation
19
+ # Yay, we failed validation.
14
20
  end
15
21
 
16
22
  unless failed
@@ -0,0 +1,8 @@
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
+
@@ -5,6 +5,7 @@ require 'bigdecimal'
5
5
  require 'digest/sha1'
6
6
  require 'date'
7
7
  require 'thread'
8
+ require 'yaml'
8
9
 
9
10
  module JSON
10
11
 
@@ -336,6 +337,15 @@ module JSON
336
337
  end
337
338
  end
338
339
 
340
+ # Convert enum to a hash
341
+ if parent_schema.schema["enum"] && parent_schema.schema["enum"].is_a?(Array)
342
+ enum_hash = {}
343
+ parent_schema.schema["enum"].each do |item|
344
+ enum_hash[item] = true
345
+ end
346
+ parent_schema.schema["enum"] = enum_hash
347
+ end
348
+
339
349
  # Each of these might be schemas
340
350
  ["additionalProperties", "additionalItems", "dependencies", "extends"].each do |key|
341
351
  if parent_schema.schema[key].is_a?(Hash)
@@ -527,7 +537,9 @@ module JSON
527
537
  if @@json_backend == 'yajl'
528
538
  @@serializer = lambda{|o| Yajl::Encoder.encode(o) }
529
539
  else
530
- @@serializer = lambda{|o| Marshal.dump(o) }
540
+ @@serializer = lambda{|o|
541
+ YAML.dump(o)
542
+ }
531
543
  end
532
544
  end
533
545
  end
@@ -1,4 +1,6 @@
1
- {"type": "object",
2
- "properties" : {
3
- "name" : { "type": "integer" }
4
- }
1
+ {
2
+ "type": "object",
3
+ "properties" : {
4
+ "name" : { "type": "integer" }
5
+ }
6
+ }
@@ -1203,6 +1203,24 @@ class JSONSchemaDraft4Test < Test::Unit::TestCase
1203
1203
  assert(JSON::Validator.validate(schema,data))
1204
1204
  end
1205
1205
 
1206
+ def test_not_fully_validate
1207
+ # Start with a simple not
1208
+ schema = {
1209
+ "$schema" => "http://json-schema.org/draft-04/schema#",
1210
+ "properties" => {
1211
+ "a" => {"not" => { "type" => ["string", "boolean"]}}
1212
+ }
1213
+ }
1214
+
1215
+ data = {"a" => 1}
1216
+ errors = JSON::Validator.fully_validate(schema,data)
1217
+ puts errors
1218
+ assert_equal(0, errors.length)
1219
+
1220
+ data = {"a" => "taco"}
1221
+ errors = JSON::Validator.fully_validate(schema,data)
1222
+ assert_equal(1, errors.length)
1223
+ end
1206
1224
 
1207
1225
  def test_definitions
1208
1226
  schema = {
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.4
4
+ version: 2.1.5
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Kenny Hoxworth
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-12-31 00:00:00.000000000 Z
12
+ date: 2014-01-01 00:00:00.000000000 Z
12
13
  dependencies: []
13
14
  description:
14
15
  email: hoxworth@gmail.com
@@ -57,6 +58,7 @@ files:
57
58
  - lib/json-schema/schema.rb
58
59
  - lib/json-schema/uri/file.rb
59
60
  - lib/json-schema/uri/uuid.rb
61
+ - lib/json-schema/util/hash.rb
60
62
  - lib/json-schema/validator.rb
61
63
  - lib/json-schema/validators/draft1.rb
62
64
  - lib/json-schema/validators/draft2.rb
@@ -110,26 +112,27 @@ files:
110
112
  homepage: http://github.com/hoxworth/json-schema/tree/master
111
113
  licenses:
112
114
  - MIT
113
- metadata: {}
114
115
  post_install_message:
115
116
  rdoc_options: []
116
117
  require_paths:
117
118
  - lib
118
119
  required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
119
121
  requirements:
120
122
  - - ! '>='
121
123
  - !ruby/object:Gem::Version
122
124
  version: '0'
123
125
  required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
124
127
  requirements:
125
128
  - - ! '>='
126
129
  - !ruby/object:Gem::Version
127
130
  version: '0'
128
131
  requirements: []
129
132
  rubyforge_project:
130
- rubygems_version: 2.0.7
133
+ rubygems_version: 1.8.23
131
134
  signing_key:
132
- specification_version: 4
135
+ specification_version: 3
133
136
  summary: Ruby JSON Schema Validator
134
137
  test_files:
135
138
  - test/test_all_of_ref_schema.rb
checksums.yaml DELETED
@@ -1,15 +0,0 @@
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=