json-schema-openc-fork 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE.md +19 -0
  3. data/README.textile +452 -0
  4. data/lib/json-schema.rb +19 -0
  5. data/lib/json-schema/attribute.rb +43 -0
  6. data/lib/json-schema/attributes/additionalitems.rb +28 -0
  7. data/lib/json-schema/attributes/additionalproperties.rb +58 -0
  8. data/lib/json-schema/attributes/allof.rb +39 -0
  9. data/lib/json-schema/attributes/anyof.rb +47 -0
  10. data/lib/json-schema/attributes/dependencies.rb +44 -0
  11. data/lib/json-schema/attributes/disallow.rb +12 -0
  12. data/lib/json-schema/attributes/divisibleby.rb +22 -0
  13. data/lib/json-schema/attributes/enum.rb +24 -0
  14. data/lib/json-schema/attributes/extends.rb +50 -0
  15. data/lib/json-schema/attributes/format.rb +14 -0
  16. data/lib/json-schema/attributes/formats/custom.rb +21 -0
  17. data/lib/json-schema/attributes/formats/date.rb +24 -0
  18. data/lib/json-schema/attributes/formats/date_time.rb +36 -0
  19. data/lib/json-schema/attributes/formats/date_time_v4.rb +15 -0
  20. data/lib/json-schema/attributes/formats/ip.rb +41 -0
  21. data/lib/json-schema/attributes/formats/time.rb +22 -0
  22. data/lib/json-schema/attributes/formats/uri.rb +20 -0
  23. data/lib/json-schema/attributes/items.rb +26 -0
  24. data/lib/json-schema/attributes/limit.rb +179 -0
  25. data/lib/json-schema/attributes/maxdecimal.rb +18 -0
  26. data/lib/json-schema/attributes/multipleof.rb +11 -0
  27. data/lib/json-schema/attributes/not.rb +30 -0
  28. data/lib/json-schema/attributes/oneof.rb +56 -0
  29. data/lib/json-schema/attributes/pattern.rb +18 -0
  30. data/lib/json-schema/attributes/patternproperties.rb +22 -0
  31. data/lib/json-schema/attributes/properties.rb +74 -0
  32. data/lib/json-schema/attributes/properties_optional.rb +26 -0
  33. data/lib/json-schema/attributes/ref.rb +74 -0
  34. data/lib/json-schema/attributes/required.rb +28 -0
  35. data/lib/json-schema/attributes/type.rb +83 -0
  36. data/lib/json-schema/attributes/type_v4.rb +29 -0
  37. data/lib/json-schema/attributes/uniqueitems.rb +16 -0
  38. data/lib/json-schema/errors/custom_format_error.rb +6 -0
  39. data/lib/json-schema/errors/json_parse_error.rb +6 -0
  40. data/lib/json-schema/errors/schema_error.rb +6 -0
  41. data/lib/json-schema/errors/validation_error.rb +46 -0
  42. data/lib/json-schema/schema.rb +63 -0
  43. data/lib/json-schema/schema/reader.rb +113 -0
  44. data/lib/json-schema/schema/validator.rb +36 -0
  45. data/lib/json-schema/util/array_set.rb +14 -0
  46. data/lib/json-schema/util/uri.rb +16 -0
  47. data/lib/json-schema/util/uuid.rb +285 -0
  48. data/lib/json-schema/validator.rb +592 -0
  49. data/lib/json-schema/validators/draft1.rb +45 -0
  50. data/lib/json-schema/validators/draft2.rb +46 -0
  51. data/lib/json-schema/validators/draft3.rb +50 -0
  52. data/lib/json-schema/validators/draft4.rb +56 -0
  53. data/lib/json-schema/validators/hyper-draft4.rb +14 -0
  54. data/resources/draft-01.json +155 -0
  55. data/resources/draft-02.json +166 -0
  56. data/resources/draft-03.json +174 -0
  57. data/resources/draft-04.json +150 -0
  58. data/test/data/all_of_ref_data.json +3 -0
  59. data/test/data/any_of_ref_data.json +7 -0
  60. data/test/data/bad_data_1.json +3 -0
  61. data/test/data/good_data_1.json +3 -0
  62. data/test/data/one_of_ref_links_data.json +5 -0
  63. data/test/schemas/address_microformat.json +18 -0
  64. data/test/schemas/all_of_ref_base_schema.json +6 -0
  65. data/test/schemas/all_of_ref_schema.json +7 -0
  66. data/test/schemas/any_of_ref_jane_schema.json +4 -0
  67. data/test/schemas/any_of_ref_jimmy_schema.json +4 -0
  68. data/test/schemas/any_of_ref_john_schema.json +4 -0
  69. data/test/schemas/any_of_ref_schema.json +15 -0
  70. data/test/schemas/definition_schema.json +15 -0
  71. data/test/schemas/extends_and_additionalProperties-1-filename.schema.json +34 -0
  72. data/test/schemas/extends_and_additionalProperties-1-ref.schema.json +34 -0
  73. data/test/schemas/extends_and_additionalProperties-2-filename.schema.json +33 -0
  74. data/test/schemas/extends_and_additionalProperties-2-ref.schema.json +33 -0
  75. data/test/schemas/good_schema_1.json +10 -0
  76. data/test/schemas/good_schema_2.json +10 -0
  77. data/test/schemas/good_schema_extends1.json +10 -0
  78. data/test/schemas/good_schema_extends2.json +13 -0
  79. data/test/schemas/inner.schema.json +21 -0
  80. data/test/schemas/one_of_ref_links_schema.json +16 -0
  81. data/test/schemas/ref john with spaces schema.json +11 -0
  82. data/test/schemas/relative_definition_schema.json +8 -0
  83. data/test/schemas/self_link_schema.json +17 -0
  84. data/test/schemas/up_link_schema.json +17 -0
  85. data/test/test_all_of_ref_schema.rb +35 -0
  86. data/test/test_any_of_ref_schema.rb +35 -0
  87. data/test/test_bad_schema_ref.rb +39 -0
  88. data/test/test_common_test_suite.rb +66 -0
  89. data/test/test_custom_format.rb +116 -0
  90. data/test/test_definition.rb +15 -0
  91. data/test/test_extended_schema.rb +62 -0
  92. data/test/test_extends_and_additionalProperties.rb +52 -0
  93. data/test/test_files_v3.rb +43 -0
  94. data/test/test_fragment_resolution.rb +30 -0
  95. data/test/test_fragment_validation_with_ref.rb +34 -0
  96. data/test/test_full_validation.rb +208 -0
  97. data/test/test_helper.rb +47 -0
  98. data/test/test_initialize_data.rb +118 -0
  99. data/test/test_jsonschema_draft1.rb +171 -0
  100. data/test/test_jsonschema_draft2.rb +142 -0
  101. data/test/test_jsonschema_draft3.rb +502 -0
  102. data/test/test_jsonschema_draft4.rb +704 -0
  103. data/test/test_list_option.rb +21 -0
  104. data/test/test_merge_missing_values.rb +45 -0
  105. data/test/test_minitems.rb +16 -0
  106. data/test/test_one_of.rb +85 -0
  107. data/test/test_ruby_schema.rb +59 -0
  108. data/test/test_schema_loader.rb +74 -0
  109. data/test/test_schema_type_attribute.rb +20 -0
  110. data/test/test_schema_validation.rb +185 -0
  111. data/test/test_stringify.rb +48 -0
  112. data/test/test_uri_related.rb +67 -0
  113. data/test/test_validator.rb +53 -0
  114. metadata +284 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDQ1ZDEyOTcxZmRjYmU4MmU5YzE1NmUzODJhZjI5MzYxODk2MzI0MQ==
