json-schema 0.1.13 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ module JSON
2
+ class Schema
3
+
4
+ class Draft3 < Validator
5
+ def initialize
6
+ super
7
+ @attributes = {
8
+ "type" => JSON::Schema::TypeAttribute,
9
+ "disallow" => JSON::Schema::DisallowAttribute,
10
+ "format" => JSON::Schema::FormatAttribute,
11
+ "maximum" => JSON::Schema::MaximumAttribute,
12
+ "minimum" => JSON::Schema::MinimumAttribute,
13
+ "minItems" => JSON::Schema::MinItemsAttribute,
14
+ "maxItems" => JSON::Schema::MaxItemsAttribute,
15
+ "uniqueItems" => JSON::Schema::UniqueItemsAttribute,
16
+ "minLength" => JSON::Schema::MinLengthAttribute,
17
+ "maxLength" => JSON::Schema::MaxLengthAttribute,
18
+ "divisibleBy" => JSON::Schema::DivisibleByAttribute,
19
+ "enum" => JSON::Schema::EnumAttribute,
20
+ "properties" => JSON::Schema::PropertiesAttribute,
21
+ "pattern" => JSON::Schema::PatternAttribute,
22
+ "patternProperties" => JSON::Schema::PatternPropertiesAttribute,
23
+ "additionalProperties" => JSON::Schema::AdditionalPropertiesAttribute,
24
+ "items" => JSON::Schema::ItemsAttribute,
25
+ "additionalItems" => JSON::Schema::AdditionalItemsAttribute,
26
+ "dependencies" => JSON::Schema::DependenciesAttribute,
27
+ "extends" => JSON::Schema::ExtendsAttribute,
28
+ "$ref" => JSON::Schema::RefAttribute
29
+ }
30
+ @uri = URI.parse("http://json-schema.org/draft-03/schema#")
31
+ end
32
+
33
+ JSON::Validator.register_validator(self.new)
34
+ JSON::Validator.register_default_validator(self.new)
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,68 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class BitwiseAndAttribute < JSON::Schema::Attribute
5
+ def self.validate(current_schema, data, fragments, validator, options = {})
6
+ if data.is_a?(Integer) && data & current_schema.schema['bitwise-and'].to_i == 0
7
+ message = "The property '#{build_fragment(fragments)}' did not evaluate to true when bitwise-AND'd with #{current_schema.schema['bitwise-or']}"
8
+ raise JSON::Schema::ValidationError.new(message, fragments, current_schema)
9
+ end
10
+ end
11
+ end
12
+
13
+ class ExtendedSchema < JSON::Schema::Validator
14
+ def initialize
15
+ super
16
+ extend_schema_definition("http://json-schema.org/draft-03/schema#")
17
+ @attributes["bitwise-and"] = BitwiseAndAttribute
18
+ @uri = URI.parse("http://test.com/test.json")
19
+ end
20
+
21
+ JSON::Validator.register_validator(self.new)
22
+ end
23
+
24
+ class JSONSchemaTestExtendedSchema < Test::Unit::TestCase
25
+ def test_schema_from_file
26
+ schema = {
27
+ "$schema" => "http://json-schema.org/draft-03/schema#",
28
+ "properties" => {
29
+ "a" => {
30
+ "bitwise-and" => 1
31
+ },
32
+ "b" => {
33
+ "type" => "string"
34
+ }
35
+ }
36
+ }
37
+
38
+ data = {"a" => 0, "b" => "taco"}
39
+ assert(JSON::Validator.validate(schema,data))
40
+ data = {"a" => 1, "b" => "taco"}
41
+ assert(JSON::Validator.validate(schema,data))
42
+ data = {"a" => 1, "b" => 5}
43
+ assert(!JSON::Validator.validate(schema,data))
44
+
45
+ schema = {
46
+ "$schema" => "http://test.com/test.json",
47
+ "properties" => {
48
+ "a" => {
49
+ "bitwise-and" => 1
50
+ },
51
+ "b" => {
52
+ "type" => "string"
53
+ }
54
+ }
55
+ }
56
+
57
+ data = {
58
+ "a" => 0
59
+ }
60
+
61
+ data = {"a" => 1, "b" => "taco"}
62
+ assert(JSON::Validator.validate(schema,data))
63
+ data = {"a" => 0, "b" => "taco"}
64
+ assert(!JSON::Validator.validate(schema,data))
65
+ data = {"a" => 1, "b" => 5}
66
+ assert(!JSON::Validator.validate(schema,data))
67
+ end
68
+ end
@@ -1,10 +1,11 @@
1
1
  require 'test/unit'
2
2
  require File.dirname(__FILE__) + '/../lib/json-schema'
3
3
 
4
- class JSONSchemaTest < Test::Unit::TestCase
4
+ class JSONSchemaDraft3Test < Test::Unit::TestCase
5
5
  def test_types
6
6
  # Set up the default datatype
7
7
  schema = {
8
+ "$schema" => "http://json-schema.org/draft-03/schema#",
8
9
  "properties" => {
9
10
  "a" => {}
10
11
  }
@@ -168,6 +169,7 @@ class JSONSchemaTest < Test::Unit::TestCase
168
169
  def test_required
169
170
  # Set up the default datatype
170
171
  schema = {
172
+ "$schema" => "http://json-schema.org/draft-03/schema#",
171
173
  "properties" => {
172
174
  "a" => {"required" => true}
173
175
  }
@@ -179,6 +181,7 @@ class JSONSchemaTest < Test::Unit::TestCase
179
181
  assert(JSON::Validator.validate(schema,data))
180
182
 
181
183
  schema = {
184
+ "$schema" => "http://json-schema.org/draft-03/schema#",
182
185
  "properties" => {
183
186
  "a" => {"type" => "integer"}
184
187
  }
@@ -194,6 +197,7 @@ class JSONSchemaTest < Test::Unit::TestCase
194
197
  def test_minimum
195
198
  # Set up the default datatype
196
199
  schema = {
200
+ "$schema" => "http://json-schema.org/draft-03/schema#",
197
201
  "properties" => {
198
202
  "a" => {"minimum" => 5}
199
203
  }
@@ -244,6 +248,7 @@ class JSONSchemaTest < Test::Unit::TestCase
244
248
  def test_maximum
245
249
  # Set up the default datatype
246
250
  schema = {
251
+ "$schema" => "http://json-schema.org/draft-03/schema#",
247
252
  "properties" => {
248
253
  "a" => {"maximum" => 5}
249
254
  }
@@ -293,6 +298,7 @@ class JSONSchemaTest < Test::Unit::TestCase
293
298
  def test_min_items
294
299
  # Set up the default datatype
295
300
  schema = {
301
+ "$schema" => "http://json-schema.org/draft-03/schema#",
296
302
  "properties" => {
297
303
  "a" => {"minItems" => 1}
298
304
  }
@@ -319,6 +325,7 @@ class JSONSchemaTest < Test::Unit::TestCase
319
325
  def test_max_items
320
326
  # Set up the default datatype
321
327
  schema = {
328
+ "$schema" => "http://json-schema.org/draft-03/schema#",
322
329
  "properties" => {
323
330
  "a" => {"maxItems" => 1}
324
331
  }
@@ -345,6 +352,7 @@ class JSONSchemaTest < Test::Unit::TestCase
345
352
  def test_unique_items
346
353
  # Set up the default datatype
347
354
  schema = {
355
+ "$schema" => "http://json-schema.org/draft-03/schema#",
348
356
  "properties" => {
349
357
  "a" => {"uniqueItems" => true}
350
358
  }
@@ -416,6 +424,7 @@ class JSONSchemaTest < Test::Unit::TestCase
416
424
  def test_pattern
417
425
  # Set up the default datatype
418
426
  schema = {
427
+ "$schema" => "http://json-schema.org/draft-03/schema#",
419
428
  "properties" => {
420
429
  "a" => {"pattern" => "\\d+ taco"}
421
430
  }
@@ -429,18 +438,19 @@ class JSONSchemaTest < Test::Unit::TestCase
429
438
  data["a"] = "156 taco bell"
430
439
  assert(JSON::Validator.validate(schema,data))
431
440
 
432
- data["a"] = "taco"
433
- assert(!JSON::Validator.validate(schema,data))
434
-
435
441
  # Test a non-string
436
442
  data["a"] = 5
437
443
  assert(JSON::Validator.validate(schema,data))
444
+
445
+ data["a"] = "taco"
446
+ assert(!JSON::Validator.validate(schema,data))
438
447
  end
439
448
 
440
449
 
441
450
  def test_min_length
442
451
  # Set up the default datatype
443
452
  schema = {
453
+ "$schema" => "http://json-schema.org/draft-03/schema#",
444
454
  "properties" => {
445
455
  "a" => {"minLength" => 1}
446
456
  }
@@ -466,6 +476,7 @@ class JSONSchemaTest < Test::Unit::TestCase
466
476
  def test_max_length
467
477
  # Set up the default datatype
468
478
  schema = {
479
+ "$schema" => "http://json-schema.org/draft-03/schema#",
469
480
  "properties" => {
470
481
  "a" => {"maxLength" => 1}
471
482
  }
@@ -491,6 +502,7 @@ class JSONSchemaTest < Test::Unit::TestCase
491
502
  def test_enum
492
503
  # Set up the default datatype
493
504
  schema = {
505
+ "$schema" => "http://json-schema.org/draft-03/schema#",
494
506
  "properties" => {
495
507
  "a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}]}
496
508
  }
@@ -526,6 +538,7 @@ class JSONSchemaTest < Test::Unit::TestCase
526
538
  def test_divisible_by
527
539
  # Set up the default datatype
528
540
  schema = {
541
+ "$schema" => "http://json-schema.org/draft-03/schema#",
529
542
  "properties" => {
530
543
  "a" => {"divisibleBy" => 1.1}
531
544
  }
@@ -559,6 +572,7 @@ class JSONSchemaTest < Test::Unit::TestCase
559
572
  def test_disallow
560
573
  # Set up the default datatype
561
574
  schema = {
575
+ "$schema" => "http://json-schema.org/draft-03/schema#",
562
576
  "properties" => {
563
577
  "a" => {"disallow" => "integer"}
564
578
  }
@@ -592,12 +606,14 @@ class JSONSchemaTest < Test::Unit::TestCase
592
606
 
593
607
  def test_extends
594
608
  schema = {
609
+ "$schema" => "http://json-schema.org/draft-03/schema#",
595
610
  "properties" => {
596
611
  "a" => { "type" => "integer"}
597
612
  }
598
613
  }
599
614
 
600
615
  schema2 = {
616
+ "$schema" => "http://json-schema.org/draft-03/schema#",
601
617
  "properties" => {
602
618
  "a" => { "maximum" => 5 }
603
619
  }
@@ -618,6 +634,7 @@ class JSONSchemaTest < Test::Unit::TestCase
618
634
  def test_pattern_properties
619
635
  # Set up the default datatype
620
636
  schema = {
637
+ "$schema" => "http://json-schema.org/draft-03/schema#",
621
638
  "patternProperties" => {
622
639
  "\\d+ taco" => {"type" => "integer"}
623
640
  }
@@ -638,6 +655,7 @@ class JSONSchemaTest < Test::Unit::TestCase
638
655
  def test_additional_properties
639
656
  # Test no additional properties allowed
640
657
  schema = {
658
+ "$schema" => "http://json-schema.org/draft-03/schema#",
641
659
  "properties" => {
642
660
  "a" => { "type" => "integer" }
643
661
  },
@@ -661,6 +679,7 @@ class JSONSchemaTest < Test::Unit::TestCase
661
679
 
662
680
  # Make sure this works with pattern properties set, too
663
681
  schema = {
682
+ "$schema" => "http://json-schema.org/draft-03/schema#",
664
683
  "patternProperties" => {
665
684
  "\\d+ taco" => {"type" => "integer"}
666
685
  },
@@ -680,6 +699,7 @@ class JSONSchemaTest < Test::Unit::TestCase
680
699
 
681
700
  def test_items
682
701
  schema = {
702
+ "$schema" => "http://json-schema.org/draft-03/schema#",
683
703
  "items" => { "type" => "integer" }
684
704
  }
685
705
 
@@ -689,6 +709,7 @@ class JSONSchemaTest < Test::Unit::TestCase
689
709
  assert(!JSON::Validator.validate(schema,data))
690
710
 
691
711
  schema = {
712
+ "$schema" => "http://json-schema.org/draft-03/schema#",
692
713
  "items" => [
693
714
  {"type" => "integer"},
694
715
  {"type" => "string"}
@@ -703,6 +724,7 @@ class JSONSchemaTest < Test::Unit::TestCase
703
724
  assert(!JSON::Validator.validate(schema,data))
704
725
 
705
726
  schema = {
727
+ "$schema" => "http://json-schema.org/draft-03/schema#",
706
728
  "items" => [
707
729
  {"type" => "integer"},
708
730
  {"type" => "string"}
@@ -715,7 +737,7 @@ class JSONSchemaTest < Test::Unit::TestCase
715
737
  data = [1,"string",3]
716
738
  assert(!JSON::Validator.validate(schema,data))
717
739
 
718
- schema = {"items" => [{"type" => "integer"},{"type" => "string"}],"additionalItems" => {"type" => "integer"}}
740
+ schema = {"$schema" => "http://json-schema.org/draft-03/schema#","items" => [{"type" => "integer"},{"type" => "string"}],"additionalItems" => {"type" => "integer"}}
719
741
 
720
742
  data = [1,"string"]
721
743
  assert(JSON::Validator.validate(schema,data))
@@ -728,6 +750,7 @@ class JSONSchemaTest < Test::Unit::TestCase
728
750
 
729
751
  def test_list_option
730
752
  schema = {
753
+ "$schema" => "http://json-schema.org/draft-03/schema#",
731
754
  "type" => "object",
732
755
  "properties" => { "a" => {"type" => "integer", "required" => true} }
733
756
  }
@@ -747,6 +770,7 @@ class JSONSchemaTest < Test::Unit::TestCase
747
770
 
748
771
  def test_self_reference
749
772
  schema = {
773
+ "$schema" => "http://json-schema.org/draft-03/schema#",
750
774
  "type" => "object",
751
775
  "properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}}
752
776
  }
@@ -760,6 +784,7 @@ class JSONSchemaTest < Test::Unit::TestCase
760
784
 
761
785
  def test_format_ipv4
762
786
  schema = {
787
+ "$schema" => "http://json-schema.org/draft-03/schema#",
763
788
  "type" => "object",
764
789
  "properties" => { "a" => {"type" => "string", "format" => "ip-address"}}
765
790
  }
@@ -782,6 +807,7 @@ class JSONSchemaTest < Test::Unit::TestCase
782
807
 
783
808
  def test_format_ipv6
784
809
  schema = {
810
+ "$schema" => "http://json-schema.org/draft-03/schema#",
785
811
  "type" => "object",
786
812
  "properties" => { "a" => {"type" => "string", "format" => "ipv6"}}
787
813
  }
@@ -804,6 +830,7 @@ class JSONSchemaTest < Test::Unit::TestCase
804
830
 
805
831
  def test_format_time
806
832
  schema = {
833
+ "$schema" => "http://json-schema.org/draft-03/schema#",
807
834
  "type" => "object",
808
835
  "properties" => { "a" => {"type" => "string", "format" => "time"}}
809
836
  }
@@ -829,6 +856,7 @@ class JSONSchemaTest < Test::Unit::TestCase
829
856
 
830
857
  def test_format_date
831
858
  schema = {
859
+ "$schema" => "http://json-schema.org/draft-03/schema#",
832
860
  "type" => "object",
833
861
  "properties" => { "a" => {"type" => "string", "format" => "date"}}
834
862
  }
@@ -849,6 +877,7 @@ class JSONSchemaTest < Test::Unit::TestCase
849
877
 
850
878
  def test_format_datetime
851
879
  schema = {
880
+ "$schema" => "http://json-schema.org/draft-03/schema#",
852
881
  "type" => "object",
853
882
  "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
854
883
  }
@@ -879,6 +908,7 @@ class JSONSchemaTest < Test::Unit::TestCase
879
908
  data2 = {"a" => 5}
880
909
 
881
910
  schema = {
911
+ "$schema" => "http://json-schema.org/draft-03/schema#",
882
912
  "type" => "object",
883
913
  "properties" => { "a" => {"format" => "regex"}}
884
914
  }
@@ -886,6 +916,7 @@ class JSONSchemaTest < Test::Unit::TestCase
886
916
  assert(!JSON::Validator.validate(schema,data2))
887
917
 
888
918
  schema = {
919
+ "$schema" => "http://json-schema.org/draft-03/schema#",
889
920
  "type" => "object",
890
921
  "properties" => { "a" => {"format" => "color"}}
891
922
  }
@@ -893,6 +924,7 @@ class JSONSchemaTest < Test::Unit::TestCase
893
924
  assert(!JSON::Validator.validate(schema,data2))
894
925
 
895
926
  schema = {
927
+ "$schema" => "http://json-schema.org/draft-03/schema#",
896
928
  "type" => "object",
897
929
  "properties" => { "a" => {"format" => "style"}}
898
930
  }
@@ -900,6 +932,7 @@ class JSONSchemaTest < Test::Unit::TestCase
900
932
  assert(!JSON::Validator.validate(schema,data2))
901
933
 
902
934
  schema = {
935
+ "$schema" => "http://json-schema.org/draft-03/schema#",
903
936
  "type" => "object",
904
937
  "properties" => { "a" => {"format" => "phone"}}
905
938
  }
@@ -907,6 +940,7 @@ class JSONSchemaTest < Test::Unit::TestCase
907
940
  assert(!JSON::Validator.validate(schema,data2))
908
941
 
909
942
  schema = {
943
+ "$schema" => "http://json-schema.org/draft-03/schema#",
910
944
  "type" => "object",
911
945
  "properties" => { "a" => {"format" => "uri"}}
912
946
  }
@@ -914,6 +948,7 @@ class JSONSchemaTest < Test::Unit::TestCase
914
948
  assert(!JSON::Validator.validate(schema,data2))
915
949
 
916
950
  schema = {
951
+ "$schema" => "http://json-schema.org/draft-03/schema#",
917
952
  "type" => "object",
918
953
  "properties" => { "a" => {"format" => "email"}}
919
954
  }
@@ -921,6 +956,7 @@ class JSONSchemaTest < Test::Unit::TestCase
921
956
  assert(!JSON::Validator.validate(schema,data2))
922
957
 
923
958
  schema = {
959
+ "$schema" => "http://json-schema.org/draft-03/schema#",
924
960
  "type" => "object",
925
961
  "properties" => { "a" => {"format" => "host-name"}}
926
962
  }
@@ -935,6 +971,7 @@ class JSONSchemaTest < Test::Unit::TestCase
935
971
  data3 = {"a" => 5.4}
936
972
 
937
973
  schema = {
974
+ "$schema" => "http://json-schema.org/draft-03/schema#",
938
975
  "type" => "object",
939
976
  "properties" => { "a" => {"format" => "utc-millisec"}}
940
977
  }
@@ -945,5 +982,22 @@ class JSONSchemaTest < Test::Unit::TestCase
945
982
 
946
983
 
947
984
 
985
+ def test_schema
986
+ schema = {
987
+ "$schema" => "http://json-schema.org/THIS-IS-NOT-A-SCHEMA",
988
+ "type" => "object"
989
+ }
990
+
991
+ data = {"a" => "taco"}
992
+ assert(!JSON::Validator.validate(schema,data))
993
+
994
+ schema = {
995
+ "$schema" => "http://json-schema.org/draft-03/schema#",
996
+ "type" => "object"
997
+ }
998
+ assert(JSON::Validator.validate(schema,data))
999
+ end
1000
+
1001
+
948
1002
  end
949
1003
 
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: 1
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 13
10
- version: 0.1.13
9
+ - 14
10
+ version: 0.1.14
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-02-16 00:00:00 -05:00
18
+ date: 2011-03-08 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -41,13 +41,36 @@ extensions: []
41
41
  extra_rdoc_files:
42
42
  - README.textile
43
43
  files:
44
+ - lib/json-schema/attributes/additionalitems.rb
45
+ - lib/json-schema/attributes/additionalproperties.rb
46
+ - lib/json-schema/attributes/dependencies.rb
47
+ - lib/json-schema/attributes/disallow.rb
48
+ - lib/json-schema/attributes/divisibleby.rb
49
+ - lib/json-schema/attributes/enum.rb
50
+ - lib/json-schema/attributes/extends.rb
51
+ - lib/json-schema/attributes/format.rb
52
+ - lib/json-schema/attributes/items.rb
53
+ - lib/json-schema/attributes/maximum.rb
54
+ - lib/json-schema/attributes/maxitems.rb
55
+ - lib/json-schema/attributes/maxlength.rb
56
+ - lib/json-schema/attributes/minimum.rb
57
+ - lib/json-schema/attributes/minitems.rb
58
+ - lib/json-schema/attributes/minlength.rb
59
+ - lib/json-schema/attributes/pattern.rb
60
+ - lib/json-schema/attributes/patternproperties.rb
61
+ - lib/json-schema/attributes/properties.rb
62
+ - lib/json-schema/attributes/ref.rb
63
+ - lib/json-schema/attributes/type.rb
64
+ - lib/json-schema/attributes/uniqueitems.rb
44
65
  - lib/json-schema/schema.rb
45
66
  - lib/json-schema/uri/file.rb
46
67
  - lib/json-schema/validator.rb
68
+ - lib/json-schema/validators/draft3.rb
47
69
  - lib/json-schema.rb
48
70
  - README.textile
71
+ - test/test_extended_schema.rb
49
72
  - test/test_files.rb
50
- - test/test_jsonschema.rb
73
+ - test/test_jsonschema_draft3.rb
51
74
  has_rdoc: true
52
75
  homepage: http://github.com/hoxworth/json-schema/tree/master
53
76
  licenses: []
@@ -83,5 +106,6 @@ signing_key:
83
106
  specification_version: 3
84
107
  summary: Ruby JSON Schema Validator
85
108
  test_files:
109
+ - test/test_extended_schema.rb
86
110
  - test/test_files.rb
87
- - test/test_jsonschema.rb
111
+ - test/test_jsonschema_draft3.rb