json-schema 0.9.5 → 0.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.textile CHANGED
@@ -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-0.9.5.gem
21
+ $ gem install json-schema-0.9.6.gem
22
22
  </pre>
23
23
 
24
24
 
@@ -6,7 +6,7 @@ require 'digest/sha1'
6
6
  require 'date'
7
7
 
8
8
  module JSON
9
-
9
+
10
10
  class Schema
11
11
  class ValidationError < Exception
12
12
  attr_reader :fragments, :schema
@@ -18,30 +18,30 @@ module JSON
18
18
  super(message)
19
19
  end
20
20
  end
21
-
21
+
22
22
  class SchemaError < Exception
23
23
  end
24
-
24
+
25
25
  class JsonParseError < Exception
26
26
  end
27
-
27
+
28
28
  class Attribute
29
29
  def self.validate(current_schema, data, fragments, validator, options = {})
30
30
  end
31
-
31
+
32
32
  def self.build_fragment(fragments)
33
33
  "#/#{fragments.join('/')}"
34
34
  end
35
35
  end
36
-
36
+
37
37
  class Validator
38
38
  attr_accessor :attributes, :uri
39
-
39
+
40
40
  def initialize()
41
41
  @attributes = {}
42
42
  @uri = nil
43
43
  end
44
-
44
+
45
45
  def extend_schema_definition(schema_uri)
46
46
  u = URI.parse(schema_uri)
47
47
  validator = JSON::Validator.validators["#{u.scheme}://#{u.host}#{u.path}"]
@@ -50,11 +50,11 @@ module JSON
50
50
  end
51
51
  @attributes.merge!(validator.attributes)
52
52
  end
53
-
53
+
54
54
  def to_s
55
55
  "#{@uri.scheme}://#{uri.host}#{uri.path}"
56
56
  end
57
-
57
+
58
58
  def validate(current_schema, data, fragments)
59
59
  current_schema.schema.each do |attr_name,attribute|
60
60
 
@@ -66,7 +66,7 @@ module JSON
66
66
  end
67
67
  end
68
68
  end
69
-
69
+
70
70
 
71
71
  class Validator
72
72
 
@@ -80,7 +80,7 @@ module JSON
80
80
  @@default_validator = nil
81
81
  @@available_json_backends = []
82
82
  @@json_backend = nil
83
-
83
+
84
84
  def initialize(schema_data, data, opts={})
85
85
  @options = @@default_opts.clone.merge(opts)
86
86
 
@@ -101,11 +101,11 @@ module JSON
101
101
  @options[:version] = validator
102
102
  end
103
103
  @base_schema = initialize_schema(schema_data)
104
- @data = initialize_data(data)
104
+ @data = initialize_data(data)
105
105
  build_schemas(@base_schema)
106
- end
107
-
108
-
106
+ end
107
+
108
+
109
109
  # Run a simple true/false validation of data against a schema
110
110
  def validate()
111
111
  begin
@@ -117,20 +117,20 @@ module JSON
117
117
  end
118
118
  end
119
119
 
120
-
120
+
121
121
  def load_ref_schema(parent_schema,ref)
122
122
  uri = URI.parse(ref)
123
123
  if uri.relative?
124
124
  uri = parent_schema.uri.clone
125
-
125
+
126
126
  # Check for absolute path
127
127
  path = ref.split("#")[0]
128
-
128
+
129
129
  # This is a self reference and thus the schema does not need to be re-loaded
130
130
  if path.nil? || path == ''
131
131
  return
132
132
  end
133
-
133
+
134
134
  if path && path[0,1] == '/'
135
135
  uri.path = Pathname.new(path).cleanpath.to_s
136
136
  else
@@ -138,7 +138,7 @@ module JSON
138
138
  end
139
139
  uri.fragment = ''
140
140
  end
141
-
141
+
142
142
  if Validator.schemas[uri.to_s].nil?
143
143
  begin
144
144
  schema = JSON::Schema.new(JSON::Validator.parse(open(uri.to_s).read), uri, @options[:version])
@@ -153,33 +153,33 @@ module JSON
153
153
  end
154
154
  end
155
155
  end
156
-
157
-
156
+
157
+
158
158
  # Build all schemas with IDs, mapping out the namespace
159
- def build_schemas(parent_schema)
159
+ def build_schemas(parent_schema)
160
160
  # Build ref schemas if they exist
161
161
  if parent_schema.schema["$ref"]
162
162
  load_ref_schema(parent_schema, parent_schema.schema["$ref"])
163
163
  end
164
-
164
+
165
165
  # Check for schemas in union types
166
166
  ["type", "disallow"].each do |key|
167
167
  if parent_schema.schema[key] && parent_schema.schema[key].is_a?(Array)
168
168
  parent_schema.schema[key].each_with_index do |type,i|
169
- if type.is_a?(Hash)
169
+ if type.is_a?(Hash)
170
170
  handle_schema(parent_schema, type)
171
171
  end
172
172
  end
173
173
  end
174
174
  end
175
-
175
+
176
176
  # All properties are schemas
177
177
  if parent_schema.schema["properties"]
178
178
  parent_schema.schema["properties"].each do |k,v|
179
179
  handle_schema(parent_schema, v)
180
180
  end
181
181
  end
182
-
182
+
183
183
  # Items are always schemas
184
184
  if parent_schema.schema["items"]
185
185
  items = parent_schema.schema["items"].clone
@@ -192,16 +192,16 @@ module JSON
192
192
  handle_schema(parent_schema, item)
193
193
  end
194
194
  end
195
-
195
+
196
196
  # Each of these might be schemas
197
197
  ["additionalProperties", "additionalItems", "dependencies", "extends"].each do |key|
198
- if parent_schema.schema[key].is_a?(Hash)
198
+ if parent_schema.schema[key].is_a?(Hash)
199
199
  handle_schema(parent_schema, parent_schema.schema[key])
200
200
  end
201
201
  end
202
-
202
+
203
203
  end
204
-
204
+
205
205
  # Either load a reference schema or create a new schema
206
206
  def handle_schema(parent_schema, obj)
207
207
  schema_uri = parent_schema.uri.clone
@@ -211,8 +211,8 @@ module JSON
211
211
  end
212
212
  build_schemas(schema)
213
213
  end
214
-
215
-
214
+
215
+
216
216
  class << self
217
217
  def validate(schema, data,opts={})
218
218
  begin
@@ -223,50 +223,50 @@ module JSON
223
223
  return false
224
224
  end
225
225
  end
226
-
226
+
227
227
  def validate!(schema, data,opts={})
228
228
  validator = JSON::Validator.new(schema, data, opts)
229
229
  validator.validate
230
230
  return true
231
231
  end
232
232
  alias_method 'validate2', 'validate!'
233
-
233
+
234
234
  def clear_cache
235
235
  @@schemas = {} if @@cache_schemas == false
236
236
  end
237
-
237
+
238
238
  def schemas
239
239
  @@schemas
240
240
  end
241
-
241
+
242
242
  def add_schema(schema)
243
243
  @@schemas[schema.uri.to_s] = schema if @@schemas[schema.uri.to_s].nil?
244
244
  end
245
-
245
+
246
246
  def cache_schemas=(val)
247
247
  @@cache_schemas = val == true ? true : false
248
248
  end
249
-
249
+
250
250
  def validators
251
251
  @@validators
252
252
  end
253
-
253
+
254
254
  def default_validator
255
255
  @@default_validator
256
256
  end
257
-
257
+
258
258
  def register_validator(v)
259
259
  @@validators[v.to_s] = v
260
260
  end
261
-
261
+
262
262
  def register_default_validator(v)
263
263
  @@default_validator = v
264
264
  end
265
-
265
+
266
266
  def json_backend
267
267
  @@json_backend
268
268
  end
269
-
269
+
270
270
  def json_backend=(backend)
271
271
  backend = backend.to_s
272
272
  if @@available_json_backends.include?(backend)
@@ -275,7 +275,7 @@ module JSON
275
275
  raise JSON::Schema::JsonParseError.new("The JSON backend '#{backend}' could not be found.")
276
276
  end
277
277
  end
278
-
278
+
279
279
  def parse(s)
280
280
  case @@json_backend.to_s
281
281
  when 'json'
@@ -289,23 +289,37 @@ module JSON
289
289
  end
290
290
  end
291
291
  end
292
-
293
- if Gem.available?('json')
292
+
293
+
294
+ if begin
295
+ Gem::Specification::find_by_name('json')
296
+ rescue
297
+ Gem.available?('json')
298
+ end
294
299
  require 'json'
295
300
  @@available_json_backends << 'json'
296
301
  @@json_backend = 'json'
297
302
  end
298
-
299
- if Gem.available?('yajl-ruby')
303
+
304
+
305
+ if begin
306
+ Gem::Specification::find_by_name('yajl-ruby')
307
+ rescue
308
+ Gem.available?('yajl-ruby')
309
+ end
300
310
  require 'yajl'
301
311
  @@available_json_backends << 'yajl'
302
312
  @@json_backend = 'yajl'
303
313
  end
304
-
305
-
314
+
315
+
306
316
  private
307
317
 
308
- if Gem.available?('uuidtools')
318
+ if begin
319
+ Gem::Specification::find_by_name('uuidtools')
320
+ rescue
321
+ Gem.available?('uuidtools')
322
+ end
309
323
  require 'uuidtools'
310
324
  @@fake_uri_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
311
325
  else
@@ -359,12 +373,12 @@ module JSON
359
373
  Validator.add_schema(schema)
360
374
  else
361
375
  raise "Invalid schema - must be either a string or a hash"
362
- end
363
-
376
+ end
377
+
364
378
  schema
365
379
  end
366
-
367
-
380
+
381
+
368
382
  def initialize_data(data)
369
383
  # Parse the data, if any
370
384
  if data.is_a?(String)
@@ -384,6 +398,6 @@ module JSON
384
398
  end
385
399
  data
386
400
  end
387
-
401
+
388
402
  end
389
403
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- hash: 49
4
+ hash: 55
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 5
10
- version: 0.9.5
9
+ - 6
10
+ version: 0.9.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kenny Hoxworth
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-31 00:00:00 -04:00
18
+ date: 2011-06-24 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21