json-schema 0.1.14 → 0.2.0

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
@@ -4,7 +4,7 @@ This library is intended to provide Ruby with an interface for validating JSON o
4
4
 
5
5
  h2. Dependencies
6
6
 
7
- The JSON::Schema library depends on the ruby JSON gem (json). Support for YAJL and other Ruby parsers is planned.
7
+ The JSON::Schema library has no dependencies if the validation methods are called using Ruby objects. However, either the <code>json</code> or the <code>yajl-ruby</code> gem needs to be installed to validate JSON strings or files containing JSON data.
8
8
 
9
9
  h2. Installation
10
10
 
@@ -18,11 +18,11 @@ From the git repo:
18
18
 
19
19
  <pre>
20
20
  $ gem build json-schema.gemspec
21
- $ gem install json-schema-0.1.14.gem
21
+ $ gem install json-schema-0.2.0.gem
22
22
  </pre>
23
23
 
24
24
 
25
- h2. Basic Usage
25
+ h2. Usage
26
26
 
27
27
  Two base validation methods exist: <code>validate</code> and <code>validate!</code>. The first returns a boolean on whether a validation attempt passes and the latter will throw a <code>JSON::Schema::ValidationError</code> with an appropriate message/trace on where the validation failed.
28
28
 
@@ -56,7 +56,7 @@ h3. Validate a JSON string against a JSON schema file
56
56
  require 'rubygems'
57
57
  require 'json-schema'
58
58
 
59
- JSON::Validator.validate('schema.json', "{'a' : 5}")
59
+ JSON::Validator.validate('schema.json', '{"a" : 5}')
60
60
  </pre>
61
61
 
62
62
  h3. Validate a list of objects against a schema that represents the individual objects
@@ -144,6 +144,16 @@ JSON::Validator.validate(schema,data) # => false
144
144
  data = {"a" => 0, "b" => "taco"}
145
145
  JSON::Validator.validate(schema,data) # => false
146
146
  </pre>
147
+
148
+ h2. JSON Backends
149
+
150
+ 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.
151
+
152
+ 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:
153
+
154
+ <pre>
155
+ JSON::Validator.json_backend = :json
156
+ </pre>
147
157
 
148
158
 
149
159
  h2. Notes
data/lib/json-schema.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/json-schema"
2
2
 
3
+ require 'rubygems'
3
4
  require 'schema'
4
5
  require 'validator'
5
6
  Dir[File.join(File.dirname(__FILE__), "json-schema/attributes/*")].each {|file| require file }
@@ -1,7 +1,7 @@
1
1
  module JSON
2
2
  class Schema
3
3
  class RefAttribute < Attribute
4
- def self.validate(current_schema, data, fragments, validator, options = {})
4
+ def self.validate(current_schema, data, fragments, validator, options = {})
5
5
  temp_uri = URI.parse(current_schema.schema['$ref'])
6
6
  if temp_uri.relative?
7
7
  temp_uri = current_schema.uri.clone
@@ -1,5 +1,3 @@
1
- require 'rubygems'
2
- require 'json'
3
1
  require 'pathname'
4
2
 
5
3
  module JSON
@@ -45,11 +43,7 @@ module JSON
45
43
  parts.pop
46
44
  parts.join('/') + '/'
47
45
  end
48
-
49
- def to_s
50
- @schema.to_json
51
- end
52
-
46
+
53
47
 
54
48
  end
55
49
  end
@@ -22,6 +22,9 @@ module JSON
22
22
  class SchemaError < Exception
23
23
  end
24
24
 
25
+ class JsonParseError < Exception
26
+ end
27
+
25
28
  class Attribute
26
29
  def self.validate(current_schema, data, fragments, validator, options = {})
27
30
  end
@@ -54,8 +57,8 @@ module JSON
54
57
 
55
58
  def validate(current_schema, data, fragments)
56
59
  current_schema.schema.each do |attr_name,attribute|
57
- if @attributes.has_key?(attr_name)
58
- @attributes[attr_name].validate(current_schema, data, fragments, self)
60
+ if @attributes.has_key?(attr_name.to_s)
61
+ @attributes[attr_name.to_s].validate(current_schema, data, fragments, self)
59
62
  end
60
63
  end
61
64
  data
@@ -73,6 +76,8 @@ module JSON
73
76
  }
74
77
  @@validators = {}
