json-schema 2.6.1 → 2.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12af76aabf22ca3fd21620ba445eec49e02bb148
4
- data.tar.gz: e337dd929041aa42efbda91fd360caf0cb854035
3
+ metadata.gz: 59384ba05ea450b7add4dd65758c55a82615ce5a
4
+ data.tar.gz: 45388f1bcb2c6a4e9dd9a4479d3cd76627929b65
5
5
  SHA512:
6
- metadata.gz: aa0037193fa2bb35abc47c05d51ad54f2d7c6f185e04ecd16332ca3b1749e67439429a69a1dbf73c15de1aa31fa21c603ea5d626c4fd6d788d3a64d4e14fc75d
7
- data.tar.gz: a73e2d48d350b12c6a2b2108027845c9063de99a61ace81b7a0120cb22f55a39b769cf90b11c91fa58fed780215f6b6e66eb8c0a9b8311806fa86f2dd51b7b2a
6
+ metadata.gz: c27a0a2cec5b072cc964108ae4b331ff7320b906e55d20701eb556d296080cb348e4613d6ae0719d945bc1bafe73cb298c2f9448549b0c612301224bd2dbe2c9
7
+ data.tar.gz: 135eea52b5d08f17a16a98cbda09136b3846bed948cc3c2ef39b37143ac2c29acb5337a18ccfa609a602fbe01b61e133463d8ab4de663bb3b1c17cc3797af76d
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- ![https://travis-ci.org/ruby-json-schema/json-schema](https://travis-ci.org/ruby-json-schema/json-schema.svg?branch=master)
2
- ![https://codeclimate.com/github/ruby-json-schema/json-schema](https://codeclimate.com/github/ruby-json-schema/json-schema/badges/gpa.svg)
1
+ [![Travis](https://travis-ci.org/ruby-json-schema/json-schema.svg?branch=master)](https://travis-ci.org/ruby-json-schema/json-schema)
2
+ [![Code Climate](https://codeclimate.com/github/ruby-json-schema/json-schema/badges/gpa.svg)](https://codeclimate.com/github/ruby-json-schema/json-schema)
3
3
 
4
4
  Ruby JSON Schema Validator
5
5
  ==========================
@@ -42,10 +42,10 @@ $ gem build json-schema.gemspec
42
42
  $ gem install json-schema-2.5.2.gem
43
43
  ```
44
44
 
45
- Usage
45
+ Validation
46
46
  -----
47
47
 
48
- Three base validation methods exist:
48
+ Three base validation methods exist:
49
49
 
50
50
  1. `validate`: returns a boolean on whether a validation attempt passes
51
51
  2. `validate!`: throws a `JSON::Schema::ValidationError` with an appropriate message/trace on where the validation failed
@@ -67,15 +67,15 @@ attribute in the schema and referencing the appropriate specification URI. Note
67
67
  that the `$schema` attribute takes precedence over the `:version` option during
68
68
  parsing and validation.
69
69
 
70
- ### Validate Ruby objects against a Ruby schema
71
-
72
70
  For further information on json schema itself refer to <a
73
71
  href="http://spacetelescope.github.io/understanding-json-schema/">Understanding
74
72
  JSON Schema</a>.
75
73
 
76
- ```rb
77
- require 'rubygems'
78
- require 'json-schema'
74
+ Basic Usage
75
+ --------------
76
+
77
+ ```ruby
78
+ require "json-schema"
79
79
 
80
80
  schema = {
81
81
  "type" => "object",
@@ -85,279 +85,188 @@ schema = {
85
85
  }
86
86
  }
87
87
 
88
- data = {
89
- "a" => 5
90
- }
91
-
92
- JSON::Validator.validate(schema, data)
93
- ```
94
-
95
- ### Validate a JSON string against a JSON schema file
88
+ #
89
+ # validate ruby objects against a ruby schema
90
+ #
96
91
 
97
- ```rb
98
- require 'rubygems'
99
- require 'json-schema'
92
+ # => true
93
+ JSON::Validator.validate(schema, { "a" => 5 })
94
+ # => false
95
+ JSON::Validator.validate(schema, {})
100
96
 
101
- JSON::Validator.validate('schema.json', '{"a" : 5}')
102
- ```
97
+ #
98
+ # validate a json string against a json schema file
99
+ #
103
100
 
104
- ### Validate a list of objects against a schema that represents the individual objects
101
+ require "json"
102
+ File.write("schema.json", JSON.dump(schema))
105
103
 
106
- ```rb
107
- require 'rubygems'
108
- require 'json-schema'
109
-
110
- data = ['user','user','user']
111
- JSON::Validator.validate('user.json', data, :list => true)
112
- ```
113
-
114
- ### Strictly validate an object's properties
115
-
116
- With the `:strict` option, validation fails when an object contains properties
117
- that are not defined in the schema's property list or doesn't match the
118
- `additionalProperties` property. Furthermore, all properties are treated as
119
- `required` regardless of `required` properties set in the schema.
120
-
121
- ```rb
122
- require 'rubygems'
123
- require 'json-schema'
124
-
125
- schema = {
126
- "type" => "object",
127
- "properties" => {
128
- "a" => {"type" => "integer"},
129
- "b" => {"type" => "integer"}
130
- }
131
- }
132
-
133
- JSON::Validator.validate(schema, {"a" => 1, "b" => 2}, :strict => true) # ==> true
134
- JSON::Validator.validate(schema, {"a" => 1, "b" => 2, "c" => 3}, :strict => true) # ==> false
135
- JSON::Validator.validate(schema, {"a" => 1}, :strict => true) # ==> false
136
- ```
137
-
138
- ### Catch a validation error and print it out
139
-
140
- ```rb
141
- require 'rubygems'
142
- require 'json-schema'
143
-
144
- schema = {
145
- "type" => "object",
146
- "required" => ["a"],
147
- "properties" => {
148
- "a" => {"type" => "integer"}
149
- }
150
- }
104
+ # => true
105
+ JSON::Validator.validate('schema.json', '{ "a": 5 }')
151
106
 
152
- data = {
153
- "a" => "taco"
154
- }
107
+ #
108
+ # raise an error when validation fails
109
+ #
155
110
 
111
+ # => "The property '#/a' of type String did not match the following type: integer"
156
112
  begin
157
- JSON::Validator.validate!(schema, data)
158
- rescue JSON::Schema::ValidationError
159
- puts $!.message
113
+ JSON::Validator.validate!(schema, { "a" => "taco" })
114
+ rescue JSON::Schema::ValidationError => e
115
+ e.message
160
116
  end
161
- ```
162
-
163
- ### Fully validate against a schema and catch all errors
164
-
165
- ```rb
166
- require 'rubygems'
167
- require 'json-schema'
168
-
169
- schema = {
170
- "type" => "object",
171
- "required" => ["a","b"],
172
- "properties" => {
173
- "a" => {"type" => "integer"},
174
- "b" => {"type" => "string"}
175
- }
176
- }
177
-
178
- data = {
179
- "a" => "taco"
180
- }
181
-
182
- errors = JSON::Validator.fully_validate(schema, data)
183
-
184
- # ["The property '#/a' of type String did not match the following type: integer in schema 03179a21-197e-5414-9611-e9f63e8324cd#", "The property '#/' did not contain a required property of 'b' in schema 03179a21-197e-5414-9611-e9f63e8324cd#"]
185
- ```
186
-
187
- ### Fully validate against a schema and catch all errors as objects
188
-
189
- ```rb
190
- require 'rubygems'
191
- require 'json-schema'
192
-
193
- schema = {
194
- "type" => "object",
195
- "required" => ["a","b"],
196
- "properties" => {
197
- "a" => {"type" => "integer"},
198
- "b" => {"type" => "string"}
199
- }
200
- }
201
-
202
- data = {
203
- "a" => "taco"
204
- }
205
117
 
206
- errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
118
+ #
119
+ # return an array of error messages when validation fails
120
+ #
207
121
 
208
- # [{:message=>"The property '#/a' of type String did not match the following type: integer in schema 03179a21-197e-5414-9611-e9f63e8324cd#", :schema=>#, :failed_attribute=>"Type", :fragment=>"#/a"}, {:message=>"The property '#/' did not contain a required property of 'b' in schema 03179a21-197e-5414-9611-e9f63e8324cd#", :schema=>#, :failed_attribute=>"Properties", :fragment=>"#/"}]
122
+ # => ["The property '#/a' of type String did not match the following type: integer in schema 18a1ffbb-4681-5b00-bd15-2c76aee4b28f"]
123
+ JSON::Validator.fully_validate(schema, { "a" => "taco" })
209
124
  ```
210
125
 
211
- ### Validate against a fragment of a supplied schema
126
+ Advanced Options
127
+ -----------------
212
128
 
213
- ```rb
214
- require 'rubygems'
215
- require 'json-schema'
129
+ ```ruby
130
+ require "json-schema"
216
131
 
217
132
  schema = {
218
- "type" => "object",
219
- "required" => ["a","b"],
133
+ "type"=>"object",
134
+ "required" => ["a"],
220
135
  "properties" => {
221
- "a" => {"type" => "integer"},
222
- "b" => {"type" => "string"},
223
- "c" => {
136
+ "a" => {
137
+ "type" => "integer",
138
+ "default" => 42
139
+ },
140
+ "b" => {
224
141
  "type" => "object",
225
142
  "properties" => {
226
- "z" => {"type" => "integer"}
143
+ "x" => {
144
+ "type" => "integer"
145
+ }
227
146
  }
228
147
  }
229
148
  }
230
149
  }
231
150
 
232
- data = {
233
- "z" => 1
234
- }
235
-
236
- JSON::Validator.validate(schema, data, :fragment => "#/properties/c")
237
- ```
238
-
239
- ### Validate a JSON object against a JSON schema object, while also validating the schema itself
240
-
241
- ```rb
242
- require 'rubygems'
243
- require 'json-schema'
244
-
245
- schema = {
246
- "type" => "object",
247
- "required" => ["a"],
248
- "properties" => {
249
- "a" => {"type" => "integer", "required" => "true"} # This will fail schema validation!
250
- }
251
- }
252
-
253
- data = {
254
- "a" => 5
255
- }
256
-
257
- JSON::Validator.validate(schema, data, :validate_schema => true)
258
- ```
259
-
260
- ### Validate a JSON object against a JSON schema object, while inserting default values from the schema
261
-
262
- With the `:insert_defaults` option set to true any missing property that has a
263
- default value specified in the schema will be inserted into the validated data.
264
- The inserted default value is validated hence catching a schema that specifies
265
- an invalid default value.
266
-
267
- ```rb
268
- require 'rubygems'
269
- require 'json-schema'
270
-
271
- schema = {
272
- "type" => "object",
273
- "required" => ["a"],
274
- "properties" => {
275
- "a" => {"type" => "integer", "default" => 42},
276
- "b" => {"type" => "integer"}
277
- }
278
- }
279
-
280
- # Would not normally validate because "a" is missing and required by schema,
281
- # but "default" option allows insertion of valid default.
282
- data = {
283
- "b" => 5
284
- }
285
-
286
- JSON::Validator.validate(schema, data)
287
- # false
288
-
289
- JSON::Validator.validate(schema, data, :insert_defaults => true)
290
- # true
291
- # data = {
292
- # "a" => 42,
293
- # "b" => 5
294
- # }
295
- ```
296
-
297
- ### Validate an object against a JSON Schema Draft 2 schema
298
-
299
- ```rb
300
- require 'rubygems'
301
- require 'json-schema'
302
-
303
- schema = {
151
+ #
152
+ # with the `:list` option, a list can be validated against a schema that represents the individual objects
153
+ #
154
+
155
+ # => true
156
+ JSON::Validator.validate(schema, [{"a" => 1}, {"a" => 2}, {"a" => 3}], :list => true)
157
+ # => false
158
+ JSON::Validator.validate(schema, [{"a" => 1}, {"a" => 2}, {"a" => 3}])
159
+
160
+ #
161
+ # with the `:errors_as_objects` option, `#fully_validate` returns errors as hashes instead of strings
162
+ #
163
+
164
+ # => [{:schema=>#<Addressable::URI:0x3ffa69cbeed8 URI:18a1ffbb-4681-5b00-bd15-2c76aee4b28f>, :fragment=>"#/a", :message=>"The property '#/a' of type String did not match the following type: integer in schema 18a1ffbb-4681-5b00-bd15-2c76aee4b28f", :failed_attribute=>"TypeV4"}]
165
+ JSON::Validator.fully_validate(schema, { "a" => "taco" }, :errors_as_objects => true)
166
+
167
+ #
168
+ # with the `:strict` option, all properties are condisidered to have `"required": true` and all objects `"additionalProperties": false`
169
+ #
170
+
171
+ # => true
172
+ JSON::Validator.validate(schema, { "a" => 1, "b" => { "x" => 2 } }, :strict => true)
173
+ # => false
174
+ JSON::Validator.validate(schema, { "a" => 1, "b" => { "x" => 2 }, "c" => 3 }, :strict => true)
175
+ # => false
176
+ JSON::Validator.validate(schema, { "a" => 1 }, :strict => true)
177
+
178
+ #
179
+ # with the `:fragment` option, only a fragment of the schema is used for validation
180
+ #
181
+
182
+ # => true
183
+ JSON::Validator.validate(schema, { "x" => 1 }, :fragment => "#/properties/b")
184
+ # => false
185
+ JSON::Validator.validate(schema, { "x" => 1 })
186
+
187
+ #
188
+ # with the `:validate_schema` option, the schema is validated (against the json schema spec) before the json is validated (against the specified schema)
189
+ #
190
+
191
+ # => true
192
+ JSON::Validator.validate(schema, { "a" => 1 }, :validate_schema => true)
193
+ # => false
194
+ JSON::Validator.validate({ "required" => true }, { "a" => 1 }, :validate_schema => true)
195
+
196
+ #
197
+ # with the `:insert_defaults` option, any undefined values in the json that have a default in the schema are replaced with the default before validation
198
+ #
199
+
200
+ # => true
201
+ JSON::Validator.validate(schema, {}, :insert_defaults => true)
202
+ # => false
203
+ JSON::Validator.validate(schema, {})
204
+
205
+ #
206
+ # with the `:version` option, schemas conforming to older drafts of the json schema spec can be used
207
+ #
208
+
209
+ v2_schema = {
304
210
  "type" => "object",
305
211
  "properties" => {
306
- "a" => {"type" => "integer", "optional" => true}
212
+ "a" => {
213
+ "type" => "integer"
214
+ }
307
215
  }
308
216
  }
309
217
 
310
- data = {
311
- "a" => 5
312
- }
313
-
314
- JSON::Validator.validate(schema, data, :version => :draft2)
315
- ```
316
-
317
- ### Explicitly specifying the type of the data
218
+ # => false
219
+ JSON::Validator.validate(v2_schema, {}, :version => :draft2)
220
+ # => true
221
+ JSON::Validator.validate(v2_schema, {})
318
222
 
319
- By default, json-schema accepts a variety of different types for the data
320
- parameter, and it will try to work out what to do with it dynamically. You can
321
- pass it a string uri (in which case it will download the json from that location
322
- before validating), a string of JSON text, or simply a ruby object (such as an
323
- array or hash representing parsed json). However, sometimes the nature of the
324
- data is ambiguous (for example, is "http://github.com" just a string, or is it a
325
- uri?). In other situations, you have already parsed your JSON, and you don't
326
- need to re-parse it.
223
+ #
224
+ # with the `:parse_data` option set to false, the json must be a parsed ruby object (not a json text, a uri or a file path)
225
+ #
327
226
 
328
- If you want to be explict about what kind of data is being parsed, JSON schema
329
- supports a number of options:
227
+ # => true
228
+ JSON::Validator.validate(schema, { "a" => 1 }, :parse_data => false)
229
+ # => false
230
+ JSON::Validator.validate(schema, '{ "a": 1 }', :parse_data => false)
330
231
 
331
- ```rb
332
- require 'rubygems'
333
- require 'json-schema'
232
+ #
233
+ # with the `:json` option, the json must be an unparsed json text (not a hash, a uri or a file path)
234
+ #
334
235
 
335
- schema = {
336
- "type" => "string"
337
- }
338
-
339
- # examines the data, determines it's a uri, then tries to load data from it
340
- JSON::Validator.validate(schema, 'https://api.github.com') # returns false
236
+ # => true
237
+ JSON::Validator.validate(schema, '{ "a": 1 }', :json => true)
238
+ # => "no implicit conversion of Hash into String"
239
+ begin
240
+ JSON::Validator.validate(schema, { "a" => 1 }, :json => true)
241
+ rescue TypeError => e
242
+ e.message
243
+ end
341
244
 
342
- # data is already parsed json - just accept it as-is
343
- JSON::Validator.validate(schema, 'https://api.github.com', :parse_data => false) # returns true
245
+ #
246
+ # with the `:uri` option, the json must be a uri or file path (not a hash or a json text)
247
+ #
344
248
 
345
- # data is parsed to a json string
346
- JSON::Validator.validate(schema, '"https://api.github.com"', :json => true) # returns true
249
+ File.write("data.json", '{ "a": 1 }')
347
250
 
348
- # loads data from the uri
349
- JSON::Validator.validate(schema, 'https://api.github.com', :uri => true) # returns false
251
+ # => true
252
+ JSON::Validator.validate(schema, "data.json", :uri => true)
253
+ # => "Can't convert Hash into String."
254
+ begin
255
+ JSON::Validator.validate(schema, { "a" => 1 }, :uri => true)
256
+ rescue TypeError => e
257
+ e.message
258
+ end
350
259
  ```
351
260
 
352
- ### Extend an existing schema and validate against it
261
+ Extending Schemas
262
+ -----------------
353
263
 
354
264
  For this example, we are going to extend the [JSON Schema Draft
355
265
  3](http://tools.ietf.org/html/draft-zyp-json-schema-03) specification by adding
356
266
  a 'bitwise-and' property for validation.
357
267
 
358
- ```rb
359
- require 'rubygems'
360
- require 'json-schema'
268
+ ```ruby
269
+ require "json-schema"
361
270
 
362
271
  class BitwiseAndAttribute < JSON::Schema::Attribute
363
272
  def self.validate(current_schema, data, fragments, processor, validator, options = {})
@@ -403,7 +312,8 @@ data = {"a" => 0, "b" => "taco"}
403
312
  JSON::Validator.validate(schema,data) # => false
404
313
  ```
405
314
 
406
- ### Custom format validation
315
+ Custom format validation
316
+ ------------------------
407
317
 
408
318
  The JSON schema standard allows custom formats in schema definitions which
409
319
  should be ignored by validators that do not support them. JSON::Schema allows
@@ -412,9 +322,8 @@ checked as parameter and must raise a `JSON::Schema::CustomFormatError` to
412
322
  indicate a format violation. The error message will be prepended by the property
413
323
  name, e.g. [The property '#a']()
414
324
 
415
- ```rb
416
- require 'rubygems'
417
- require 'json-schema'
325
+ ```ruby
326
+ require "json-schema"
418
327
 
419
328
  format_proc = -> value {
420
329
  raise JSON::Schema::CustomFormatError.new("must be 42") unless value == "42"
@@ -456,7 +365,7 @@ control all schemas which should be used by validation, this could be
456
365
  accomplished by registering all referenced schemas with the validator in
457
366
  advance:
458
367
 
459
- ```rb
368
+ ```ruby
460
369
  schema = JSON::Schema.new(some_schema_definition, Addressable::URI.parse('http://example.com/my-schema'))
461
370
  JSON::Validator.add_schema(schema)
462
371
  ```
@@ -464,7 +373,7 @@ JSON::Validator.add_schema(schema)
464
373
  If more extensive control is necessary, the `JSON::Schema::Reader` instance used
465
374
  can be configured in a few ways:
466
375
 
467
- ```rb
376
+ ```ruby
468
377
  # Change the default schema reader used
469
378
  JSON::Validator.schema_reader = JSON::Schema::Reader.new(:accept_uri => true, :accept_file => false)
470
379
 
@@ -491,7 +400,7 @@ If more than one of the supported JSON backends are installed, the `yajl-ruby`
491
400
  parser is used by default. This can be changed by issuing the following before
492
401
  validation:
493
402
 
494
- ```rb
403
+ ```ruby
495
404
  JSON::Validator.json_backend = :json
496
405
  ```
497
406
 
@@ -48,11 +48,11 @@ module JSON
48
48
  if ref_schema
49
49
  # Perform fragment resolution to retrieve the appropriate level for the schema
50
50
  target_schema = ref_schema.schema
51
- fragments = temp_uri.fragment.split("/")
51
+ fragments = JSON::Util::URI.parse(JSON::Util::URI.unescape_uri(temp_uri)).fragment.split("/")
52
52
  fragment_path = ''
53
53
  fragments.each do |fragment|
54
54
  if fragment && fragment != ''
55
- fragment = JSON::Util::URI.unescaped_uri(fragment.gsub('~0', '~').gsub('~1', '/'))
55
+ fragment = fragment.gsub('~0', '~').gsub('~1', '/')
56
56
  if target_schema.is_a?(Array)
57
57
  target_schema = target_schema[fragment.to_i]
58
58
  else
@@ -102,7 +102,7 @@ module JSON
102
102
 
103
103
  def read_file(pathname)
104
104
  if accept_file?(pathname)
105
- File.read(JSON::Util::URI.unescaped_uri(pathname.to_s))
105
+ File.read(JSON::Util::URI.unescaped_path(pathname.to_s))
106
106
  else
107
107
  raise JSON::Schema::ReadRefused.new(pathname.to_s, :file)
108
108
  end
@@ -54,7 +54,11 @@ module JSON
54
54
  Addressable::URI.convert_path(parsed_uri.path)
55
55
  end
56
56
 
57
- def self.unescaped_uri(uri)
57
+ def self.unescape_uri(uri)
58
+ Addressable::URI.unescape(uri)
59
+ end
60
+
61
+ def self.unescaped_path(uri)
58
62
  parsed_uri = parse(uri)
59
63
 
60
64
  Addressable::URI.unescape(parsed_uri.path)
@@ -608,7 +608,7 @@ module JSON
608
608
  end
609
609
  else
610
610
  begin
611
- File.read(JSON::Util::URI.unescaped_uri(uri))
611
+ File.read(JSON::Util::URI.unescaped_path(uri))
612
612
  rescue SystemCallError => e
613
613
  raise JSON::Schema::JsonLoadError, e.message
614
614
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.1
4
+ version: 2.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenny Hoxworth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-26 00:00:00.000000000 Z
11
+ date: 2016-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -174,4 +174,3 @@ signing_key:
174
174
  specification_version: 4
175
175
  summary: Ruby JSON Schema Validator
176
176
  test_files: []
177
- has_rdoc: