schemacop 2.4.5 → 3.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +25 -1
  4. data/.travis.yml +3 -1
  5. data/CHANGELOG.md +32 -1
  6. data/README.md +53 -710
  7. data/README_V2.md +775 -0
  8. data/README_V3.md +1195 -0
  9. data/Rakefile +8 -12
  10. data/VERSION +1 -1
  11. data/lib/schemacop.rb +35 -36
  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/v2/node_supporting_field.rb +70 -0
  26. data/lib/schemacop/{node_supporting_type.rb → v2/node_supporting_type.rb} +14 -11
  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 +218 -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 +256 -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 +215 -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 +198 -0
  86. data/test/unit/schemacop/v3/any_of_node_test.rb +218 -0
  87. data/test/unit/schemacop/v3/array_node_test.rb +815 -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 +884 -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/node_supporting_field.rb +0 -58
  145. data/lib/schemacop/root_node.rb +0 -4
  146. data/lib/schemacop/validator/array_validator.rb +0 -30
  147. data/lib/schemacop/validator/float_validator.rb +0 -5
  148. data/lib/schemacop/validator/hash_validator.rb +0 -35
  149. data/lib/schemacop/validator/integer_validator.rb +0 -5
  150. data/lib/schemacop/validator/number_validator.rb +0 -19
  151. data/lib/schemacop/validator/object_validator.rb +0 -27
  152. data/lib/schemacop/validator/string_validator.rb +0 -37
  153. data/test/casting_test.rb +0 -90
  154. data/test/collector_test.rb +0 -45
  155. data/test/custom_check_test.rb +0 -93
  156. data/test/custom_if_test.rb +0 -95
  157. data/test/defaults_test.rb +0 -93
  158. data/test/empty_test.rb +0 -14
  159. data/test/nil_dis_allow_test.rb +0 -41
  160. data/test/node_resolver_test.rb +0 -26
  161. data/test/short_forms_test.rb +0 -349
  162. data/test/test_helper.rb +0 -13
  163. data/test/types_test.rb +0 -84
  164. data/test/validator_array_test.rb +0 -97
  165. data/test/validator_boolean_test.rb +0 -15
  166. data/test/validator_float_test.rb +0 -57
  167. data/test/validator_hash_test.rb +0 -93
  168. data/test/validator_integer_test.rb +0 -46
  169. data/test/validator_nil_test.rb +0 -13
  170. data/test/validator_number_test.rb +0 -60
  171. data/test/validator_object_test.rb +0 -139
  172. data/test/validator_string_test.rb +0 -76
  173. data/test/validator_symbol_test.rb +0 -16
@@ -0,0 +1,148 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V3
5
+ class NodeTest < V3Test
6
+ def test_empty_schema
7
+ # Basic, empty schema, which will allow anything
8
+ @schema = Schemacop::Schema3.new
9
+
10
+ assert_json({})
11
+
12
+ assert_validation(nil)
13
+ assert_validation([nil])
14
+ assert_validation(1)
15
+ assert_validation('foo')
16
+ assert_validation(:bar)
17
+ assert_validation({ foo: 'bar', baz: 123 })
18
+ assert_validation([nil, 'foo', 1234, { bar: :bar }])
19
+ end
20
+
21
+ def test_empty_enum_schema
22
+ @schema = Schemacop::Schema3.new enum: [1, 2, 'foo', :bar, { qux: 42 }]
23
+
24
+ assert_json({ enum: [1, 2, 'foo', :bar, { qux: 42 }] })
25
+
26
+ assert_validation(nil)
27
+ assert_validation(1)
28
+ assert_validation('foo')
29
+ assert_validation(:bar)
30
+ assert_validation({ qux: 42 })
31
+
32
+ assert_validation(3) do
33
+ error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
34
+ end
35
+ assert_validation('bar') do
36
+ error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
37
+ end
38
+ assert_validation(:foo) do
39
+ error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
40
+ end
41
+ assert_validation({ qux: 13 }) do
42
+ error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
43
+ end
44
+ end
45
+
46
+ def test_empty_schema_with_generic_keywords
47
+ @schema = Schemacop::Schema3.new enum: [1, 'foo'],
48
+ title: 'Empty schema',
49
+ description: 'Empty schema holding generic keywords',
50
+ examples: [
51
+ 1,
52
+ 'foo'
53
+ ]
54
+
55
+ assert_json({
56
+ enum: [1, 'foo'],
57
+ title: 'Empty schema',
58
+ description: 'Empty schema holding generic keywords',
59
+ examples: [
60
+ 1,
61
+ 'foo'
62
+ ]
63
+ })
64
+ end
65
+
66
+ def test_cast_in_root
67
+ schema :integer, cast_str: true, required: true
68
+
69
+ assert_json(
70
+ oneOf: [
71
+ { type: :integer },
72
+ { type: :string, format: :integer }
73
+ ]
74
+ )
75
+
76
+ assert_validation(5)
77
+
78
+ assert_validation(nil) do
79
+ error '/', 'Value must be given.'
80
+ end
81
+
82
+ assert_validation('5')
83
+ assert_validation('5.3') do
84
+ error '/', 'Matches 0 definitions but should match exactly 1.'
85
+ end
86
+
87
+ assert_cast(5, 5)
88
+ assert_cast('5', 5)
89
+ end
90
+
91
+ def test_cast_in_array
92
+ schema :array do
93
+ list :number, cast_str: true, minimum: 3
94
+ end
95
+
96
+ assert_json(
97
+ type: :array,
98
+ items: {
99
+ oneOf: [
100
+ { type: :number, minimum: 3 },
101
+ { type: :string, format: :number }
102
+ ]
103
+ }
104
+ )
105
+
106
+ assert_validation(nil)
107
+ assert_validation([nil])
108
+ assert_validation([5, 5.3, '42.0', '42.42'])
109
+ assert_validation([5, 5.3, '42.0', '42.42', 'bar']) do
110
+ error '/[4]', 'Matches 0 definitions but should match exactly 1.'
111
+ end
112
+ assert_validation([2]) do
113
+ error '/[0]', 'Matches 0 definitions but should match exactly 1.'
114
+ end
115
+ assert_validation(['2']) do
116
+ error '/[0]', 'Matches 0 definitions but should match exactly 1.'
117
+ end
118
+
119
+ assert_cast(['3'], [3])
120
+ assert_cast(['4', 5, '6'], [4, 5, 6])
121
+ end
122
+
123
+ def test_cast_in_array_required
124
+ schema :array do
125
+ num cast_str: true, minimum: 3, required: true
126
+ end
127
+
128
+ assert_json(
129
+ type: :array,
130
+ items: [
131
+ {
132
+ oneOf: [
133
+ { type: :number, minimum: 3 },
134
+ { type: :string, format: :number }
135
+ ]
136
+ }
137
+ ],
138
+ additionalItems: false
139
+ )
140
+
141
+ assert_validation(nil)
142
+ assert_validation([nil]) do
143
+ error '/[0]', 'Value must be given.'
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,292 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V3
5
+ class NumberNodeTest < V3Test
6
+ EXP_INVALID_TYPE = 'Invalid type, expected "big_decimal" or "float" or "integer" or "rational".'.freeze
7
+
8
+ def test_basic
9
+ schema :number
10
+ assert_validation 25
11
+ assert_validation 3.323523242323523
12
+ assert_validation(-14)
13
+ assert_validation(-14.5)
14
+ assert_validation(1r)
15
+ assert_validation(2.5r)
16
+ assert_validation(BigDecimal(6))
17
+
18
+ assert_validation((6 + 0i)) do
19
+ error '/', EXP_INVALID_TYPE
20
+ end
21
+
22
+ assert_json(type: :number)
23
+ end
24
+
25
+ def test_hash
26
+ schema { num! :age }
27
+
28
+ assert_json(
29
+ type: :object,
30
+ properties: {
31
+ age: { type: :number }
32
+ },
33
+ required: %i[age],
34
+ additionalProperties: false
35
+ )
36
+ assert_validation age: 30
37
+ assert_validation age: 2.5r
38
+ assert_validation age: BigDecimal(5)
39
+ end
40
+
41
+ def test_array
42
+ schema(:array) do
43
+ list :number
44
+ end
45
+
46
+ assert_json(
47
+ type: :array,
48
+ items: { type: :number }
49
+ )
50
+
51
+ assert_validation [30]
52
+ assert_validation [30.3, 42.0]
53
+ assert_validation [30, 30r, 30.0, BigDecimal(30)]
54
+ assert_validation ['30.3', 30.3] do
55
+ error '/[0]', EXP_INVALID_TYPE
56
+ end
57
+ end
58
+
59
+ def test_type
60
+ schema :number
61
+
62
+ assert_json(
63
+ type: :number
64
+ )
65
+
66
+ assert_validation '42.5' do
67
+ error '/', EXP_INVALID_TYPE
68
+ end
69
+
70
+ schema { num! :age }
71
+
72
+ assert_validation age: :foo do
73
+ error '/age', EXP_INVALID_TYPE
74
+ end
75
+
76
+ assert_validation age: '234' do
77
+ error '/age', EXP_INVALID_TYPE
78
+ end
79
+ end
80
+
81
+ def test_minimum
82
+ schema :number, minimum: 0
83
+
84
+ assert_json(
85
+ type: :number,
86
+ minimum: 0
87
+ )
88
+
89
+ assert_validation 5
90
+ assert_validation 0
91
+ assert_validation(-1) do
92
+ error '/', 'Value must have a minimum of 0.'
93
+ end
94
+ end
95
+
96
+ def test_exclusive_minimum
97
+ schema :number, exclusive_minimum: 0
98
+
99
+ assert_json(
100
+ type: :number,
101
+ exclusiveMinimum: 0
102
+ )
103
+
104
+ assert_validation 5
105
+ assert_validation 1
106
+ assert_validation(0) do
107
+ error '/', 'Value must have an exclusive minimum of 0.'
108
+ end
109
+ assert_validation(-5) do
110
+ error '/', 'Value must have an exclusive minimum of 0.'
111
+ end
112
+ end
113
+
114
+ def test_maximum
115
+ schema :number, maximum: 5
116
+
117
+ assert_json(
118
+ type: :number,
119
+ maximum: 5
120
+ )
121
+
122
+ assert_validation 5
123
+ assert_validation 0
124
+ assert_validation(6) do
125
+ error '/', 'Value must have a maximum of 5.'
126
+ end
127
+ end
128
+
129
+ def test_exclusive_maximum
130
+ schema :number, exclusive_maximum: 5
131
+
132
+ assert_json(
133
+ type: :number,
134
+ exclusiveMaximum: 5
135
+ )
136
+
137
+ assert_validation 4
138
+ assert_validation 1
139
+ assert_validation(5) do
140
+ error '/', 'Value must have an exclusive maximum of 5.'
141
+ end
142
+ assert_validation(9) do
143
+ error '/', 'Value must have an exclusive maximum of 5.'
144
+ end
145
+ end
146
+
147
+ def test_multiple_of
148
+ schema :number, multiple_of: 2
149
+
150
+ assert_json(
151
+ type: :number,
152
+ multipleOf: 2
153
+ )
154
+
155
+ assert_validation(-4.0)
156
+ assert_validation(-2)
157
+ assert_validation(0)
158
+ assert_validation(2)
159
+ assert_validation(300)
160
+
161
+ assert_validation(5) do
162
+ error '/', 'Value must be a multiple of 2.'
163
+ end
164
+
165
+ schema :number, multiple_of: 1.2
166
+
167
+ assert_json(
168
+ type: :number,
169
+ multipleOf: 1.2
170
+ )
171
+
172
+ assert_validation(1.2)
173
+ assert_validation(2.4)
174
+ assert_validation(0)
175
+ assert_validation(-4.8)
176
+
177
+ assert_validation(3.8) do
178
+ error '/', 'Value must be a multiple of 1.2.'
179
+ end
180
+ end
181
+
182
+ # Helper function that checks for all the options if the option is
183
+ # an allowed class or something else, in which case it needs to raise
184
+ def validate_self_should_error(value_to_check)
185
+ assert_raises_with_message Exceptions::InvalidSchemaError,
186
+ 'Option "minimum" must be a "big_decimal" or "float" or "integer" or "rational"' do
187
+ schema :number, minimum: value_to_check
188
+ end
189
+
190
+ assert_raises_with_message Exceptions::InvalidSchemaError,
191
+ 'Option "maximum" must be a "big_decimal" or "float" or "integer" or "rational"' do
192
+ schema :number, maximum: value_to_check
193
+ end
194
+
195
+ assert_raises_with_message Exceptions::InvalidSchemaError,
196
+ 'Option "exclusive_minimum" must be a "big_decimal" or "float" or "integer" or "rational"' do
197
+ schema :number, exclusive_minimum: value_to_check
198
+ end
199
+
200
+ assert_raises_with_message Exceptions::InvalidSchemaError,
201
+ 'Option "exclusive_maximum" must be a "big_decimal" or "float" or "integer" or "rational"' do
202
+ schema :number, exclusive_maximum: value_to_check
203
+ end
204
+
205
+ assert_raises_with_message Exceptions::InvalidSchemaError,
206
+ 'Option "multiple_of" must be a "big_decimal" or "float" or "integer" or "rational"' do
207
+ schema :number, multiple_of: value_to_check
208
+ end
209
+ end
210
+
211
+ def test_validate_self
212
+ assert_raises_with_message Exceptions::InvalidSchemaError,
213
+ 'Option "minimum" can\'t be greater than "maximum".' do
214
+ schema :number, minimum: 5, maximum: 4
215
+ end
216
+
217
+ assert_raises_with_message Exceptions::InvalidSchemaError,
218
+ 'Option "exclusive_minimum" can\'t be '\
219
+ 'greater than "exclusive_maximum".' do
220
+ schema :number, exclusive_minimum: 5, exclusive_maximum: 4
221
+ end
222
+
223
+ assert_raises_with_message Exceptions::InvalidSchemaError,
224
+ 'Option "multiple_of" can\'t be 0.' do
225
+ schema :number, multiple_of: 0
226
+ end
227
+
228
+ validate_self_should_error(1 + 0i) # Complex
229
+ validate_self_should_error(Object.new)
230
+ validate_self_should_error(true)
231
+ validate_self_should_error(false)
232
+ validate_self_should_error('1')
233
+ validate_self_should_error('1.0')
234
+ validate_self_should_error('String')
235
+ end
236
+
237
+ def test_enum_schema
238
+ schema :number, enum: [1, 2, 'foo', :bar, { qux: 42 }, 4.2]
239
+
240
+ assert_json({
241
+ type: :number,
242
+ enum: [1, 2, 'foo', :bar, { qux: 42 }, 4.2]
243
+ })
244
+
245
+ assert_validation(nil)
246
+ assert_validation(1)
247
+ assert_validation(4.2)
248
+
249
+ # Even we put those types in the enum, they need to fail the validations,
250
+ # as they are not numbers
251
+ assert_validation('foo') do
252
+ error '/', EXP_INVALID_TYPE
253
+ end
254
+ assert_validation(:bar) do
255
+ error '/', EXP_INVALID_TYPE
256
+ end
257
+ assert_validation({ qux: 42 }) do
258
+ error '/', EXP_INVALID_TYPE
259
+ end
260
+
261
+ # These need to fail validation, as they are not in the enum list
262
+ assert_validation(0.5) do
263
+ error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}, 4.2].'
264
+ end
265
+ assert_validation(4) do
266
+ error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}, 4.2].'
267
+ end
268
+ end
269
+
270
+ def test_with_generic_keywords
271
+ schema :number, enum: [1, 'foo', 4.2],
272
+ title: 'Number schema',
273
+ description: 'Number schema holding generic keywords',
274
+ examples: [
275
+ 1,
276
+ 4.2
277
+ ]
278
+
279
+ assert_json({
280
+ type: :number,
281
+ enum: [1, 'foo', 4.2],
282
+ title: 'Number schema',
283
+ description: 'Number schema holding generic keywords',
284
+ examples: [
285
+ 1,
286
+ 4.2
287
+ ]
288
+ })
289
+ end
290
+ end
291
+ end
292
+ end
@@ -0,0 +1,170 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V3
5
+ class ObjectNodeTest < V3Test
6
+ def test_basic
7
+ schema :object
8
+
9
+ assert_validation nil
10
+ assert_validation true
11
+ assert_validation false
12
+ assert_validation Object.new
13
+ assert_validation 'foo'
14
+
15
+ assert_json({})
16
+ end
17
+
18
+ def test_required_with_no_types
19
+ schema :object, required: true
20
+
21
+ assert_validation nil do
22
+ error '/', 'Value must be given.'
23
+ end
24
+ end
25
+
26
+ def test_with_classes
27
+ schema :object, classes: [String, Date]
28
+ assert_validation 'foo'
29
+ assert_validation Date.today
30
+ assert_validation({}.with_indifferent_access) do
31
+ error '/', 'Invalid type, expected "Date" or "String".'
32
+ end
33
+ assert_validation DateTime.now do
34
+ error '/', 'Invalid type, expected "Date" or "String".'
35
+ end
36
+ end
37
+
38
+ def test_non_strict
39
+ schema :object, classes: [String, Date, Hash], strict: false
40
+ assert_validation 'foo'
41
+ assert_validation 'foo'.html_safe
42
+ assert_validation Date.today
43
+ assert_validation nil
44
+ assert_validation DateTime.now
45
+ assert_validation({}.with_indifferent_access)
46
+ assert_validation Time.now do
47
+ error '/', 'Invalid type, expected "Date" or "Hash" or "String".'
48
+ end
49
+ end
50
+
51
+ def test_required
52
+ schema :object, required: true
53
+
54
+ assert_validation true
55
+ assert_validation false
56
+ assert_validation nil do
57
+ error '/', 'Value must be given.'
58
+ end
59
+ end
60
+
61
+ def test_hash
62
+ schema { obj! :myobj, String }
63
+ assert_json(
64
+ type: :object,
65
+ properties: {
66
+ myobj: {}
67
+ },
68
+ required: %i[myobj],
69
+ additionalProperties: false
70
+ )
71
+ assert_validation myobj: ''
72
+ assert_validation myobj: '42'
73
+ assert_validation myobj: Date.today do
74
+ error '/myobj', 'Invalid type, expected "String".'
75
+ end
76
+ assert_validation({}) do
77
+ error '/myobj', 'Value must be given.'
78
+ end
79
+ end
80
+
81
+ def test_enum_schema
82
+ schema :object, enum: [true, 'foo', :baz, [], { qux: '123' }]
83
+
84
+ # Can't represent a Ruby Object as a JSON value
85
+ assert_json({})
86
+
87
+ # As we didn't provide any classes, any object (i.e. everything) will
88
+ # be validated. However, only those elements we put into the enum list
89
+ # will be allowed
90
+ assert_validation(nil)
91
+ assert_validation(true)
92
+ assert_validation('foo')
93
+ assert_validation(:baz)
94
+ assert_validation([])
95
+ assert_validation({ qux: '123' })
96
+
97
+ # These will fail, as we didn't put them into the enum list
98
+ assert_validation(1) do
99
+ error '/', 'Value not included in enum [true, "foo", :baz, [], {:qux=>"123"}].'
100
+ end
101
+ assert_validation(:bar) do
102
+ error '/', 'Value not included in enum [true, "foo", :baz, [], {:qux=>"123"}].'
103
+ end
104
+ assert_validation({ qux: 42 }) do
105
+ error '/', 'Value not included in enum [true, "foo", :baz, [], {:qux=>"123"}].'
106
+ end
107
+ end
108
+
109
+ def test_enum_schema_with_classes
110
+ schema :object, classes: [String, Symbol, TrueClass], enum: [true, 'foo', :baz, [], { qux: '123' }, false]
111
+
112
+ # Can't represent a Ruby Object as a JSON value
113
+ assert_json({})
114
+
115
+ # Values need to be one of the classed we defined above, as well as in the
116
+ # enum list for the validation to pass
117
+ assert_validation(nil)
118
+ assert_validation(true)
119
+ assert_validation('foo')
120
+ assert_validation(:baz)
121
+
122
+ # These will fail, as they aren't of one of the classed we defined above
123
+ assert_validation([]) do
124
+ error '/', 'Invalid type, expected "String" or "Symbol" or "TrueClass".'
125
+ end
126
+ assert_validation({ qux: '123' }) do
127
+ error '/', 'Invalid type, expected "String" or "Symbol" or "TrueClass".'
128
+ end
129
+ assert_validation(false) do
130
+ error '/', 'Invalid type, expected "String" or "Symbol" or "TrueClass".'
131
+ end
132
+ end
133
+
134
+ def test_with_generic_keywords
135
+ schema :object, enum: [1, 'foo'],
136
+ title: 'Object schema',
137
+ description: 'Object schema holding generic keywords',
138
+ examples: [
139
+ 'foo'
140
+ ]
141
+
142
+ assert_json({})
143
+ end
144
+
145
+ def test_validate_self
146
+ assert_raises_with_message Exceptions::InvalidSchemaError,
147
+ 'Option "strict" must be a "boolean".' do
148
+ schema :object, strict: 'false'
149
+ end
150
+
151
+ assert_raises_with_message Exceptions::InvalidSchemaError,
152
+ 'Option "strict" must be a "boolean".' do
153
+ schema :object, strict: 123
154
+ end
155
+
156
+ assert_raises_with_message Exceptions::InvalidSchemaError,
157
+ 'Option "strict" must be a "boolean".' do
158
+ schema :object, strict: [1, 2, 3]
159
+ end
160
+
161
+ # rubocop:disable Lint/BooleanSymbol
162
+ assert_raises_with_message Exceptions::InvalidSchemaError,
163
+ 'Option "strict" must be a "boolean".' do
164
+ schema :object, strict: :false
165
+ end
166
+ # rubocop:enable Lint/BooleanSymbol
167
+ end
168
+ end
169
+ end
170
+ end