75
78
  @@default_validator = nil
79
+ @@available_json_backends = []
80
+ @@json_backend = nil
76
81
 
77
82
  def initialize(schema_data, data, opts={})
78
83
  @options = @@default_opts.clone.merge(opts)
@@ -117,7 +122,7 @@ module JSON
117
122
 
118
123
  if Validator.schemas[uri.to_s].nil?
119
124
  begin
120
- schema = JSON::Schema.new(JSON.parse(open(uri.to_s).read), uri)
125
+ schema = JSON::Schema.new(JSON::Validator.parse(open(uri.to_s).read), uri)
121
126
  Validator.add_schema(schema)
122
127
  build_schemas(schema)
123
128
  rescue JSON::ParserError
@@ -236,8 +241,45 @@ module JSON
236
241
  def register_default_validator(v)
237
242
  @@default_validator = v
238
243
  end
244
+
245
+ def json_backend
246
+ @@json_backend
247
+ end
248
+
249
+ def json_backend=(backend)
250
+ backend = backend.to_s
251
+ if @@available_json_backend.include?(backend)
252
+ @@json_backend = backend
253
+ else
254
+ raise JSON::Schema::JsonParseError.new("The JSON backend '#{backend}' could not be found.")
255
+ end
256
+ end
257
+
258
+ def parse(s)
259
+ case @@json_backend.to_s
260
+ when 'json'
261
+ JSON.parse(s)
262
+ when 'yajl'
263
+ json = StringIO.new(s)
264
+ parser = Yajl::Parser.new
265
+ parser.parse(json)
266
+ else
267
+ raise JSON::Schema::JsonParseError.new("No supported JSON parsers found. The following parsers are suported:\n * yajl-ruby\n * json")
268
+ end
269
+ end
239
270
  end
240
271
 
272
+ if Gem.available?('json')
273
+ require 'json'
274
+ @@available_json_backends << 'json'
275
+ @@json_backend = 'json'
276
+ end
277
+
278
+ if Gem.available?('yajl-ruby')
279
+ require 'yajl'
280
+ @@available_json_backends << 'yajl'
281
+ @@json_backend = 'yajl'
282
+ end
241
283
 
242
284
 
243
285
  private
@@ -247,7 +289,11 @@ module JSON
247
289
  begin
248
290
  # Build a fake URI for this
249
291
  schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema)}")
250
- schema = JSON::Schema.new(JSON.parse(schema),schema_uri)
292
+ schema = JSON::Validator.parse(schema)
293
+ if @options[:list]
294
+ schema = {"type" => "array", "items" => schema}
295
+ end
296
+ schema = JSON::Schema.new(schema,schema_uri)
251
297
  Validator.add_schema(schema)
252
298
  rescue
253
299
  # Build a uri for it
@@ -261,27 +307,26 @@ module JSON
261
307
  end
262
308
  end
263
309
  if Validator.schemas[schema_uri.to_s].nil?
264
- schema = JSON::Schema.new(JSON.parse(open(schema_uri.to_s).read),schema_uri)
310
+ schema = JSON::Validator.parse(open(schema_uri.to_s).read)
311
+ if @options[:list]
312
+ schema = {"type" => "array", "items" => schema}
313
+ end
314
+ schema = JSON::Schema.new(schema,schema_uri)
265
315
  Validator.add_schema(schema)
266
316
  else
267
317
  schema = Validator.schemas[schema_uri.to_s]
268
318
  end
269
319
  end
270
320
  elsif schema.is_a?(Hash)
271
- schema = schema.to_json
272
- schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema)}")
273
- schema = JSON::Schema.new(JSON.parse(schema),schema_uri)
321
+ if @options[:list]
322
+ schema = {"type" => "array", "items" => schema}
323
+ end
324
+ schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema.inspect)}")
325
+ schema = JSON::Schema.new(schema,schema_uri)
274
326
  Validator.add_schema(schema)
275
327
  else
276
328
  raise "Invalid schema - must be either a string or a hash"
277
- end
278
-
279
- if @options[:list]
280
- inter_json = {:type => "array", :items => { "$ref" => schema.uri.to_s }}.to_json
281
- wrapper_schema = JSON::Schema.new(JSON.parse(inter_json),URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(inter_json)}"))
282
- build_schemas(schema)
283
- schema = wrapper_schema
284
- end
329
+ end
285
330
 
