json-schema 2.6.0 → 2.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{README.textile → README.md} +180 -113
- data/lib/json-schema/validator.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12af76aabf22ca3fd21620ba445eec49e02bb148
|
4
|
+
data.tar.gz: e337dd929041aa42efbda91fd360caf0cb854035
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa0037193fa2bb35abc47c05d51ad54f2d7c6f185e04ecd16332ca3b1749e67439429a69a1dbf73c15de1aa31fa21c603ea5d626c4fd6d788d3a64d4e14fc75d
|
7
|
+
data.tar.gz: a73e2d48d350b12c6a2b2108027845c9063de99a61ace81b7a0120cb22f55a39b769cf90b11c91fa58fed780215f6b6e66eb8c0a9b8311806fa86f2dd51b7b2a
|
@@ -1,48 +1,79 @@
|
|
1
|
-
!https://travis-ci.org/ruby-json-schema/json-schema
|
2
|
-
!https://codeclimate.com/github/ruby-json-schema/json-schema
|
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)
|
3
3
|
|
4
|
-
|
4
|
+
Ruby JSON Schema Validator
|
5
|
+
==========================
|
5
6
|
|
6
|
-
This library is intended to provide Ruby with an interface for validating JSON
|
7
|
+
This library is intended to provide Ruby with an interface for validating JSON
|
8
|
+
objects against a JSON schema conforming to [JSON Schema Draft
|
9
|
+
4](http://tools.ietf.org/html/draft-zyp-json-schema-04). Legacy support for
|
10
|
+
[JSON Schema Draft 3](http://tools.ietf.org/html/draft-zyp-json-schema-03),
|
11
|
+
[JSON Schema Draft 2](http://tools.ietf.org/html/draft-zyp-json-schema-02), and
|
12
|
+
[JSON Schema Draft 1](http://tools.ietf.org/html/draft-zyp-json-schema-01) is
|
13
|
+
also included.
|
7
14
|
|
8
|
-
|
15
|
+
Additional Resources
|
16
|
+
--------------------
|
9
17
|
|
10
|
-
|
11
|
-
|
18
|
+
- [Google Groups](https://groups.google.com/forum/#!forum/ruby-json-schema)
|
19
|
+
- #ruby-json-schema on chat.freenode.net
|
12
20
|
|
13
|
-
|
21
|
+
Version 2.0.0 Upgrade Notes
|
22
|
+
---------------------------
|
14
23
|
|
15
|
-
Please be aware that the upgrade to version 2.0.0 will use Draft-04
|
24
|
+
Please be aware that the upgrade to version 2.0.0 will use Draft-04 **by
|
25
|
+
default**, so schemas that do not declare a validator using the `$schema`
|
26
|
+
keyword will use Draft-04 now instead of Draft-03. This is the reason for the
|
27
|
+
major version upgrade.
|
16
28
|
|
17
|
-
|
29
|
+
Installation
|
30
|
+
------------
|
18
31
|
|
19
32
|
From rubygems.org:
|
20
33
|
|
21
|
-
|
34
|
+
```sh
|
22
35
|
gem install json-schema
|
23
|
-
|
36
|
+
```
|
24
37
|
|
25
38
|
From the git repo:
|
26
39
|
|
27
|
-
|
40
|
+
```sh
|
28
41
|
$ gem build json-schema.gemspec
|
29
42
|
$ gem install json-schema-2.5.2.gem
|
30
|
-
|
43
|
+
```
|
31
44
|
|
45
|
+
Usage
|
46
|
+
-----
|
32
47
|
|
33
|
-
|
48
|
+
Three base validation methods exist:
|
34
49
|
|
35
|
-
|
50
|
+
1. `validate`: returns a boolean on whether a validation attempt passes
|
51
|
+
2. `validate!`: throws a `JSON::Schema::ValidationError` with an appropriate message/trace on where the validation failed
|
52
|
+
3. `fully_validate`: builds an array of validation errors return when validation is complete
|
36
53
|
|
37
|
-
All methods take two arguments, which can be either a JSON string, a file
|
54
|
+
All methods take two arguments, which can be either a JSON string, a file
|
55
|
+
containing JSON, or a Ruby object representing JSON data. The first argument to
|
56
|
+
these methods is always the schema, the second is always the data to validate.
|
57
|
+
An optional third options argument is also accepted; available options are used
|
58
|
+
in the examples below.
|
38
59
|
|
39
|
-
By default, the validator uses the
|
60
|
+
By default, the validator uses the [JSON Schema Draft
|
61
|
+
4](http://tools.ietf.org/html/draft-zyp-json-schema-04) specification for
|
62
|
+
validation; however, the user is free to specify additional specifications or
|
63
|
+
extend existing ones. Legacy support for Draft 1, Draft 2, and Draft 3 is
|
64
|
+
included by either passing an optional `:version` parameter to the `validate`
|
65
|
+
method (set either as `:draft1` or `draft2`), or by declaring the `$schema`
|
66
|
+
attribute in the schema and referencing the appropriate specification URI. Note
|
67
|
+
that the `$schema` attribute takes precedence over the `:version` option during
|
68
|
+
parsing and validation.
|
40
69
|
|
41
|
-
|
70
|
+
### Validate Ruby objects against a Ruby schema
|
42
71
|
|
43
|
-
For further information on json schema itself refer to <a
|
72
|
+
For further information on json schema itself refer to <a
|
73
|
+
href="http://spacetelescope.github.io/understanding-json-schema/">Understanding
|
74
|
+
JSON Schema</a>.
|
44
75
|
|
45
|
-
|
76
|
+
```rb
|
46
77
|
require 'rubygems'
|
47
78
|
require 'json-schema'
|
48
79
|
|
@@ -59,32 +90,35 @@ data = {
|
|
59
90
|
}
|
60
91
|
|
61
92
|
JSON::Validator.validate(schema, data)
|
62
|
-
|
93
|
+
```
|
63
94
|
|
64
|
-
|
95
|
+
### Validate a JSON string against a JSON schema file
|
65
96
|
|
66
|
-
|
97
|
+
```rb
|
67
98
|
require 'rubygems'
|
68
99
|
require 'json-schema'
|
69
100
|
|
70
101
|
JSON::Validator.validate('schema.json', '{"a" : 5}')
|
71
|
-
|
102
|
+
```
|
72
103
|
|
73
|
-
|
104
|
+
### Validate a list of objects against a schema that represents the individual objects
|
74
105
|
|
75
|
-
|
106
|
+
```rb
|
76
107
|
require 'rubygems'
|
77
108
|
require 'json-schema'
|
78
109
|
|
79
110
|
data = ['user','user','user']
|
80
111
|
JSON::Validator.validate('user.json', data, :list => true)
|
81
|
-
|
112
|
+
```
|
82
113
|
|
83
|
-
|
114
|
+
### Strictly validate an object's properties
|
84
115
|
|
85
|
-
With the
|
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.
|
86
120
|
|
87
|
-
|
121
|
+
```rb
|
88
122
|
require 'rubygems'
|
89
123
|
require 'json-schema'
|
90
124
|
|
@@ -99,11 +133,11 @@ schema = {
|
|
99
133
|
JSON::Validator.validate(schema, {"a" => 1, "b" => 2}, :strict => true) # ==> true
|
100
134
|
JSON::Validator.validate(schema, {"a" => 1, "b" => 2, "c" => 3}, :strict => true) # ==> false
|
101
135
|
JSON::Validator.validate(schema, {"a" => 1}, :strict => true) # ==> false
|
102
|
-
|
136
|
+
```
|
103
137
|
|
104
|
-
|
138
|
+
### Catch a validation error and print it out
|
105
139
|
|
106
|
-
|
140
|
+
```rb
|
107
141
|
require 'rubygems'
|
108
142
|
require 'json-schema'
|
109
143
|
|
@@ -124,12 +158,11 @@ begin
|
|
124
158
|
rescue JSON::Schema::ValidationError
|
125
159
|
puts $!.message
|
126
160
|
end
|
127
|
-
|
161
|
+
```
|
128
162
|
|
163
|
+
### Fully validate against a schema and catch all errors
|
129
164
|
|
130
|
-
|
131
|
-
|
132
|
-
<pre>
|
165
|
+
```rb
|
133
166
|
require 'rubygems'
|
134
167
|
require 'json-schema'
|
135
168
|
|
@@ -149,11 +182,11 @@ data = {
|
|
149
182
|
errors = JSON::Validator.fully_validate(schema, data)
|
150
183
|
|
151
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#"]
|
152
|
-
|
185
|
+
```
|
153
186
|
|
154
|
-
|
187
|
+
### Fully validate against a schema and catch all errors as objects
|
155
188
|
|
156
|
-
|
189
|
+
```rb
|
157
190
|
require 'rubygems'
|
158
191
|
require 'json-schema'
|
159
192
|
|
@@ -172,41 +205,40 @@ data = {
|
|
172
205
|
|
173
206
|
errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
|
174
207
|
|
175
|
-
# [{:message=>"The property '#/a' of type String did not match the following type: integer in schema 03179a21-197e-5414-9611-e9f63e8324cd#", :schema
|
176
|
-
|
177
|
-
</pre>
|
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=>"#/"}]
|
209
|
+
```
|
178
210
|
|
179
|
-
|
211
|
+
### Validate against a fragment of a supplied schema
|
180
212
|
|
181
|
-
|
182
|
-
|
183
|
-
|
213
|
+
```rb
|
214
|
+
require 'rubygems'
|
215
|
+
require 'json-schema'
|
184
216
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
}
|
217
|
+
schema = {
|
218
|
+
"type" => "object",
|
219
|
+
"required" => ["a","b"],
|
220
|
+
"properties" => {
|
221
|
+
"a" => {"type" => "integer"},
|
222
|
+
"b" => {"type" => "string"},
|
223
|
+
"c" => {
|
224
|
+
"type" => "object",
|
225
|
+
"properties" => {
|
226
|
+
"z" => {"type" => "integer"}
|
196
227
|
}
|
197
228
|
}
|
198
229
|
}
|
230
|
+
}
|
199
231
|
|
200
|
-
|
201
|
-
|
202
|
-
|
232
|
+
data = {
|
233
|
+
"z" => 1
|
234
|
+
}
|
203
235
|
|
204
|
-
|
205
|
-
|
236
|
+
JSON::Validator.validate(schema, data, :fragment => "#/properties/c")
|
237
|
+
```
|
206
238
|
|
207
|
-
|
239
|
+
### Validate a JSON object against a JSON schema object, while also validating the schema itself
|
208
240
|
|
209
|
-
|
241
|
+
```rb
|
210
242
|
require 'rubygems'
|
211
243
|
require 'json-schema'
|
212
244
|
|
@@ -223,14 +255,16 @@ data = {
|
|
223
255
|
}
|
224
256
|
|
225
257
|
JSON::Validator.validate(schema, data, :validate_schema => true)
|
226
|
-
|
258
|
+
```
|
227
259
|
|
228
|
-
|
260
|
+
### Validate a JSON object against a JSON schema object, while inserting default values from the schema
|
229
261
|
|
230
|
-
With the
|
231
|
-
default value specified in the schema will be inserted into the validated data.
|
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.
|
232
266
|
|
233
|
-
|
267
|
+
```rb
|
234
268
|
require 'rubygems'
|
235
269
|
require 'json-schema'
|
236
270
|
|
@@ -258,11 +292,11 @@ JSON::Validator.validate(schema, data, :insert_defaults => true)
|
|
258
292
|
# "a" => 42,
|
259
293
|
# "b" => 5
|
260
294
|
# }
|
295
|
+
```
|
261
296
|
|
262
|
-
|
263
|
-
h3. Validate an object against a JSON Schema Draft 2 schema
|
297
|
+
### Validate an object against a JSON Schema Draft 2 schema
|
264
298
|
|
265
|
-
|
299
|
+
```rb
|
266
300
|
require 'rubygems'
|
267
301
|
require 'json-schema'
|
268
302
|
|
@@ -278,15 +312,23 @@ data = {
|
|
278
312
|
}
|
279
313
|
|
280
314
|
JSON::Validator.validate(schema, data, :version => :draft2)
|
281
|
-
|
315
|
+
```
|
282
316
|
|
283
|
-
|
317
|
+
### Explicitly specifying the type of the data
|
284
318
|
|
285
|
-
By default, json-schema accepts a variety of different types for the data
|
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.
|
286
327
|
|
287
|
-
If you want to be explict about what kind of data is being parsed, JSON schema
|
328
|
+
If you want to be explict about what kind of data is being parsed, JSON schema
|
329
|
+
supports a number of options:
|
288
330
|
|
289
|
-
|
331
|
+
```rb
|
290
332
|
require 'rubygems'
|
291
333
|
require 'json-schema'
|
292
334
|
|
@@ -305,13 +347,15 @@ JSON::Validator.validate(schema, '"https://api.github.com"', :json => true) # re
|
|
305
347
|
|
306
348
|
# loads data from the uri
|
307
349
|
JSON::Validator.validate(schema, 'https://api.github.com', :uri => true) # returns false
|
308
|
-
|
350
|
+
```
|
309
351
|
|
310
|
-
|
352
|
+
### Extend an existing schema and validate against it
|
311
353
|
|
312
|
-
For this example, we are going to extend the
|
354
|
+
For this example, we are going to extend the [JSON Schema Draft
|
355
|
+
3](http://tools.ietf.org/html/draft-zyp-json-schema-03) specification by adding
|
356
|
+
a 'bitwise-and' property for validation.
|
313
357
|
|
314
|
-
|
358
|
+
```rb
|
315
359
|
require 'rubygems'
|
316
360
|
require 'json-schema'
|
317
361
|
|
@@ -357,13 +401,18 @@ data = {"a" => 1, "b" => 5}
|
|
357
401
|
JSON::Validator.validate(schema,data) # => false
|
358
402
|
data = {"a" => 0, "b" => "taco"}
|
359
403
|
JSON::Validator.validate(schema,data) # => false
|
360
|
-
|
404
|
+
```
|
361
405
|
|
362
|
-
|
406
|
+
### Custom format validation
|
363
407
|
|
364
|
-
The JSON schema standard allows custom formats in schema definitions which
|
408
|
+
The JSON schema standard allows custom formats in schema definitions which
|
409
|
+
should be ignored by validators that do not support them. JSON::Schema allows
|
410
|
+
registering procs as custom format validators which receive the value to be
|
411
|
+
checked as parameter and must raise a `JSON::Schema::CustomFormatError` to
|
412
|
+
indicate a format violation. The error message will be prepended by the property
|
413
|
+
name, e.g. [The property '#a']()
|
365
414
|
|
366
|
-
|
415
|
+
```rb
|
367
416
|
require 'rubygems'
|
368
417
|
require 'json-schema'
|
369
418
|
|
@@ -396,21 +445,26 @@ schema = {
|
|
396
445
|
}
|
397
446
|
}
|
398
447
|
errors = JSON::Validator.fully_validate(schema, {"a" => "23"})
|
448
|
+
```
|
399
449
|
|
400
|
-
|
401
|
-
|
402
|
-
h2. Controlling Remote Schema Reading
|
450
|
+
Controlling Remote Schema Reading
|
451
|
+
---------------------------------
|
403
452
|
|
404
|
-
In some cases, you may wish to prevent the JSON Schema library from making HTTP
|
453
|
+
In some cases, you may wish to prevent the JSON Schema library from making HTTP
|
454
|
+
calls or reading local files in order to resolve `$ref` schemas. If you fully
|
455
|
+
control all schemas which should be used by validation, this could be
|
456
|
+
accomplished by registering all referenced schemas with the validator in
|
457
|
+
advance:
|
405
458
|
|
406
|
-
|
459
|
+
```rb
|
407
460
|
schema = JSON::Schema.new(some_schema_definition, Addressable::URI.parse('http://example.com/my-schema'))
|
408
461
|
JSON::Validator.add_schema(schema)
|
409
|
-
|
462
|
+
```
|
410
463
|
|
411
|
-
If more extensive control is necessary, the
|
464
|
+
If more extensive control is necessary, the `JSON::Schema::Reader` instance used
|
465
|
+
can be configured in a few ways:
|
412
466
|
|
413
|
-
|
467
|
+
```rb
|
414
468
|
# Change the default schema reader used
|
415
469
|
JSON::Validator.schema_reader = JSON::Schema::Reader.new(:accept_uri => true, :accept_file => false)
|
416
470
|
|
@@ -419,34 +473,47 @@ schema_reader = JSON::Schema::Reader.new(
|
|
419
473
|
:accept_uri => proc { |uri| uri.host == 'my-website.com' }
|
420
474
|
)
|
421
475
|
JSON::Validator.validate(some_schema, some_object, :schema_reader => schema_reader)
|
422
|
-
|
476
|
+
```
|
423
477
|
|
424
|
-
The
|
478
|
+
The `JSON::Schema::Reader` interface requires only an object which responds to
|
479
|
+
`read(string)` and returns a `JSON::Schema` instance. See the [API
|
480
|
+
documentation](http://www.rubydoc.info/github/ruby-json-schema/json-schema/master/JSON/Schema/Reader)
|
481
|
+
for more information.
|
425
482
|
|
426
|
-
|
483
|
+
JSON Backends
|
484
|
+
-------------
|
427
485
|
|
428
|
-
The JSON Schema library currently supports the
|
486
|
+
The JSON Schema library currently supports the `json` and `yajl-ruby` backend
|
487
|
+
JSON parsers. If either of these libraries are installed, they will be
|
488
|
+
automatically loaded and used to parse any JSON strings supplied by the user.
|
429
489
|
|
430
|
-
If more than one of the supported JSON backends are installed, the
|
490
|
+
If more than one of the supported JSON backends are installed, the `yajl-ruby`
|
491
|
+
parser is used by default. This can be changed by issuing the following before
|
492
|
+
validation:
|
431
493
|
|
432
|
-
|
494
|
+
```rb
|
433
495
|
JSON::Validator.json_backend = :json
|
434
|
-
|
496
|
+
```
|
435
497
|
|
436
|
-
Optionally, the JSON Schema library supports using the MultiJSON library for
|
498
|
+
Optionally, the JSON Schema library supports using the MultiJSON library for
|
499
|
+
selecting JSON backends. If the MultiJSON library is installed, it will be
|
500
|
+
autoloaded.
|
437
501
|
|
438
|
-
|
502
|
+
Notes
|
503
|
+
-----
|
439
504
|
|
440
505
|
The 'format' attribute is only validated for the following values:
|
441
506
|
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
All other 'format' attribute values are simply checked to ensure the instance
|
507
|
+
- date-time
|
508
|
+
- date
|
509
|
+
- time
|
510
|
+
- ip-address (IPv4 address in draft1, draft2 and draft3)
|
511
|
+
- ipv4 (IPv4 address in draft4)
|
512
|
+
- ipv6
|
513
|
+
- uri
|
514
|
+
|
515
|
+
All other 'format' attribute values are simply checked to ensure the instance
|
516
|
+
value is of the correct datatype (e.g., an instance value is validated to be an
|
517
|
+
integer or a float in the case of 'utc-millisec').
|
451
518
|
|
452
519
|
Additionally, JSON::Validator does not handle any json hyperschema attributes.
|
@@ -569,7 +569,7 @@ module JSON
|
|
569
569
|
end
|
570
570
|
Validator.add_schema(schema)
|
571
571
|
else
|
572
|
-
raise SchemaParseError, "Invalid schema - must be either a string or a hash"
|
572
|
+
raise JSON::Schema::SchemaParseError, "Invalid schema - must be either a string or a hash"
|
573
573
|
end
|
574
574
|
|
575
575
|
schema
|
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.
|
4
|
+
version: 2.6.1
|
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-
|
11
|
+
date: 2016-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -85,11 +85,11 @@ email: hoxworth@gmail.com
|
|
85
85
|
executables: []
|
86
86
|
extensions: []
|
87
87
|
extra_rdoc_files:
|
88
|
-
- README.
|
88
|
+
- README.md
|
89
89
|
- LICENSE.md
|
90
90
|
files:
|
91
91
|
- LICENSE.md
|
92
|
-
- README.
|
92
|
+
- README.md
|
93
93
|
- lib/json-schema.rb
|
94
94
|
- lib/json-schema/attribute.rb
|
95
95
|
- lib/json-schema/attributes/additionalitems.rb
|