5
+ data.tar.gz: !binary |-
6
+ ODU3N2JiZjVkMGZiZDUwZjYzYmY2ZmViYjc1ZTViZTRmMzg2MTM0ZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YTc2NGY3YmNiMTNiYzMwYTJiNWIyZGQwMjA0N2MxNzczMTFjZWJiMGI0OTY1
10
+ MDg0MzBkOTRiMWYwNzZkNGExZGRiMDM1Y2Y5NjAyOGJkMGMwYmNiMmM3NmU3
11
+ NDlkMDkzZjE4NDM0OTI3NjA1NWIyOGMwNGVkZWYzMjI2OWJlM2Q=
12
+ data.tar.gz: !binary |-
13
+ ZGQ5ZjhmNTcyZGU5YTE3MGIxNTcwNjgwZTQ4MTBlMzk0YmYwMjkwODcwNTIx
14
+ Mjc4NjM1OTZkYjgwZjU5OGUxZWYzNWUzZThhOTcxNzU0YmI1ZWE0M2FkZjI1
15
+ YzRiN2M2ZWU5MmEyNjVhOGQwZGYyYzRiMTY0ZDJiZjllNjdlZjM=
data/LICENSE.md ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010-2011, Lookingglass Cyber Solutions
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,452 @@
1
+ !https://travis-ci.org/ruby-json-schema/json-schema.svg?branch=master!:https://travis-ci.org/ruby-json-schema/json-schema
2
+ !https://codeclimate.com/github/ruby-json-schema/json-schema/badges/gpa.svg!:https://codeclimate.com/github/ruby-json-schema/json-schema
3
+
4
+ h1. Ruby JSON Schema Validator
5
+
6
+ This library is intended to provide Ruby with an interface for validating JSON objects against a JSON schema conforming to "JSON Schema Draft 4":http://tools.ietf.org/html/draft-zyp-json-schema-04. Legacy support for "JSON Schema Draft 3":http://tools.ietf.org/html/draft-zyp-json-schema-03, "JSON Schema Draft 2":http://tools.ietf.org/html/draft-zyp-json-schema-02, and "JSON Schema Draft 1":http://tools.ietf.org/html/draft-zyp-json-schema-01 is also included.
7
+
8
+ h2. Additional Resources
9
+
10
+ * "Google Groups":https://groups.google.com/forum/#!forum/ruby-json-schema
11
+ * #ruby-json-schema on chat.freenode.net
12
+
13
+ h2. Version 2.0.0 Upgrade Notes
14
+
15
+ Please be aware that the upgrade to version 2.0.0 will use Draft-04 *by default*, so schemas that do not declare a validator using the <code>$schema</code> keyword will use Draft-04 now instead of Draft-03. This is the reason for the major version upgrade.
16
+
17
+ h2. Installation
18
+
19
+ From rubygems.org:
20
+
21
+ <pre>
22
+ gem install json-schema
23
+ </pre>
24
+
25
+ From the git repo:
26
+
27
+ <pre>
28
+ $ gem build json-schema.gemspec
29
+ $ gem install json-schema-2.5.0.gem
30
+ </pre>
31
+
32
+
33
+ h2. Usage
34
+
35
+ Three base validation methods exist: <code>validate</code>, <code>validate!</code>, and <code>fully_validate</code>. The first returns a boolean on whether a validation attempt passes and the second will throw a <code>JSON::Schema::ValidationError</code> with an appropriate message/trace on where the validation failed. The third validation method does not immediately fail upon a validation error and instead builds an array of validation errors return when validation is complete.
36
+
37
+ All methods take two arguments, which can be either a JSON string, a file containing JSON, or a Ruby object representing JSON data. The first argument to these methods is always the schema, the second is always the data to validate. An optional third options argument is also accepted; available options are used in the examples below.
38
+
39
+ By default, the validator uses the "JSON Schema Draft 4":http://tools.ietf.org/html/draft-zyp-json-schema-04 specification for validation; however, the user is free to specify additional specifications or extend existing ones. Legacy support for Draft 1, Draft 2, and Draft 3 is included by either passing an optional <code>:version</code> parameter to the <code>validate</code> method (set either as <code>:draft1</code> or <code>draft2</code>), or by declaring the <code>$schema</code> attribute in the schema and referencing the appropriate specification URI. Note that the <code>$schema</code> attribute takes precedence over the <code>:version</code> option during parsing and validation.
40
+
41
+ h3. Validate Ruby objects against a Ruby schema
42
+
43
+ For further information on json schema itself refer to <a href="http://spacetelescope.github.io/understanding-json-schema/">Understanding JSON Schema</a>.
44
+
45
+ <pre>
46
+ require 'rubygems'
47
+ require 'json-schema'
48
+
49
+ schema = {
50
+ "type" => "object",
51
+ "required" => ["a"],
52
+ "properties" => {
53
+ "a" => {"type" => "integer"}
54
+ }
55
+ }
56
+
57
+ data = {
58
+ "a" => 5
59
+ }
60
+
61
+ JSON::Validator.validate(schema, data)
62
+ </pre>
63
+
64
+ h3. Validate a JSON string against a JSON schema file
65
+
66
+ <pre>
67
+ require 'rubygems'
68
+ require 'json-schema'
69
+
70
+ JSON::Validator.validate('schema.json', '{"a" : 5}')
71
+ </pre>
72
+
73
+ h3. Validate a list of objects against a schema that represents the individual objects
74
+
75
+ <pre>
76
+ require 'rubygems'
77
+ require 'json-schema'
78
+
79
+ data = ['user','user','user']
80
+ JSON::Validator.validate('user.json', data, :list => true)
81
+ </pre>
82
+
83
+ h3. Strictly validate an object's properties
84
+
85
+ With the <code>:strict</code> option, validation fails when an object contains properties that are not defined in the schema's property list or doesn't match the <code>additionalProperties</code> property. Furthermore, all properties are treated as <code>required</code> by default.
86
+
87
+ <pre>
88
+ require 'rubygems'
89
+ require 'json-schema'
90
+
91
+ schema = {
92
+ "type" => "object",
93
+ "properties" => {
94
+ "a" => {"type" => "integer"},
95
+ "b" => {"type" => "integer"}
96
+ }
97
+ }
98
+
99
+ JSON::Validator.validate(schema, {"a" => 1, "b" => 2}, :strict => true) # ==> true
100
+ JSON::Validator.validate(schema, {"a" => 1, "b" => 2, "c" => 3}, :strict => true) # ==> false
101
+ JSON::Validator.validate(schema, {"a" => 1}, :strict => true) # ==> false
102
+ </pre>
103
+
104
+ h3. Catch a validation error and print it out
105
+
106
+ <pre>
107
+ require 'rubygems'
108
+ require 'json-schema'
109
+
110
+ schema = {
111
+ "type" => "object",
112
+ "required" => ["a"],
113
+ "properties" => {
114
+ "a" => {"type" => "integer"}
115
+ }
116
+ }
117
+
118
+ data = {
119
+ "a" => "taco"
120
+ }
121
+
122
+ begin
123
+ JSON::Validator.validate!(schema, data)
124
+ rescue JSON::Schema::ValidationError
125
+ puts $!.message
126
+ end
127
+ </pre>
128
+
129
+
130
+ h3. Fully validate against a schema and catch all errors
131
+
132
+ <pre>
133
+ require 'rubygems'
134
+ require 'json-schema'
135
+
136
+ schema = {
137
+ "type" => "object",
138
+ "required" => ["a","b"],
139
+ "properties" => {
140
+ "a" => {"type" => "integer"},
141
+ "b" => {"type" => "string"}
142
+ }
143
+ }
144
+
145
+ data = {
146
+ "a" => "taco"
147
+ }
148
+
149
+ errors = JSON::Validator.fully_validate(schema, data)
150
+
151
+ # ["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
+ </pre>
153
+
154
+ h3. Fully validate against a schema and catch all errors as objects
155
+
156
+ <pre>
157
+ require 'rubygems'
158
+ require 'json-schema'
159
+
160
+ schema = {
161
+ "type" => "object",
162
+ "required" => ["a","b"],
163
+ "properties" => {
164
+ "a" => {"type" => "integer"},
165
+ "b" => {"type" => "string"}
166
+ }
167
+ }
168
+
169
+ data = {
170
+ "a" => "taco"
171
+ }
172
+
173
+ errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
174
+
175
+ # [{:message=>"The property '#/a' of type String did not match the following type: integer in schema 03179a21-197e-5414-9611-e9f63e8324cd#", :schema=>#<URI::Generic:0x103a76198 URL:03179a21-197e-5414-9611-e9f63e8324cd#>, :failed_attribute=>"Type", :fragment=>"#/a"}, {:message=>"The property '#/' did not contain a required property of 'b' in schema 03179a21-197e-5414-9611-e9f63e8324cd#", :schema=>#<URI::Generic:0x103a76198 URL:03179a21-197e-5414-9611-e9f63e8324cd#>, :failed_attribute=>"Properties", :fragment=>"#/"}]
176
+
177
+ </pre>
178
+
179
+ h3. Validate against a fragment of a supplied schema
180
+
181
+ <pre>
182
+ require 'rubygems'
183
+ require 'json-schema'
184
+
185
+ schema = {
186
+ "type" => "object",
187
+ "required" => ["a","b"],
188
+ "properties" => {
189
+ "a" => {"type" => "integer"},
190
+ "b" => {"type" => "string"},
191
+ "c" => {
192
+ "type" => "object",
193
+ "properties" => {
194
+ "z" => {"type" => "integer"}
195
+ }
196
+ }
197
+ }
198
+ }
199
+
200
+ data = {
201
+ "z" => 1
202
+ }
203
+
204
+ JSON::Validator.validate(schema, data, :fragment => "#/properties/c")
205
+ </pre>
206
+
207
+ h3. Validate a JSON object against a JSON schema object, while also validating the schema itself
208
+
209
+ <pre>
210
+ require 'rubygems'
211
+ require 'json-schema'
212
+
213
+ schema = {
214
+ "type" => "object",
215
+ "required" => ["a"],
216
+ "properties" => {
217
+ "a" => {"type" => "integer"} # This will fail schema validation!
218
+ }
219
+ }
220
+
221
+ data = {
222
+ "a" => 5
223
+ }
224
+
225
+ JSON::Validator.validate(schema, data, :validate_schema => true)
226
+ </pre>
227
+
228
+ h3. Validate a JSON object against a JSON schema object, while inserting default values from the schema
229
+
230
+ With the :insert_defaults option set to true any missing property that has a
231
+ default value specified in the schema will be inserted into the validated data. The inserted default value is validated hence catching a schema that specifies an invalid default value.
232
+
233
+ <pre>
234
+ require 'rubygems'
235
+ require 'json-schema'
236
+
237
+ schema = {
238
+ "type" => "object",
239
+ "required" => ["a"],
240
+ "properties" => {
241
+ "a" => {"type" => "integer", "default" => 42},
242
+ "b" => {"type" => "integer"}
243
+ }
244
+ }
245
+
246
+ # Would not normally validate because "a" is missing and required by schema,
247
+ # but "default" option allows insertion of valid default.
248
+ data = {
249
+ "b" => 5
250
+ }
251
+
252
+ JSON::Validator.validate(schema, data)
253
+ # false
254
+
255
+ JSON::Validator.validate(schema, data, :insert_defaults => true)
256
+ # true
257
+ # data = {
258
+ # "a" => 42,
259
+ # "b" => 5
260
+ # }
261
+
262
+ </pre>
263
+ h3. Validate an object against a JSON Schema Draft 2 schema
264
+
265
+ <pre>
266
+ require 'rubygems'
267
+ require 'json-schema'
268
+
269
+ schema = {
270
+ "type" => "object",
271
+ "properties" => {
272
+ "a" => {"type" => "integer", "optional" => true}
273
+ }
274
+ }
275
+
276
+ data = {
277
+ "a" => 5
278
+ }
279
+
280
+ JSON::Validator.validate(schema, data, :version => :draft2)
281
+ </pre>
282
+
283
+ h3. Explicitly specifying the type of the data
284
+
285
+ By default, json-schema accepts a variety of different types for the data parameter, and it will try to work out what to do with it dynamically. You can pass it a string uri (in which case it will download the json from that location before validating), a string of JSON text, or simply a ruby object (such as an array or hash representing parsed json). However, sometimes the nature of the data is ambiguous (for example, is "http://github.com" just a string, or is it a uri?). In other situations, you have already parsed your JSON, and you don't need to re-parse it.
286
+
287
+ If you want to be explict about what kind of data is being parsed, JSON schema supports a number of options:
288
+
289
+ <pre>
290
+ require 'rubygems'
291
+ require 'json-schema'
292
+
293
+ schema = {
294
+ "type" => "string"
295
+ }
296
+
297
+ # examines the data, determines it's a uri, then tries to load data from it
298
+ JSON::Validator.validate(schema, 'https://api.github.com') # returns false
299
+
300
+ # data is already parsed json - just accept it as-is
301
+ JSON::Validator.validate(schema, 'https://api.github.com', :parse_data => false) # returns true
302
+
303
+ # data is parsed to a json string
304
+ JSON::Validator.validate(schema, '"https://api.github.com"', :json => true) # returns true
305
+
306
+ # loads data from the uri
307
+ JSON::Validator.validate(schema, 'https://api.github.com', :uri => true) # returns false
308
+ </pre>
309
+
310
+ h3. Extend an existing schema and validate against it
311
+
312
+ For this example, we are going to extend the "JSON Schema Draft 3":http://tools.ietf.org/html/draft-zyp-json-schema-03 specification by adding a 'bitwise-and' property for validation.
313
+
314
+ <pre>
315
+ require 'rubygems'
316
+ require 'json-schema'
317
+
318
+ class BitwiseAndAttribute < JSON::Schema::Attribute
319
+ def self.validate(current_schema, data, fragments, processor, validator, options = {})
320
+ if data.is_a?(Integer) && data & current_schema.schema['bitwise-and'].to_i == 0
321
+ message = "The property '#{build_fragment(fragments)}' did not evaluate to true when bitwise-AND'd with #{current_schema.schema['bitwise-or']}"
322
+ raise JSON::Schema::ValidationError.new(message, fragments, current_schema)
323
+ end
324
+ end
325
+ end
326
+
327
+ class ExtendedSchema < JSON::Schema::Validator
328
+ def initialize
329
+ super
330
+ extend_schema_definition("http://json-schema.org/draft-03/schema#")
331
+ @attributes["bitwise-and"] = BitwiseAndAttribute
332
+ @uri = URI.parse("http://test.com/test.json")
333
+ end
334
+
335
+ JSON::Validator.register_validator(self.new)
336
+ end
337
+
338
+ schema = {
339
+ "$schema" => "http://test.com/test.json",
340
+ "properties" => {
341
+ "a" => {
342
+ "bitwise-and" => 1
343
+ },
344
+ "b" => {
345
+ "type" => "string"
346
+ }
347
+ }
348
+ }
349
+
350
+ data = {
351
+ "a" => 0
352
+ }
353
+
354
+ data = {"a" => 1, "b" => "taco"}
355
+ JSON::Validator.validate(schema,data) # => true
356
+ data = {"a" => 1, "b" => 5}
357
+ JSON::Validator.validate(schema,data) # => false
358
+ data = {"a" => 0, "b" => "taco"}
359
+ JSON::Validator.validate(schema,data) # => false
360
+ </pre>
361
+
362
+ h3. Custom format validation
363
+
364
+ The JSON schema standard allows custom formats in schema definitions which should be ignored by validators that do not support them. JSON::Schema allows registering procs as custom format validators which receive the value to be checked as parameter and must raise a <code>JSON::Schema::CustomFormatError</code> to indicate a format violation. The error message will be prepended by the property name, e.g. "The property '#a'":
365
+
366
+ <pre>
367
+ require 'rubygems'
368
+ require 'json-schema'
369
+
370
+ format_proc = -> value {
371
+ raise JSON::Schema::CustomFormatError.new("must be 42") unless value == "42"
372
+ }
373
+
374
+ # register the proc for format 'the-answer' for draft4 schema
375
+ JSON::Validator.register_format_validator("the-answer", format_proc, ["draft4"])
376
+
377
+ # omitting the version parameter uses ["draft1", "draft2", "draft3", "draft4"] as default
378
+ JSON::Validator.register_format_validator("the-answer", format_proc)
379
+
380
+ # deregistering the custom validator
381
+ # (also ["draft1", "draft2", "draft3", "draft4"] as default version)
382
+ JSON::Validator.deregister_format_validator('the-answer', ["draft4"])
383
+
384
+ # shortcut to restore the default formats for validators (same default as before)
385
+ JSON::Validator.restore_default_formats(["draft4"])
386
+
387
+ # with the validator registered as above, the following results in
388
+ # ["The property '#a' must be 42"] as returned errors
389
+ schema = {
390
+ "$schema" => "http://json-schema.org/draft-04/schema#",
391
+ "properties" => {
392
+ "a" => {
393
+ "type" => "string",
394
+ "format" => "the-answer",
395
+ }
396
+ }
397
+ }
398
+ errors = JSON::Validator.fully_validate(schema, {"a" => "23"})
399
+
400
+ </pre>
401
+
402
+ h2. Controlling Remote Schema Reading
403
+
404
+ In some cases, you may wish to prevent the JSON Schema library from making HTTP calls or reading local files in order to resolve <code>$ref</code> schemas. If you fully control all schemas which should be used by validation, this could be accomplished by registering all referenced schemas with the validator in advance:
405
+
406
+ <pre>
407
+ schema = JSON::Schema.new(some_schema_definition, Addressable::URI.parse('http://example.com/my-schema'))
408
+ JSON::Validator.add_schema(schema)
409
+ </pre>
410
+
411
+ If more extensive control is necessary, the <code>JSON::Schema::Reader</code> instance used can be configured in a few ways:
412
+
413
+ <pre>
414
+ # Change the default schema reader used
415
+ JSON::Validator.schema_reader = JSON::Schema::Reader.new(:accept_uri => true, :accept_file => false)
416
+
417
+ # For this validation call, use a reader which only accepts URIs from my-website.com
418
+ schema_reader = JSON::Schema::Reader.new(
419
+ :accept_uri => proc { |uri| uri.host == 'my-website.com' }
420
+ )
421
+ JSON::Validator.validate(some_schema, some_object, :schema_reader => schema_reader)
422
+ </pre>
423
+
424
+ The <code>JSON::Schema::Reader</code> interface requires only an object which responds to <code>read(string)</code> and returns a <code>JSON::Schema</code> instance. See the "API documentation":http://www.rubydoc.info/github/ruby-json-schema/json-schema/master/JSON/Schema/Reader for more information.
425
+
426
+ h2. JSON Backends
427
+
428
+ The JSON Schema library currently supports the <code>json</code> and <code>yajl-ruby</code> backend JSON parsers. If either of these libraries are installed, they will be automatically loaded and used to parse any JSON strings supplied by the user.
429
+
430
+ If more than one of the supported JSON backends are installed, the <code>yajl-ruby</code> parser is used by default. This can be changed by issuing the following before validation:
431
+
432
+ <pre>
433
+ JSON::Validator.json_backend = :json
434
+ </pre>
435
+
436
+ Optionally, the JSON Schema library supports using the MultiJSON library for selecting JSON backends. If the MultiJSON library is installed, it will be autoloaded.
437
+
438
+ h2. Notes
439
+
440
+ The 'format' attribute is only validated for the following values:
441
+
442
+ * date-time
443
+ * date
444
+ * time
445
+ * ip-address (IPv4 address in draft1, draft2 and draft3)
446
+ * ipv4 (IPv4 address in draft4)
447
+ * ipv6
448
+ * uri
449
+
450
+ All other 'format' attribute values are simply checked to ensure the instance value is of the correct datatype (e.g., an instance value is validated to be an integer or a float in the case of 'utc-millisec').
451
+
452
+ Additionally, JSON::Validator does not handle any json hyperschema attributes.