286
331
  schema
287
332
  end
@@ -291,7 +336,7 @@ module JSON
291
336
  # Parse the data, if any
292
337
  if data.is_a?(String)
293
338
  begin
294
- data = JSON.parse(data)
339
+ data = JSON::Validator.parse(data)
295
340
  rescue
296
341
  json_uri = URI.parse(data)
297
342
  if json_uri.relative?
@@ -301,7 +346,7 @@ module JSON
301
346
  schema_uri = URI.parse("file://#{Dir.pwd}/#{data}")
302
347
  end
303
348
  end
304
- data = JSON.parse(open(json_uri.to_s).read)
349
+ data = JSON::Validator.parse(open(json_uri.to_s).read)
305
350
  end
306
351
  end
307
352
  data
data/test/test_files.rb CHANGED
@@ -2,43 +2,42 @@ require 'test/unit'
2
2
  require File.dirname(__FILE__) + '/../lib/json-schema'
3
3
 
4
4
  class JSONSchemaTest < Test::Unit::TestCase
5
- def test_schema_from_file
6
- data = {"a" => 5}
7
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
8
- data = {"a" => "bad"}
9
- assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
10
- end
11
5
 
12
- def test_data_from_file
13
- schema = {"type" => "object", "properties" => {"a" => {"type" => "integer"}}}
14
- assert(JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/good_data_1.json")))
15
- assert(!JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
16
- end
6
+ #
7
+ # These tests are ONLY run if there is an appropriate JSON backend parser available
8
+ #
17
9
 
18
- def test_both_from_file
19
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/good_data_1.json")))
20
- assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
10
+ def test_schema_from_file
11
+ if JSON::Validator.json_backend != nil
12
+ data = {"a" => 5}
13
+ assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
14
+ data = {"a" => "bad"}
15
+ assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
16
+ end
21
17
  end
22
18
 
23
- def test_invalid_schema
24
- data = {}
25
- assert_raise JSON::ParserError do
26
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/invalid_schema_1.json"),data))
19
+ def test_data_from_file
20
+ if JSON::Validator.json_backend != nil
21
+ schema = {"type" => "object", "properties" => {"a" => {"type" => "integer"}}}
22
+ assert(JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/good_data_1.json")))
23
+ assert(!JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
27
24
  end
28
25
  end
29
26
 
30
- def test_invalid_data
31
- schema = {}
32
- assert_raise JSON::ParserError do
33
- assert(JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/invalid_data_1.json")))
27
+ def test_both_from_file
28
+ if JSON::Validator.json_backend != nil
29
+ assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/good_data_1.json")))
30
+ assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
34
31
  end
35
32
  end
36
-
33
+
37
34
  def test_file_ref
38
- data = {"b" => {"a" => 5}}
39
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_2.json"),data))
35
+ if JSON::Validator.json_backend != nil
36
+ data = {"b" => {"a" => 5}}
37
+ assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_2.json"),data))
40
38
 
41
- data = {"b" => {"a" => "boo"}}
42
- assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
39
+ data = {"b" => {"a" => "boo"}}
40
+ assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
41
+ end
43
42
  end
44
43
  end
@@ -759,12 +759,11 @@ class JSONSchemaDraft3Test < Test::Unit::TestCase
759
759
  assert(JSON::Validator.validate(schema,data,:list => true))
760
760
  assert(!JSON::Validator.validate(schema,data))
761
761
 
762
- data = [{"a" => 1},{"b" => 2},{"a" => 3}]
763
- assert(!JSON::Validator.validate(schema,data,:list => true))
764
-
765
762
  data = {"a" => 1}
766
763
  assert(!JSON::Validator.validate(schema,data,:list => true))
767
764
 
765
+ data = [{"a" => 1},{"b" => 2},{"a" => 3}]
766
+ assert(!JSON::Validator.validate(schema,data,:list => true))
768
767
  end
769
768
 
770
769
 
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: 7
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 14
10
- version: 0.1.14
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kenny Hoxworth
@@ -15,23 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-08 00:00:00 -05:00
18
+ date: 2011-03-09 00:00:00 -05:00
19
19
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: json
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
33
- type: :runtime
34
- version_requirements: *id001
20
+ dependencies: []
21
+
35
22
  description:
36
23
  email: hoxworth@gmail.com
37
24
  executables: []