json-schema 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,8 +24,9 @@ require 'json-schema'
24
24
  JSON::Validator.validate('schema.json', 'data.json')
25
25
 
26
26
  schema = {
27
+ "type" => "object",
27
28
  "properties" => {
28
- "a" => {"type" => "integer"}
29
+ "a" => {"type" => "integer", "required" => true}
29
30
  }
30
31
  }
31
32
 
@@ -35,6 +36,9 @@ data = {
35
36
 
36
37
  JSON::Validator.validate(schema, data)
37
38
 
39
+ data = [data,data,data]
40
+ JSON::Validator.validate(schema, data, :list => true)
41
+
38
42
  data = {
39
43
  "a" => "taco"
40
44
  }
@@ -20,6 +20,9 @@ module JSON
20
20
 
21
21
  @@schemas = {}
22
22
  @@cache_schemas = false
23
+ @@default_opts = {
24
+ :list => false
25
+ }
23
26
 
24
27
  ValidationMethods = [
25
28
  "type",
@@ -45,7 +48,8 @@ module JSON
45
48
  ]
46
49
 
47
50
 
48
- def initialize(schema_data, data)
51
+ def initialize(schema_data, data, opts={})
52
+ @options = @@default_opts.clone.merge(opts)
49
53
  @base_schema = initialize_schema(schema_data)
50
54
  @data = initialize_data(data)
51
55
  build_schemas(@base_schema)
@@ -579,13 +583,13 @@ module JSON
579
583
 
580
584
 
581
585
  class << self
582
- def validate(schema, data)
583
- validator = JSON::Validator.new(schema, data)
586
+ def validate(schema, data,opts={})
587
+ validator = JSON::Validator.new(schema, data, opts)
584
588
  validator.validate
585
589
  end
586
590
 
587
- def validate2(schema, data)
588
- validator = JSON::Validator.new(schema, data)
591
+ def validate2(schema, data,opts={})
592
+ validator = JSON::Validator.new(schema, data, opts)
589
593
  validator.validate2
590
594
  end
591
595
 
@@ -615,8 +619,9 @@ module JSON
615
619
  if schema.is_a?(String)
616
620
  begin
617
621
  # Build a fake URI for this
618
- schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema)}.json")
622
+ schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema)}")
619
623
  schema = JSON::Schema.new(JSON.parse(schema),schema_uri)
624
+ Validator.add_schema(schema)
620
625
  rescue
621
626
  # Build a uri for it
622
627
  schema_uri = URI.parse(schema)
@@ -637,12 +642,19 @@ module JSON
637
642
  end
638
643
  elsif schema.is_a?(Hash)
639
644
  schema = schema.to_json
640
- schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema)}.json")
645
+ schema_uri = URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(schema)}")
641
646
  schema = JSON::Schema.new(JSON.parse(schema),schema_uri)
647
+ Validator.add_schema(schema)
642
648
  else
643
649
  raise "Invalid schema - must be either a string or a hash"
644
650
  end
645
651
 
652
+ if @options[:list]
653
+ inter_json = {:type => "array", :items => schema.schema}.to_json
654
+ wrapper_schema = JSON::Schema.new(JSON.parse(inter_json),URI.parse("file://#{Dir.pwd}/#{Digest::SHA1.hexdigest(inter_json)}"))
655
+ schema = wrapper_schema
656
+ end
657
+
646
658
  schema
647
659
  end
648
660
 
@@ -724,5 +724,25 @@ class JSONSchemaTest < Test::Unit::TestCase
724
724
  data = [1,"string","string"]
725
725
  assert(!JSON::Validator.validate(schema,data))
726
726
  end
727
+
728
+
729
+ def test_list_option
730
+ schema = {
731
+ "type" => "object",
732
+ "properties" => { "a" => {"type" => "integer", "required" => true} }
733
+ }
734
+
735
+ data = [{"a" => 1},{"a" => 2},{"a" => 3}]
736
+ assert(JSON::Validator.validate(schema,data,:list => true))
737
+ assert(!JSON::Validator.validate(schema,data))
738
+
739
+ data = [{"a" => 1},{"b" => 2},{"a" => 3}]
740
+ assert(!JSON::Validator.validate(schema,data,:list => true))
741
+
742
+ data = {"a" => 1}
743
+ assert(!JSON::Validator.validate(schema,data,:list => true))
744
+
745
+ end
746
+
727
747
  end
728
748
 
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: 21
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 7
10
- version: 0.1.7
9
+ - 8
10
+ version: 0.1.8
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: 2010-12-03 00:00:00 -05:00
18
+ date: 2010-12-14 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency