schemacop 2.4.7 → 3.0.0.rc0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +25 -1
  4. data/.travis.yml +2 -1
  5. data/CHANGELOG.md +8 -0
  6. data/README.md +41 -708
  7. data/README_V2.md +775 -0
  8. data/README_V3.md +683 -0
  9. data/Rakefile +8 -12
  10. data/VERSION +1 -1
  11. data/lib/schemacop.rb +35 -37
  12. data/lib/schemacop/base_schema.rb +37 -0
  13. data/lib/schemacop/railtie.rb +10 -0
  14. data/lib/schemacop/schema.rb +1 -60
  15. data/lib/schemacop/schema2.rb +22 -0
  16. data/lib/schemacop/schema3.rb +21 -0
  17. data/lib/schemacop/scoped_env.rb +25 -13
  18. data/lib/schemacop/v2.rb +26 -0
  19. data/lib/schemacop/{caster.rb → v2/caster.rb} +16 -2
  20. data/lib/schemacop/{collector.rb → v2/collector.rb} +5 -2
  21. data/lib/schemacop/{dupper.rb → v2/dupper.rb} +1 -1
  22. data/lib/schemacop/{field_node.rb → v2/field_node.rb} +4 -3
  23. data/lib/schemacop/v2/node.rb +142 -0
  24. data/lib/schemacop/{node_resolver.rb → v2/node_resolver.rb} +1 -1
  25. data/lib/schemacop/{node_supporting_field.rb → v2/node_supporting_field.rb} +8 -10
  26. data/lib/schemacop/{node_supporting_type.rb → v2/node_supporting_type.rb} +6 -3
  27. data/lib/schemacop/{node_with_block.rb → v2/node_with_block.rb} +3 -2
  28. data/lib/schemacop/v2/root_node.rb +6 -0
  29. data/lib/schemacop/v2/validator/array_validator.rb +32 -0
  30. data/lib/schemacop/{validator → v2/validator}/boolean_validator.rb +1 -1
  31. data/lib/schemacop/v2/validator/float_validator.rb +7 -0
  32. data/lib/schemacop/v2/validator/hash_validator.rb +37 -0
  33. data/lib/schemacop/v2/validator/integer_validator.rb +7 -0
  34. data/lib/schemacop/{validator → v2/validator}/nil_validator.rb +1 -1
  35. data/lib/schemacop/v2/validator/number_validator.rb +21 -0
  36. data/lib/schemacop/v2/validator/object_validator.rb +29 -0
  37. data/lib/schemacop/v2/validator/string_validator.rb +39 -0
  38. data/lib/schemacop/{validator → v2/validator}/symbol_validator.rb +1 -1
  39. data/lib/schemacop/v3.rb +45 -0
  40. data/lib/schemacop/v3/all_of_node.rb +27 -0
  41. data/lib/schemacop/v3/any_of_node.rb +28 -0
  42. data/lib/schemacop/v3/array_node.rb +219 -0
  43. data/lib/schemacop/v3/boolean_node.rb +16 -0
  44. data/lib/schemacop/v3/combination_node.rb +45 -0
  45. data/lib/schemacop/v3/context.rb +17 -0
  46. data/lib/schemacop/v3/dsl_scope.rb +46 -0
  47. data/lib/schemacop/v3/global_context.rb +114 -0
  48. data/lib/schemacop/v3/hash_node.rb +217 -0
  49. data/lib/schemacop/v3/integer_node.rb +13 -0
  50. data/lib/schemacop/v3/is_not_node.rb +32 -0
  51. data/lib/schemacop/v3/node.rb +214 -0
  52. data/lib/schemacop/v3/node_registry.rb +49 -0
  53. data/lib/schemacop/v3/number_node.rb +18 -0
  54. data/lib/schemacop/v3/numeric_node.rb +76 -0
  55. data/lib/schemacop/v3/object_node.rb +40 -0
  56. data/lib/schemacop/v3/one_of_node.rb +28 -0
  57. data/lib/schemacop/v3/reference_node.rb +49 -0
  58. data/lib/schemacop/v3/result.rb +58 -0
  59. data/lib/schemacop/v3/string_node.rb +124 -0
  60. data/lib/schemacop/v3/symbol_node.rb +13 -0
  61. data/schemacop.gemspec +24 -27
  62. data/test/lib/test_helper.rb +152 -0
  63. data/test/schemas/nested/group.rb +6 -0
  64. data/test/schemas/user.rb +7 -0
  65. data/test/unit/schemacop/v2/casting_test.rb +120 -0
  66. data/test/unit/schemacop/v2/collector_test.rb +47 -0
  67. data/test/unit/schemacop/v2/custom_check_test.rb +95 -0
  68. data/test/unit/schemacop/v2/custom_if_test.rb +97 -0
  69. data/test/unit/schemacop/v2/defaults_test.rb +95 -0
  70. data/test/unit/schemacop/v2/empty_test.rb +16 -0
  71. data/test/unit/schemacop/v2/nil_dis_allow_test.rb +43 -0
  72. data/test/unit/schemacop/v2/node_resolver_test.rb +28 -0
  73. data/test/unit/schemacop/v2/short_forms_test.rb +351 -0
  74. data/test/unit/schemacop/v2/types_test.rb +88 -0
  75. data/test/unit/schemacop/v2/validator_array_test.rb +99 -0
  76. data/test/unit/schemacop/v2/validator_boolean_test.rb +17 -0
  77. data/test/unit/schemacop/v2/validator_float_test.rb +59 -0
  78. data/test/unit/schemacop/v2/validator_hash_test.rb +95 -0
  79. data/test/unit/schemacop/v2/validator_integer_test.rb +48 -0
  80. data/test/unit/schemacop/v2/validator_nil_test.rb +15 -0
  81. data/test/unit/schemacop/v2/validator_number_test.rb +62 -0
  82. data/test/unit/schemacop/v2/validator_object_test.rb +141 -0
  83. data/test/unit/schemacop/v2/validator_string_test.rb +78 -0
  84. data/test/unit/schemacop/v2/validator_symbol_test.rb +18 -0
  85. data/test/unit/schemacop/v3/all_of_node_test.rb +199 -0
  86. data/test/unit/schemacop/v3/any_of_node_test.rb +218 -0
  87. data/test/unit/schemacop/v3/array_node_test.rb +805 -0
  88. data/test/unit/schemacop/v3/boolean_node_test.rb +126 -0
  89. data/test/unit/schemacop/v3/global_context_test.rb +164 -0
  90. data/test/unit/schemacop/v3/hash_node_test.rb +775 -0
  91. data/test/unit/schemacop/v3/integer_node_test.rb +323 -0
  92. data/test/unit/schemacop/v3/is_not_node_test.rb +173 -0
  93. data/test/unit/schemacop/v3/node_test.rb +148 -0
  94. data/test/unit/schemacop/v3/number_node_test.rb +292 -0
  95. data/test/unit/schemacop/v3/object_node_test.rb +170 -0
  96. data/test/unit/schemacop/v3/one_of_node_test.rb +187 -0
  97. data/test/unit/schemacop/v3/reference_node_test.rb +351 -0
  98. data/test/unit/schemacop/v3/string_node_test.rb +334 -0
  99. data/test/unit/schemacop/v3/symbol_node_test.rb +75 -0
  100. metadata +152 -145
  101. data/doc/Schemacop.html +0 -146
  102. data/doc/Schemacop/ArrayValidator.html +0 -329
  103. data/doc/Schemacop/BooleanValidator.html +0 -145
  104. data/doc/Schemacop/Caster.html +0 -379
  105. data/doc/Schemacop/Collector.html +0 -787
  106. data/doc/Schemacop/Dupper.html +0 -214
  107. data/doc/Schemacop/Exceptions.html +0 -115
  108. data/doc/Schemacop/Exceptions/InvalidSchemaError.html +0 -124
  109. data/doc/Schemacop/Exceptions/ValidationError.html +0 -124
  110. data/doc/Schemacop/FieldNode.html +0 -421
  111. data/doc/Schemacop/FloatValidator.html +0 -158
  112. data/doc/Schemacop/HashValidator.html +0 -293
  113. data/doc/Schemacop/IntegerValidator.html +0 -158
  114. data/doc/Schemacop/NilValidator.html +0 -145
  115. data/doc/Schemacop/Node.html +0 -1438
  116. data/doc/Schemacop/NodeResolver.html +0 -258
  117. data/doc/Schemacop/NodeSupportingField.html +0 -590
  118. data/doc/Schemacop/NodeSupportingType.html +0 -612
  119. data/doc/Schemacop/NodeWithBlock.html +0 -289
  120. data/doc/Schemacop/NumberValidator.html +0 -232
  121. data/doc/Schemacop/ObjectValidator.html +0 -298
  122. data/doc/Schemacop/RootNode.html +0 -171
  123. data/doc/Schemacop/Schema.html +0 -699
  124. data/doc/Schemacop/StringValidator.html +0 -295
  125. data/doc/Schemacop/SymbolValidator.html +0 -145
  126. data/doc/ScopedEnv.html +0 -351
  127. data/doc/_index.html +0 -379
  128. data/doc/class_list.html +0 -51
  129. data/doc/css/common.css +0 -1
  130. data/doc/css/full_list.css +0 -58
  131. data/doc/css/style.css +0 -496
  132. data/doc/file.README.html +0 -833
  133. data/doc/file_list.html +0 -56
  134. data/doc/frames.html +0 -17
  135. data/doc/index.html +0 -833
  136. data/doc/inheritance.graphml +0 -524
  137. data/doc/inheritance.pdf +0 -825
  138. data/doc/js/app.js +0 -303
  139. data/doc/js/full_list.js +0 -216
  140. data/doc/js/jquery.js +0 -4
  141. data/doc/method_list.html +0 -587
  142. data/doc/top-level-namespace.html +0 -112
  143. data/lib/schemacop/node.rb +0 -139
  144. data/lib/schemacop/root_node.rb +0 -4
  145. data/lib/schemacop/validator/array_validator.rb +0 -30
  146. data/lib/schemacop/validator/float_validator.rb +0 -5
  147. data/lib/schemacop/validator/hash_validator.rb +0 -35
  148. data/lib/schemacop/validator/integer_validator.rb +0 -5
  149. data/lib/schemacop/validator/number_validator.rb +0 -19
  150. data/lib/schemacop/validator/object_validator.rb +0 -27
  151. data/lib/schemacop/validator/string_validator.rb +0 -37
  152. data/test/casting_test.rb +0 -118
  153. data/test/collector_test.rb +0 -45
  154. data/test/custom_check_test.rb +0 -93
  155. data/test/custom_if_test.rb +0 -95
  156. data/test/defaults_test.rb +0 -93
  157. data/test/empty_test.rb +0 -14
  158. data/test/nil_dis_allow_test.rb +0 -41
  159. data/test/node_resolver_test.rb +0 -26
  160. data/test/short_forms_test.rb +0 -349
  161. data/test/test_helper.rb +0 -13
  162. data/test/types_test.rb +0 -84
  163. data/test/validator_array_test.rb +0 -97
  164. data/test/validator_boolean_test.rb +0 -15
  165. data/test/validator_float_test.rb +0 -57
  166. data/test/validator_hash_test.rb +0 -93
  167. data/test/validator_integer_test.rb +0 -46
  168. data/test/validator_nil_test.rb +0 -13
  169. data/test/validator_number_test.rb +0 -60
  170. data/test/validator_object_test.rb +0 -139
  171. data/test/validator_string_test.rb +0 -76
  172. data/test/validator_symbol_test.rb +0 -16
@@ -0,0 +1,141 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorObjectTest < V2Test
6
+ # Classes used in the tests below
7
+ class User; end
8
+ class AdminUser; end
9
+ class SubUser < User; end
10
+
11
+ def test_basic
12
+ s = Schema.new do
13
+ type :object, classes: User
14
+ end
15
+
16
+ assert_nothing_raised { s.validate!(User.new) }
17
+ assert_verr { s.validate!(SubUser.new) }
18
+ assert_verr { s.validate!(AdminUser.new) }
19
+ end
20
+
21
+ # In modern versions of ruby, some classes such as Tempfile are not derived
22
+ # from Object but from BasicObject. Before the time of writing this test,
23
+ # the ObjectValidator only accepted subclasses of Object and classes like
24
+ # Tempfile did not work at all. This test ensures that this is working now.
25
+ def test_basic_object
26
+ refute Tempfile <= Object
27
+ assert Tempfile <= BasicObject
28
+
29
+ s = Schema.new do
30
+ type :object
31
+ end
32
+
33
+ assert_nothing_raised { s.validate!(Tempfile.new) }
34
+
35
+ s = Schema.new do
36
+ req :foo
37
+ end
38
+
39
+ assert_nothing_raised { s.validate!(foo: Tempfile.new) }
40
+
41
+ s = Schema.new do
42
+ req :foo, Tempfile
43
+ end
44
+
45
+ assert_nothing_raised { s.validate!(foo: Tempfile.new) }
46
+ assert_verr { s.validate!(foo: Time.new) }
47
+
48
+ s = Schema.new do
49
+ req :foo, :object, classes: Tempfile
50
+ end
51
+
52
+ assert_nothing_raised { s.validate!(foo: Tempfile.new) }
53
+ end
54
+
55
+ def test_multiple_classes_long
56
+ s = Schema.new do
57
+ type :object, classes: User
58
+ type :object, classes: AdminUser
59
+ end
60
+
61
+ assert_nothing_raised { s.validate!(User.new) }
62
+ assert_nothing_raised { s.validate!(AdminUser.new) }
63
+ assert_verr { s.validate!(SubUser.new) }
64
+ assert_verr { s.validate!([User.new, AdminUser.new]) }
65
+ end
66
+
67
+ def test_multiple_classes_short
68
+ s = Schema.new do
69
+ type :object, classes: [User, AdminUser]
70
+ end
71
+
72
+ assert_nothing_raised { s.validate!(User.new) }
73
+ assert_nothing_raised { s.validate!(AdminUser.new) }
74
+ assert_verr { s.validate!(SubUser.new) }
75
+ assert_verr { s.validate!([User.new, AdminUser.new]) }
76
+ end
77
+
78
+ def test_hash_of_objects
79
+ s = Schema.new do
80
+ req :user do
81
+ type :object, classes: User
82
+ end
83
+ opt :multitype do
84
+ type :object, classes: [User, AdminUser]
85
+ end
86
+ end
87
+
88
+ assert_nothing_raised { s.validate!(user: User.new) }
89
+ assert_nothing_raised { s.validate!(user: User.new, multitype: AdminUser.new) }
90
+ assert_nothing_raised { s.validate!(user: User.new, multitype: User.new) }
91
+ assert_verr { s.validate!(user: AdminUser.new) }
92
+ assert_verr { s.validate!(user: User.new, multitype: 12) }
93
+ assert_verr { s.validate!(user: User.new, multitype: self) }
94
+ end
95
+
96
+ def test_any
97
+ s = Schema.new do
98
+ type :object
99
+ end
100
+
101
+ assert_nothing_raised { s.validate!(User.new) }
102
+ assert_nothing_raised { s.validate!(123) }
103
+ assert_nothing_raised { s.validate!('sali') }
104
+ assert_nothing_raised { s.validate!(self) }
105
+ assert_nothing_raised { s.validate!(self.class) }
106
+ assert_verr { s.validate!(nil) }
107
+ end
108
+
109
+ def test_any_in_hash
110
+ s = Schema.new do
111
+ req :fld do
112
+ type :object
113
+ end
114
+ end
115
+
116
+ assert_nothing_raised { s.validate!(fld: User.new) }
117
+ assert_nothing_raised { s.validate!(fld: self) }
118
+ assert_verr { s.validate!(fld: nil) }
119
+ assert_verr { s.validate!({}) }
120
+ end
121
+
122
+ def test_strict_option
123
+ s = Schema.new do
124
+ req :o_strict do
125
+ type :object, classes: User, strict: true
126
+ end
127
+ opt :o_ns do
128
+ type :object, classes: User, strict: false
129
+ end
130
+ end
131
+
132
+ assert_nothing_raised { s.validate!(o_strict: User.new, o_ns: User.new) }
133
+ assert_nothing_raised { s.validate!(o_strict: User.new, o_ns: SubUser.new) }
134
+ assert_nothing_raised { s.validate!(o_strict: User.new) }
135
+ assert_verr { s.validate!(o_strict: SubUser.new) }
136
+ assert_verr { s.validate!(o_strict: User.new, o_ns: AdminUser.new) }
137
+ assert_verr { s.validate!(o_strict: SubUser.new, o_ns: User.new) }
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,78 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorStringTest < V2Test
6
+ def test_basic
7
+ s = Schema.new do
8
+ type :string
9
+ end
10
+ assert_nothing_raised { s.validate! '' }
11
+ assert_nothing_raised { s.validate! 'abc' }
12
+ assert_nothing_raised { s.validate! 123.to_s }
13
+ assert_nothing_raised { s.validate! inspect }
14
+ end
15
+
16
+ def test_option_min
17
+ s = Schema.new do
18
+ type :string, min: 3
19
+ end
20
+
21
+ assert_nothing_raised { s.validate!('abc') }
22
+ assert_nothing_raised { s.validate!('abcd') }
23
+ assert_verr { s.validate!('ab') }
24
+ assert_verr { s.validate!('') }
25
+ end
26
+
27
+ def test_option_max
28
+ s = Schema.new do
29
+ type :string, max: 5
30
+ end
31
+
32
+ assert_nothing_raised { s.validate!('') }
33
+ assert_nothing_raised { s.validate!('abcd') }
34
+ assert_nothing_raised { s.validate!('abcde') }
35
+ assert_verr { s.validate!('abcdef') }
36
+ end
37
+
38
+ def test_options_min_max
39
+ s = Schema.new do
40
+ type :string, min: 3, max: 5
41
+ end
42
+
43
+ assert_nothing_raised { s.validate!('abc') }
44
+ assert_nothing_raised { s.validate!('abcd') }
45
+ assert_nothing_raised { s.validate!('abcde') }
46
+ assert_verr { s.validate!('ab') }
47
+ assert_verr { s.validate!('abcdef') }
48
+ assert_verr { s.validate!('') }
49
+ end
50
+
51
+ def test_float_options
52
+ assert_raises Exceptions::InvalidSchemaError, 'String option :min must be an integer >= 0.' do
53
+ Schema.new do
54
+ type :string, min: 3.2
55
+ end
56
+ end
57
+
58
+ assert_raises Exceptions::InvalidSchemaError, 'String option :max must be an integer >= 0.' do
59
+ Schema.new do
60
+ type :string, max: 5.2
61
+ end
62
+ end
63
+
64
+ assert_raises Exceptions::InvalidSchemaError, 'String option :min must be an integer >= 0.' do
65
+ Schema.new do
66
+ type :string, min: 3.2, max: 5
67
+ end
68
+ end
69
+
70
+ assert_raises Exceptions::InvalidSchemaError, 'String option :max must be an integer >= 0.' do
71
+ Schema.new do
72
+ type :string, min: 3, max: 5.2
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V2
5
+ class ValidatorSymbolTest < V2Test
6
+ def test_basic
7
+ s = Schema.new do
8
+ type :symbol
9
+ end
10
+
11
+ assert_nothing_raised { s.validate!(:good) }
12
+ assert_nothing_raised { s.validate!('-+/'.to_sym) }
13
+ assert_verr { s.validate!('bad') }
14
+ assert_verr { s.validate!(456) }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,199 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V3
5
+ class AllOfNodeTest < V3Test
6
+ def test_optional
7
+ schema :all_of do
8
+ str min_length: 2
9
+ str max_length: 4
10
+ end
11
+
12
+ assert_validation(nil)
13
+ assert_validation('12')
14
+ assert_validation('1234')
15
+ assert_validation('1') do
16
+ error '/', 'Does not match all allOf conditions.'
17
+ end
18
+ assert_validation('12345') do
19
+ error '/', 'Does not match all allOf conditions.'
20
+ end
21
+ end
22
+
23
+ def test_required
24
+ schema :all_of, required: true do
25
+ int minimum: 2
26
+ int maximum: 4
27
+ end
28
+
29
+ assert_validation(2)
30
+ assert_validation(3)
31
+ assert_validation(4)
32
+
33
+ assert_validation(5) do
34
+ error '/', 'Does not match all allOf conditions.'
35
+ end
36
+ assert_validation(1) do
37
+ error '/', 'Does not match all allOf conditions.'
38
+ end
39
+ assert_validation({}) do
40
+ error '/', 'Does not match all allOf conditions.'
41
+ end
42
+ assert_validation(42) do
43
+ error '/', 'Does not match all allOf conditions.'
44
+ end
45
+ end
46
+
47
+ def test_nested
48
+ schema :all_of do
49
+ hsh additional_properties: true do
50
+ all_of! :foo do
51
+ int minimum: 2
52
+ int maximum: 4
53
+ end
54
+ end
55
+ hsh additional_properties: true do
56
+ any_of! :bar do
57
+ int minimum: 6
58
+ int maximum: 8
59
+ end
60
+ end
61
+ end
62
+
63
+ assert_validation(foo: 2, bar: 7)
64
+ assert_validation(foo: 5, bar: 7) do
65
+ error '/', 'Does not match all allOf conditions.'
66
+ end
67
+ assert_validation(foo: 5) do
68
+ error '/', 'Does not match all allOf conditions.'
69
+ end
70
+ end
71
+
72
+ def test_simple_casting
73
+ schema :all_of do
74
+ str format: :integer
75
+ end
76
+
77
+ assert_validation(nil)
78
+ assert_validation('1')
79
+ assert_validation('Foo') do
80
+ error '/', 'Does not match all allOf conditions.'
81
+ end
82
+
83
+ assert_cast('1', 1)
84
+ assert_cast('1337', 1337)
85
+ end
86
+
87
+ def test_casting_with_conditions
88
+ schema :all_of do
89
+ str format: :integer
90
+ str min_length: 2
91
+ str max_length: 3
92
+ end
93
+
94
+ assert_validation(nil)
95
+ assert_validation('12')
96
+ assert_validation('123')
97
+
98
+ assert_validation('1') do
99
+ error '/', 'Does not match all allOf conditions.'
100
+ end
101
+
102
+ assert_validation('1234') do
103
+ error '/', 'Does not match all allOf conditions.'
104
+ end
105
+ assert_cast('42', 42)
106
+ end
107
+
108
+ def test_casting_with_conditions_changed_order
109
+ schema :all_of do
110
+ str min_length: 2
111
+ str max_length: 3
112
+ str format: :integer
113
+ end
114
+
115
+ assert_validation(nil)
116
+ assert_validation('12')
117
+ assert_validation('123')
118
+
119
+ assert_validation('1') do
120
+ error '/', 'Does not match all allOf conditions.'
121
+ end
122
+
123
+ assert_validation('1234') do
124
+ error '/', 'Does not match all allOf conditions.'
125
+ end
126
+
127
+ assert_cast('42', 42)
128
+ assert_cast('123', 123)
129
+ end
130
+
131
+ def test_hash_casting
132
+ schema :all_of do
133
+ hsh additional_properties: true do
134
+ str! :foo, format: :integer
135
+ end
136
+ hsh additional_properties: true do
137
+ str! :bar, format: :date
138
+ end
139
+ end
140
+
141
+ assert_validation(foo: '42', bar: '2020-01-15')
142
+
143
+ assert_cast(
144
+ { foo: '42', bar: '2020-01-15' },
145
+ foo: 42,
146
+ bar: Date.new(2020, 1, 15)
147
+ )
148
+ end
149
+
150
+ def test_nonsensical_casting
151
+ schema :all_of do
152
+ str format: :integer
153
+ str format: :boolean
154
+ end
155
+
156
+ assert_validation(nil)
157
+ assert_validation('42') do
158
+ error '/', 'Does not match all allOf conditions.'
159
+ end
160
+ assert_validation('true') do
161
+ error '/', 'Does not match all allOf conditions.'
162
+ end
163
+ end
164
+
165
+ def test_defaults
166
+ schema :all_of do
167
+ str default: 'foobar'
168
+ end
169
+
170
+ assert_validation(nil)
171
+ assert_validation('Hello World')
172
+
173
+ assert_json({
174
+ allOf: [
175
+ type: :string,
176
+ default: 'foobar'
177
+ ]
178
+ })
179
+ end
180
+
181
+ def test_with_generic_keywords
182
+ schema :all_of, title: 'allOf schema',
183
+ description: 'allOf schema holding generic keywords',
184
+ examples: [
185
+ 'foo'
186
+ ]
187
+
188
+ assert_json({
189
+ allOf: [],
190
+ title: 'allOf schema',
191
+ description: 'allOf schema holding generic keywords',
192
+ examples: [
193
+ 'foo'
194
+ ]
195
+ })
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,218 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V3
5
+ class AnyOfNodeTest < V3Test
6
+ def test_optional
7
+ schema :any_of do
8
+ str
9
+ int
10
+ end
11
+
12
+ assert_validation(nil)
13
+ assert_validation('string')
14
+ assert_validation(42)
15
+ assert_validation(42.1) do
16
+ error '/', 'Does not match any anyOf condition.'
17
+ end
18
+ assert_validation({}) do
19
+ error '/', 'Does not match any anyOf condition.'
20
+ end
21
+ end
22
+
23
+ def test_required
24
+ schema :any_of, required: true do
25
+ str
26
+ int
27
+ end
28
+
29
+ assert_validation('string')
30
+ assert_validation(42)
31
+
32
+ assert_validation(nil) do
33
+ error '/', 'Value must be given.'
34
+ end
35
+
36
+ assert_validation(42.1) do
37
+ error '/', 'Does not match any anyOf condition.'
38
+ end
39
+ assert_validation({}) do
40
+ error '/', 'Does not match any anyOf condition.'
41
+ end
42
+ end
43
+
44
+ def test_nested
45
+ schema :any_of do
46
+ hsh do
47
+ any_of! :foo do
48
+ int
49
+ str
50
+ end
51
+ end
52
+ int
53
+ end
54
+
55
+ assert_validation(nil)
56
+ assert_validation(42)
57
+ assert_validation(foo: 42)
58
+ assert_validation(foo: 'str')
59
+ assert_validation('string') do
60
+ error '/', 'Does not match any anyOf condition.'
61
+ end
62
+ assert_validation(bar: :baz) do
63
+ error '/', 'Does not match any anyOf condition.'
64
+ end
65
+ assert_validation(foo: :bar) do
66
+ error '/', 'Does not match any anyOf condition.'
67
+ end
68
+ end
69
+
70
+ def test_all_types
71
+ schema :any_of do
72
+ all_of do
73
+ hsh { str! :foo }
74
+ end
75
+ any_of do
76
+ obj(Date)
77
+ obj(Time)
78
+ end
79
+ ary
80
+ boo
81
+ int
82
+ num
83
+ hsh { int! :bar }
84
+ one_of do
85
+ hsh { str! :a, pattern: '^a' }
86
+ hsh { str! :a, pattern: 'z$' }
87
+ end
88
+ ref :MyDate
89
+ str max_length: 2
90
+ end
91
+
92
+ context = Context.new
93
+ context.schema :MyDate, :string, format: :date
94
+
95
+ Schemacop.with_context context do
96
+ assert_validation(nil)
97
+ assert_validation(foo: 'str')
98
+ assert_validation([1, 2, 3])
99
+ assert_validation(true)
100
+ assert_validation(false)
101
+ assert_validation(42)
102
+ assert_validation(42.2)
103
+ assert_validation(bar: 42)
104
+ assert_validation(a: 'a hello')
105
+ assert_validation(a: 'hello z')
106
+ assert_validation('1990-01-13')
107
+ assert_validation('12')
108
+ assert_validation(Date.new(1990, 1, 13))
109
+ assert_validation(Time.now)
110
+
111
+ assert_validation('1990-01-13 12') do
112
+ error '/', 'Does not match any anyOf condition.'
113
+ end
114
+ assert_validation(a: 'a hello z') do
115
+ error '/', 'Does not match any anyOf condition.'
116
+ end
117
+ assert_validation(Object.new) do
118
+ error '/', 'Does not match any anyOf condition.'
119
+ end
120
+ end
121
+ end
122
+
123
+ def test_casting
124
+ schema do
125
+ any_of! :created_at do
126
+ str format: :date
127
+ str format: :date_time
128
+ end
129
+ end
130
+
131
+ assert_validation(created_at: '2020-01-01')
132
+ assert_validation(created_at: '2020-01-01T17:38:20')
133
+
134
+ assert_cast(
135
+ { created_at: '2020-01-01' },
136
+ created_at: Date.new(2020, 1, 1)
137
+ )
138
+ assert_cast(
139
+ { created_at: '2020-01-01T17:38:20' },
140
+ created_at: DateTime.new(2020, 1, 1, 17, 38, 20)
141
+ )
142
+ end
143
+
144
+ def test_defaults
145
+ schema do
146
+ any_of! :foo do
147
+ hsh { str? :bar }
148
+ hsh { str? :baz, default: 'Baz' }
149
+ end
150
+ end
151
+
152
+ assert_validation(foo: { bar: 'Bar' })
153
+ assert_validation(foo: { baz: 'Baz' })
154
+
155
+ assert_validation(foo: { xyz: 'Baz' }) do
156
+ error '/foo', 'Does not match any anyOf condition.'
157
+ end
158
+
159
+ assert_cast(
160
+ { foo: { bar: nil } },
161
+ foo: { bar: nil }
162
+ )
163
+
164
+ assert_cast(
165
+ { foo: { baz: nil } },
166
+ foo: { baz: 'Baz' }
167
+ )
168
+
169
+ schema do
170
+ any_of! :foo do
171
+ hsh { str? :bar, format: :date }
172
+ hsh { str? :bar, default: 'Baz', format: :date_time }
173
+ end
174
+ end
175
+
176
+ assert_cast(
177
+ { foo: { bar: '1990-01-13' } },
178
+ foo: { bar: Date.new(1990, 1, 13) }
179
+ )
180
+
181
+ assert_cast(
182
+ { foo: { bar: '1990-01-13T10:00:00Z' } },
183
+ foo: { bar: DateTime.new(1990, 1, 13, 10, 0, 0) }
184
+ )
185
+ end
186
+
187
+ def test_with_generic_keywords
188
+ schema :any_of, title: 'anyOf schema',
189
+ description: 'anyOf schema holding generic keywords',
190
+ examples: [
191
+ 'foo'
192
+ ] do
193
+ str
194
+ int
195
+ end
196
+
197
+ assert_json({
198
+ anyOf: [
199
+ { type: :string },
200
+ { type: :integer }
201
+ ],
202
+ title: 'anyOf schema',
203
+ description: 'anyOf schema holding generic keywords',
204
+ examples: [
205
+ 'foo'
206
+ ]
207
+ })
208
+ end
209
+
210
+ def test_invalid_schema
211
+ assert_raises_with_message Exceptions::InvalidSchemaError,
212
+ 'Node "any_of" makes only sense with at least 1 item.' do
213
+ schema :any_of
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end