schemacop 2.4.7 → 3.0.0.rc4

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/.releaser_config +0 -1
  4. data/.rubocop.yml +25 -1
  5. data/.travis.yml +3 -1
  6. data/CHANGELOG.md +37 -0
  7. data/README.md +53 -710
  8. data/README_V2.md +775 -0
  9. data/README_V3.md +1253 -0
  10. data/Rakefile +8 -12
  11. data/VERSION +1 -1
  12. data/lib/schemacop.rb +35 -37
  13. data/lib/schemacop/base_schema.rb +37 -0
  14. data/lib/schemacop/railtie.rb +10 -0
  15. data/lib/schemacop/schema.rb +1 -60
  16. data/lib/schemacop/schema2.rb +22 -0
  17. data/lib/schemacop/schema3.rb +21 -0
  18. data/lib/schemacop/scoped_env.rb +25 -13
  19. data/lib/schemacop/v2.rb +25 -0
  20. data/lib/schemacop/{caster.rb → v2/caster.rb} +17 -2
  21. data/lib/schemacop/{collector.rb → v2/collector.rb} +5 -2
  22. data/lib/schemacop/{dupper.rb → v2/dupper.rb} +1 -1
  23. data/lib/schemacop/{field_node.rb → v2/field_node.rb} +4 -3
  24. data/lib/schemacop/v2/node.rb +142 -0
  25. data/lib/schemacop/{node_resolver.rb → v2/node_resolver.rb} +1 -1
  26. data/lib/schemacop/v2/node_supporting_field.rb +70 -0
  27. data/lib/schemacop/{node_supporting_type.rb → v2/node_supporting_type.rb} +8 -5
  28. data/lib/schemacop/{node_with_block.rb → v2/node_with_block.rb} +3 -2
  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 +267 -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 +219 -0
  52. data/lib/schemacop/v3/node_registry.rb +45 -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 +55 -0
  58. data/lib/schemacop/v3/result.rb +58 -0
  59. data/lib/schemacop/v3/string_node.rb +132 -0
  60. data/lib/schemacop/v3/symbol_node.rb +13 -0
  61. data/schemacop.gemspec +24 -27
  62. data/test/lib/test_helper.rb +167 -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 +157 -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 +106 -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 +166 -0
  90. data/test/unit/schemacop/v3/hash_node_test.rb +972 -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 +162 -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 +367 -0
  98. data/test/unit/schemacop/v3/string_node_test.rb +372 -0
  99. data/test/unit/schemacop/v3/symbol_node_test.rb +75 -0
  100. metadata +151 -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 -118
  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,372 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V3
5
+ class StringNodeTest < V3Test
6
+ EXP_INVALID_TYPE = 'Invalid type, expected "string".'.freeze
7
+
8
+ def test_basic
9
+ schema :string
10
+ assert_validation 'Hello World'
11
+ assert_validation ''
12
+ assert_json(type: :string)
13
+ end
14
+
15
+ def test_required
16
+ schema :string, required: true
17
+ assert_validation 'Hello World'
18
+ assert_validation ''
19
+ assert_validation nil do
20
+ error '/', 'Value must be given.'
21
+ end
22
+ end
23
+
24
+ def test_hash
25
+ schema { str! :name }
26
+ assert_validation name: 'Hello World'
27
+ assert_json(type: :object, properties: { name: { type: :string } }, required: %i[name], additionalProperties: false)
28
+ end
29
+
30
+ def test_type
31
+ schema :string
32
+
33
+ assert_json(type: :string)
34
+
35
+ assert_validation 42 do
36
+ error '/', EXP_INVALID_TYPE
37
+ end
38
+
39
+ schema { str! :name }
40
+
41
+ assert_json(type: :object, properties: { name: { type: :string } }, required: %i[name], additionalProperties: false)
42
+
43
+ assert_validation name: :foo do
44
+ error '/name', EXP_INVALID_TYPE
45
+ end
46
+
47
+ assert_validation name: 234 do
48
+ error '/name', EXP_INVALID_TYPE
49
+ end
50
+ end
51
+
52
+ def test_min_length
53
+ schema :string, min_length: 5
54
+
55
+ assert_json(type: :string, minLength: 5)
56
+
57
+ assert_validation '12345'
58
+ assert_validation '12345678'
59
+
60
+ assert_validation '1234' do
61
+ error '/', 'String is 4 characters long but must be at least 5.'
62
+ end
63
+
64
+ assert_validation '' do
65
+ error '/', 'String is 0 characters long but must be at least 5.'
66
+ end
67
+ end
68
+
69
+ def test_max_length
70
+ schema :string, max_length: 5
71
+
72
+ assert_json(type: :string, maxLength: 5)
73
+
74
+ assert_validation ''
75
+ assert_validation '12345'
76
+ assert_validation '1234'
77
+
78
+ assert_validation '123456' do
79
+ error '/', 'String is 6 characters long but must be at most 5.'
80
+ end
81
+ end
82
+
83
+ def test_pattern
84
+ schema :string, pattern: '^a_.*_z$'
85
+
86
+ assert_json(type: :string, pattern: '^a_.*_z$')
87
+
88
+ assert_validation 'a__z'
89
+ assert_validation 'a_ foo bar _z'
90
+ assert_validation '' do
91
+ error '/', 'String does not match pattern "^a_.*_z$".'
92
+ end
93
+ assert_validation 'a_ _zfoo' do
94
+ error '/', 'String does not match pattern "^a_.*_z$".'
95
+ end
96
+ end
97
+
98
+ def test_format_date
99
+ schema :string, format: :date
100
+
101
+ assert_json(type: :string, format: :date)
102
+
103
+ assert_validation '2020-01-13'
104
+ assert_validation '2020-02-29'
105
+ assert_validation '2021-02-29' # Leap years are not validated
106
+
107
+ assert_validation '2020-13-29' do
108
+ error '/', 'String does not match format "date".'
109
+ end
110
+
111
+ assert_validation 'foo 2020-01-29 bar' do
112
+ error '/', 'String does not match format "date".'
113
+ end
114
+
115
+ assert_cast(nil, nil)
116
+ assert_cast('2020-01-13', Date.new(2020, 1, 13))
117
+ end
118
+
119
+ def test_format_date_time
120
+ schema :string, format: :date_time
121
+
122
+ assert_json(type: :string, format: :'date-time')
123
+
124
+ assert_validation '2018-11-13T20:20:39+00:00'
125
+ assert_validation '2018-11-13T20:20:39Z'
126
+
127
+ assert_validation '2020-13-29' do
128
+ error '/', 'String does not match format "date-time".'
129
+ end
130
+
131
+ assert_validation '2018-13-13T20:20:39+00:00' do
132
+ error '/', 'String does not match format "date-time".'
133
+ end
134
+
135
+ assert_validation '2018-11-13T20:20:39Y' do
136
+ error '/', 'String does not match format "date-time".'
137
+ end
138
+
139
+ assert_cast(nil, nil)
140
+ assert_cast('2018-11-13T20:20:39+00:00', DateTime.new(2018, 11, 13, 20, 20, 39))
141
+ end
142
+
143
+ def test_format_email
144
+ schema :string, format: :email
145
+
146
+ assert_json(type: :string, format: :email)
147
+
148
+ assert_validation 'john.doe@example.com'
149
+ assert_validation 'john.doe+foo-bar_baz@example.com'
150
+ assert_validation 'JOHN.DOE+FOO-BAR_BAZ@EXAMPLE.COM'
151
+
152
+ assert_validation 'someemail' do
153
+ error '/', 'String does not match format "email".'
154
+ end
155
+
156
+ assert_validation 'john doe@example.com' do
157
+ error '/', 'String does not match format "email".'
158
+ end
159
+
160
+ assert_validation '@john@example.com' do
161
+ error '/', 'String does not match format "email".'
162
+ end
163
+
164
+ assert_cast(nil, nil)
165
+ assert_cast('john.doe@example.com', 'john.doe@example.com')
166
+ end
167
+
168
+ def test_enum
169
+ schema :string, enum: ['foo', 'some string', 'some other string', 42]
170
+
171
+ assert_json(type: :string, enum: ['foo', 'some string', 'some other string', 42])
172
+
173
+ assert_validation 'foo'
174
+ assert_validation 'some string'
175
+ assert_validation 'some other string'
176
+
177
+ assert_validation 'fooo' do
178
+ error '/', 'Value not included in enum ["foo", "some string", "some other string", 42].'
179
+ end
180
+
181
+ assert_validation 'other value' do
182
+ error '/', 'Value not included in enum ["foo", "some string", "some other string", 42].'
183
+ end
184
+
185
+ # Integer value 42 is in the enum of allowed values, but it's not a string,
186
+ # so the validation still fails
187
+ assert_validation 42 do
188
+ error '/', 'Invalid type, expected "string".'
189
+ end
190
+ end
191
+
192
+ def test_boolean_casting
193
+ schema :string, format: :boolean
194
+
195
+ assert_json(type: :string, format: :boolean)
196
+
197
+ assert_cast 'true', true
198
+ assert_cast 'false', false
199
+
200
+ assert_cast nil, nil
201
+ end
202
+
203
+ def test_time_casting
204
+ schema :string, format: :time
205
+ assert_json(type: :string, format: :time)
206
+ assert_cast '20:30:39+00:00', Time.strptime('20:30:39+00:00', '%H:%M:%S%z')
207
+
208
+ assert_cast nil, nil
209
+ end
210
+
211
+ def test_date_casting
212
+ schema :string, format: :date
213
+ assert_json(type: :string, format: :date)
214
+ assert_cast '2018-11-13', Date.new(2018, 11, 13)
215
+
216
+ assert_cast nil, nil
217
+ end
218
+
219
+ def test_date_time_casting
220
+ schema :string, format: :date_time
221
+ assert_json(type: :string, format: :'date-time')
222
+ assert_cast '2018-11-13T20:20:39+00:00', DateTime.new(2018, 11, 13, 20, 20, 39)
223
+ assert_cast '2018-11-13T20:20:39Z', DateTime.new(2018, 11, 13, 20, 20, 39)
224
+ assert_cast '2018-11-13T20:20:39+01:00', DateTime.new(2018, 11, 13, 20, 20, 39, '+1')
225
+
226
+ assert_cast nil, nil
227
+ end
228
+
229
+ def test_email_casting
230
+ schema :string, format: :email
231
+ assert_json(type: :string, format: :email)
232
+ assert_cast 'support@example.com', 'support@example.com'
233
+
234
+ assert_cast nil, nil
235
+ end
236
+
237
+ def test_default
238
+ schema :string, default: 'Hello'
239
+
240
+ assert_json(
241
+ type: :string,
242
+ default: 'Hello'
243
+ )
244
+
245
+ assert_validation(nil)
246
+ assert_validation('Foo')
247
+ assert_validation(5) do
248
+ error '/', 'Invalid type, expected "string".'
249
+ end
250
+
251
+ assert_cast('Foo', 'Foo')
252
+ assert_cast(nil, 'Hello')
253
+ end
254
+
255
+ def test_default_casting
256
+ schema :string, format: :integer, default: '42'
257
+
258
+ assert_json(
259
+ type: :string,
260
+ format: :integer,
261
+ default: '42'
262
+ )
263
+
264
+ assert_validation(nil)
265
+ assert_validation('123')
266
+ assert_validation(5) do
267
+ error '/', 'Invalid type, expected "string".'
268
+ end
269
+
270
+ assert_cast('123', 123)
271
+ assert_cast(nil, 42)
272
+ end
273
+
274
+ # Helper function that checks for all the options if the option is
275
+ # an integer or something else, in which case it needs to raise
276
+ def validate_self_should_error(value_to_check)
277
+ assert_raises_with_message Exceptions::InvalidSchemaError,
278
+ 'Option "min_length" must be an "integer"' do
279
+ schema :string, min_length: value_to_check
280
+ end
281
+
282
+ assert_raises_with_message Exceptions::InvalidSchemaError,
283
+ 'Option "max_length" must be an "integer"' do
284
+ schema :string, max_length: value_to_check
285
+ end
286
+ end
287
+
288
+ def test_validate_self
289
+ assert_raises_with_message Exceptions::InvalidSchemaError,
290
+ 'Format "not-existing" is not supported.' do
291
+ schema :string, format: :not_existing
292
+ end
293
+
294
+ assert_raises_with_message Exceptions::InvalidSchemaError,
295
+ 'Option "min_length" can\'t be greater than "max_length".' do
296
+ schema :string, min_length: 5, max_length: 4
297
+ end
298
+
299
+ assert_raises_with_message Exceptions::InvalidSchemaError,
300
+ 'Option "pattern" must be a string.' do
301
+ schema :string, pattern: //
302
+ end
303
+
304
+ assert_raises_with_message Exceptions::InvalidSchemaError,
305
+ 'Option "pattern" can\'t be parsed: end pattern '\
306
+ 'with unmatched parenthesis: /(abcde/.' do
307
+ schema :string, pattern: '(abcde'
308
+ end
309
+
310
+ validate_self_should_error(1.0)
311
+ validate_self_should_error(4r)
312
+ validate_self_should_error(true)
313
+ validate_self_should_error(false)
314
+ validate_self_should_error(Object.new)
315
+ validate_self_should_error((4 + 6i))
316
+ validate_self_should_error('13')
317
+ validate_self_should_error('Lorem ipsum')
318
+ end
319
+
320
+ def test_enum_schema
321
+ schema :string, enum: [1, 2, 'foo', :bar, { qux: 42 }]
322
+
323
+ assert_json({
324
+ type: :string,
325
+ enum: [1, 2, 'foo', :bar, { qux: 42 }]
326
+ })
327
+
328
+ assert_validation(nil)
329
+ assert_validation('foo')
330
+
331
+ # Even we put those types in the enum, they need to fail the validations,
332
+ # as they are not strings
333
+ assert_validation(1) do
334
+ error '/', 'Invalid type, expected "string".'
335
+ end
336
+ assert_validation(:bar) do
337
+ error '/', 'Invalid type, expected "string".'
338
+ end
339
+ assert_validation({ qux: 42 }) do
340
+ error '/', 'Invalid type, expected "string".'
341
+ end
342
+
343
+ # These need to fail validation, as they are not in the enum list
344
+ assert_validation('bar') do
345
+ error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
346
+ end
347
+ assert_validation('Lorem ipsum') do
348
+ error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
349
+ end
350
+ end
351
+
352
+ def test_with_generic_keywords
353
+ schema :string, enum: [1, 'foo'],
354
+ title: 'String schema',
355
+ description: 'String schema holding generic keywords',
356
+ examples: [
357
+ 'foo'
358
+ ]
359
+
360
+ assert_json({
361
+ type: :string,
362
+ enum: [1, 'foo'],
363
+ title: 'String schema',
364
+ description: 'String schema holding generic keywords',
365
+ examples: [
366
+ 'foo'
367
+ ]
368
+ })
369
+ end
370
+ end
371
+ end
372
+ end
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ module V3
5
+ class SymbolNodeTest < V3Test
6
+ EXP_INVALID_TYPE = 'Invalid type, expected "Symbol".'.freeze
7
+
8
+ def test_basic
9
+ schema :symbol
10
+
11
+ assert_validation :foo
12
+ assert_validation :'n0238n)Q(hqr3hrw3'
13
+ assert_validation 42 do
14
+ error '/', EXP_INVALID_TYPE
15
+ end
16
+ assert_validation '42' do
17
+ error '/', EXP_INVALID_TYPE
18
+ end
19
+ assert_json({})
20
+ end
21
+
22
+ def test_required
23
+ schema :symbol, required: true
24
+ assert_validation :foo
25
+ assert_validation ''.to_sym
26
+ assert_validation nil do
27
+ error '/', 'Value must be given.'
28
+ end
29
+ end
30
+
31
+ def test_hash
32
+ schema { sym! :name }
33
+ assert_validation name: :foo
34
+ assert_json(type: :object, properties: { name: {} }, required: %i[name], additionalProperties: false)
35
+ end
36
+
37
+ def test_array
38
+ schema(:array) do
39
+ list :symbol
40
+ end
41
+
42
+ assert_validation %i[foo bar baz]
43
+ assert_json(type: :array, items: {})
44
+ end
45
+
46
+ def test_enum_schema
47
+ schema :symbol, enum: [1, 2, 'foo', :bar, { qux: 42 }]
48
+
49
+ # For symbol nodes, json representation is an empty hash, as we can't
50
+ # repsresent symbols in json
51
+ assert_json({})
52
+
53
+ assert_validation(nil)
54
+ assert_validation(:bar)
55
+
56
+ # Even we put those types in the enum, they need to fail the validations,
57
+ # as they are not symbols
58
+ assert_validation('foo') do
59
+ error '/', 'Invalid type, expected "Symbol".'
60
+ end
61
+ assert_validation(1) do
62
+ error '/', 'Invalid type, expected "Symbol".'
63
+ end
64
+ assert_validation({ qux: 42 }) do
65
+ error '/', 'Invalid type, expected "Symbol".'
66
+ end
67
+
68
+ # These need to fail validation, as they are not in the enum list
69
+ assert_validation(:foo) do
70
+ error '/', 'Value not included in enum [1, 2, "foo", :bar, {:qux=>42}].'
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemacop
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.7
4
+ version: 3.0.0.rc4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-02 00:00:00.000000000 Z
11
+ date: 2021-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -26,20 +26,6 @@ dependencies:
26
26
  version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.3'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.3'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - ">="
@@ -53,21 +39,7 @@ dependencies:
53
39
  - !ruby/object:Gem::Version
54
40
  version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
- name: ci_reporter
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '2.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '2.0'
69
- - !ruby/object:Gem::Dependency
70
- name: ci_reporter_minitest
42
+ name: rake
71
43
  requirement: !ruby/object:Gem::Requirement
72
44
  requirements:
73
45
  - - ">="
@@ -81,7 +53,7 @@ dependencies:
81
53
  - !ruby/object:Gem::Version
82
54
  version: '0'
83
55
  - !ruby/object:Gem::Dependency
84
- name: haml
56
+ name: minitest
85
57
  requirement: !ruby/object:Gem::Requirement
86
58
  requirements:
87
59
  - - ">="
@@ -95,7 +67,7 @@ dependencies:
95
67
  - !ruby/object:Gem::Version
96
68
  version: '0'
97
69
  - !ruby/object:Gem::Dependency
98
- name: colorize
70
+ name: minitest-reporters
99
71
  requirement: !ruby/object:Gem::Requirement
100
72
  requirements:
101
73
  - - ">="
@@ -109,7 +81,7 @@ dependencies:
109
81
  - !ruby/object:Gem::Version
110
82
  version: '0'
111
83
  - !ruby/object:Gem::Dependency
112
- name: yard
84
+ name: colorize
113
85
  requirement: !ruby/object:Gem::Requirement
114
86
  requirements:
115
87
  - - ">="
@@ -128,16 +100,16 @@ dependencies:
128
100
  requirements:
129
101
  - - '='
130
102
  - !ruby/object:Gem::Version
131
- version: 0.35.1
103
+ version: 0.92.0
132
104
  type: :development
133
105
  prerelease: false
134
106
  version_requirements: !ruby/object:Gem::Requirement
135
107
  requirements:
136
108
  - - '='
137
109
  - !ruby/object:Gem::Version
138
- version: 0.35.1
110
+ version: 0.92.0
139
111
  - !ruby/object:Gem::Dependency
140
- name: redcarpet
112
+ name: pry
141
113
  requirement: !ruby/object:Gem::Requirement
142
114
  requirements:
143
115
  - - ">="
@@ -151,7 +123,7 @@ dependencies:
151
123
  - !ruby/object:Gem::Version
152
124
  version: '0'
153
125
  - !ruby/object:Gem::Dependency
154
- name: pry
126
+ name: byebug
155
127
  requirement: !ruby/object:Gem::Requirement
156
128
  requirements:
157
129
  - - ">="
@@ -164,6 +136,20 @@ dependencies:
164
136
  - - ">="
165
137
  - !ruby/object:Gem::Version
166
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '='
144
+ - !ruby/object:Gem::Version
145
+ version: 0.21.2
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '='
151
+ - !ruby/object:Gem::Version
152
+ version: 0.21.2
167
153
  description:
168
154
  email:
169
155
  executables: []
@@ -179,97 +165,100 @@ files:
179
165
  - Gemfile
180
166
  - LICENSE
181
167
  - README.md
168
+ - README_V2.md
169
+ - README_V3.md
182
170
  - RUBY_VERSION
183
171
  - Rakefile
184
172
  - VERSION
185
- - doc/Schemacop.html
186
- - doc/Schemacop/ArrayValidator.html
187
- - doc/Schemacop/BooleanValidator.html
188
- - doc/Schemacop/Caster.html
189
- - doc/Schemacop/Collector.html
190
- - doc/Schemacop/Dupper.html
191
- - doc/Schemacop/Exceptions.html
192
- - doc/Schemacop/Exceptions/InvalidSchemaError.html
193
- - doc/Schemacop/Exceptions/ValidationError.html
194
- - doc/Schemacop/FieldNode.html
195
- - doc/Schemacop/FloatValidator.html
196
- - doc/Schemacop/HashValidator.html
197
- - doc/Schemacop/IntegerValidator.html
198
- - doc/Schemacop/NilValidator.html
199
- - doc/Schemacop/Node.html
200
- - doc/Schemacop/NodeResolver.html
201
- - doc/Schemacop/NodeSupportingField.html
202
- - doc/Schemacop/NodeSupportingType.html
203
- - doc/Schemacop/NodeWithBlock.html
204
- - doc/Schemacop/NumberValidator.html
205
- - doc/Schemacop/ObjectValidator.html
206
- - doc/Schemacop/RootNode.html
207
- - doc/Schemacop/Schema.html
208
- - doc/Schemacop/StringValidator.html
209
- - doc/Schemacop/SymbolValidator.html
210
- - doc/ScopedEnv.html
211
- - doc/_index.html
212
- - doc/class_list.html
213
- - doc/css/common.css
214
- - doc/css/full_list.css
215
- - doc/css/style.css
216
- - doc/file.README.html
217
- - doc/file_list.html
218
- - doc/frames.html
219
- - doc/index.html
220
- - doc/inheritance.graphml
221
- - doc/inheritance.pdf
222
- - doc/js/app.js
223
- - doc/js/full_list.js
224
- - doc/js/jquery.js
225
- - doc/method_list.html
226
- - doc/top-level-namespace.html
227
173
  - lib/schemacop.rb
228
- - lib/schemacop/caster.rb
229
- - lib/schemacop/collector.rb
230
- - lib/schemacop/dupper.rb
174
+ - lib/schemacop/base_schema.rb
231
175
  - lib/schemacop/exceptions.rb
232
- - lib/schemacop/field_node.rb
233
- - lib/schemacop/node.rb
234
- - lib/schemacop/node_resolver.rb
235
- - lib/schemacop/node_supporting_field.rb
236
- - lib/schemacop/node_supporting_type.rb
237
- - lib/schemacop/node_with_block.rb
238
- - lib/schemacop/root_node.rb
176
+ - lib/schemacop/railtie.rb
239
177
  - lib/schemacop/schema.rb
178
+ - lib/schemacop/schema2.rb
179
+ - lib/schemacop/schema3.rb
240
180
  - lib/schemacop/scoped_env.rb
241
- - lib/schemacop/validator/array_validator.rb
242
- - lib/schemacop/validator/boolean_validator.rb
243
- - lib/schemacop/validator/float_validator.rb
244
- - lib/schemacop/validator/hash_validator.rb
245
- - lib/schemacop/validator/integer_validator.rb
246
- - lib/schemacop/validator/nil_validator.rb
247
- - lib/schemacop/validator/number_validator.rb
248
- - lib/schemacop/validator/object_validator.rb
249
- - lib/schemacop/validator/string_validator.rb
250
- - lib/schemacop/validator/symbol_validator.rb
181
+ - lib/schemacop/v2.rb
182
+ - lib/schemacop/v2/caster.rb
183
+ - lib/schemacop/v2/collector.rb
184
+ - lib/schemacop/v2/dupper.rb
185
+ - lib/schemacop/v2/field_node.rb
186
+ - lib/schemacop/v2/node.rb
187
+ - lib/schemacop/v2/node_resolver.rb
188
+ - lib/schemacop/v2/node_supporting_field.rb
189
+ - lib/schemacop/v2/node_supporting_type.rb
190
+ - lib/schemacop/v2/node_with_block.rb
191
+ - lib/schemacop/v2/validator/array_validator.rb
192
+ - lib/schemacop/v2/validator/boolean_validator.rb
193
+ - lib/schemacop/v2/validator/float_validator.rb
194
+ - lib/schemacop/v2/validator/hash_validator.rb
195
+ - lib/schemacop/v2/validator/integer_validator.rb
196
+ - lib/schemacop/v2/validator/nil_validator.rb
197
+ - lib/schemacop/v2/validator/number_validator.rb
198
+ - lib/schemacop/v2/validator/object_validator.rb
199
+ - lib/schemacop/v2/validator/string_validator.rb
200
+ - lib/schemacop/v2/validator/symbol_validator.rb
201
+ - lib/schemacop/v3.rb
202
+ - lib/schemacop/v3/all_of_node.rb
203
+ - lib/schemacop/v3/any_of_node.rb
204
+ - lib/schemacop/v3/array_node.rb
205
+ - lib/schemacop/v3/boolean_node.rb
206
+ - lib/schemacop/v3/combination_node.rb
207
+ - lib/schemacop/v3/context.rb
208
+ - lib/schemacop/v3/dsl_scope.rb
209
+ - lib/schemacop/v3/global_context.rb
210
+ - lib/schemacop/v3/hash_node.rb
211
+ - lib/schemacop/v3/integer_node.rb
212
+ - lib/schemacop/v3/is_not_node.rb
213
+ - lib/schemacop/v3/node.rb
214
+ - lib/schemacop/v3/node_registry.rb
215
+ - lib/schemacop/v3/number_node.rb
216
+ - lib/schemacop/v3/numeric_node.rb
217
+ - lib/schemacop/v3/object_node.rb
218
+ - lib/schemacop/v3/one_of_node.rb
219
+ - lib/schemacop/v3/reference_node.rb
220
+ - lib/schemacop/v3/result.rb
221
+ - lib/schemacop/v3/string_node.rb
222
+ - lib/schemacop/v3/symbol_node.rb
251
223
  - schemacop.gemspec
252
- - test/casting_test.rb
253
- - test/collector_test.rb
254
- - test/custom_check_test.rb
255
- - test/custom_if_test.rb
256
- - test/defaults_test.rb
257
- - test/empty_test.rb
258
- - test/nil_dis_allow_test.rb
259
- - test/node_resolver_test.rb
260
- - test/short_forms_test.rb
261
- - test/test_helper.rb
262
- - test/types_test.rb
263
- - test/validator_array_test.rb
264
- - test/validator_boolean_test.rb
265
- - test/validator_float_test.rb
266
- - test/validator_hash_test.rb
267
- - test/validator_integer_test.rb
268
- - test/validator_nil_test.rb
269
- - test/validator_number_test.rb
270
- - test/validator_object_test.rb
271
- - test/validator_string_test.rb
272
- - test/validator_symbol_test.rb
224
+ - test/lib/test_helper.rb
225
+ - test/schemas/nested/group.rb
226
+ - test/schemas/user.rb
227
+ - test/unit/schemacop/v2/casting_test.rb
228
+ - test/unit/schemacop/v2/collector_test.rb
229
+ - test/unit/schemacop/v2/custom_check_test.rb
230
+ - test/unit/schemacop/v2/custom_if_test.rb
231
+ - test/unit/schemacop/v2/defaults_test.rb
232
+ - test/unit/schemacop/v2/empty_test.rb
233
+ - test/unit/schemacop/v2/nil_dis_allow_test.rb
234
+ - test/unit/schemacop/v2/node_resolver_test.rb
235
+ - test/unit/schemacop/v2/short_forms_test.rb
236
+ - test/unit/schemacop/v2/types_test.rb
237
+ - test/unit/schemacop/v2/validator_array_test.rb
238
+ - test/unit/schemacop/v2/validator_boolean_test.rb
239
+ - test/unit/schemacop/v2/validator_float_test.rb
240
+ - test/unit/schemacop/v2/validator_hash_test.rb
241
+ - test/unit/schemacop/v2/validator_integer_test.rb
242
+ - test/unit/schemacop/v2/validator_nil_test.rb
243
+ - test/unit/schemacop/v2/validator_number_test.rb
244
+ - test/unit/schemacop/v2/validator_object_test.rb
245
+ - test/unit/schemacop/v2/validator_string_test.rb
246
+ - test/unit/schemacop/v2/validator_symbol_test.rb
247
+ - test/unit/schemacop/v3/all_of_node_test.rb
248
+ - test/unit/schemacop/v3/any_of_node_test.rb
249
+ - test/unit/schemacop/v3/array_node_test.rb
250
+ - test/unit/schemacop/v3/boolean_node_test.rb
251
+ - test/unit/schemacop/v3/global_context_test.rb
252
+ - test/unit/schemacop/v3/hash_node_test.rb
253
+ - test/unit/schemacop/v3/integer_node_test.rb
254
+ - test/unit/schemacop/v3/is_not_node_test.rb
255
+ - test/unit/schemacop/v3/node_test.rb
256
+ - test/unit/schemacop/v3/number_node_test.rb
257
+ - test/unit/schemacop/v3/object_node_test.rb
258
+ - test/unit/schemacop/v3/one_of_node_test.rb
259
+ - test/unit/schemacop/v3/reference_node_test.rb
260
+ - test/unit/schemacop/v3/string_node_test.rb
261
+ - test/unit/schemacop/v3/symbol_node_test.rb
273
262
  homepage: https://github.com/sitrox/schemacop
274
263
  licenses:
275
264
  - MIT
@@ -285,34 +274,51 @@ required_ruby_version: !ruby/object:Gem::Requirement
285
274
  version: '0'
286
275
  required_rubygems_version: !ruby/object:Gem::Requirement
287
276
  requirements:
288
- - - ">="
277
+ - - ">"
289
278
  - !ruby/object:Gem::Version
290
- version: '0'
279
+ version: 1.3.1
291
280
  requirements: []
292
- rubygems_version: 3.1.2
281
+ rubygems_version: 3.0.3
293
282
  signing_key:
294
283
  specification_version: 4
295
284
  summary: Schemacop validates ruby structures consisting of nested hashes and arrays
296
285
  against simple schema definitions.
297
286
  test_files:
298
- - test/casting_test.rb
299
- - test/collector_test.rb
300
- - test/custom_check_test.rb
301
- - test/custom_if_test.rb
302
- - test/defaults_test.rb
303
- - test/empty_test.rb
304
- - test/nil_dis_allow_test.rb
305
- - test/node_resolver_test.rb
306
- - test/short_forms_test.rb
307
- - test/test_helper.rb
308
- - test/types_test.rb
309
- - test/validator_array_test.rb
310
- - test/validator_boolean_test.rb
311
- - test/validator_float_test.rb
312
- - test/validator_hash_test.rb
313
- - test/validator_integer_test.rb
314
- - test/validator_nil_test.rb
315
- - test/validator_number_test.rb
316
- - test/validator_object_test.rb
317
- - test/validator_string_test.rb
318
- - test/validator_symbol_test.rb
287
+ - test/lib/test_helper.rb
288
+ - test/schemas/nested/group.rb
289
+ - test/schemas/user.rb
290
+ - test/unit/schemacop/v2/casting_test.rb
291
+ - test/unit/schemacop/v2/collector_test.rb
292
+ - test/unit/schemacop/v2/custom_check_test.rb
293
+ - test/unit/schemacop/v2/custom_if_test.rb
294
+ - test/unit/schemacop/v2/defaults_test.rb
295
+ - test/unit/schemacop/v2/empty_test.rb
296
+ - test/unit/schemacop/v2/nil_dis_allow_test.rb
297
+ - test/unit/schemacop/v2/node_resolver_test.rb
298
+ - test/unit/schemacop/v2/short_forms_test.rb
299
+ - test/unit/schemacop/v2/types_test.rb
300
+ - test/unit/schemacop/v2/validator_array_test.rb
301
+ - test/unit/schemacop/v2/validator_boolean_test.rb
302
+ - test/unit/schemacop/v2/validator_float_test.rb
303
+ - test/unit/schemacop/v2/validator_hash_test.rb
304
+ - test/unit/schemacop/v2/validator_integer_test.rb
305
+ - test/unit/schemacop/v2/validator_nil_test.rb
306
+ - test/unit/schemacop/v2/validator_number_test.rb
307
+ - test/unit/schemacop/v2/validator_object_test.rb
308
+ - test/unit/schemacop/v2/validator_string_test.rb
309
+ - test/unit/schemacop/v2/validator_symbol_test.rb
310
+ - test/unit/schemacop/v3/all_of_node_test.rb
311
+ - test/unit/schemacop/v3/any_of_node_test.rb
312
+ - test/unit/schemacop/v3/array_node_test.rb
313
+ - test/unit/schemacop/v3/boolean_node_test.rb
314
+ - test/unit/schemacop/v3/global_context_test.rb
315
+ - test/unit/schemacop/v3/hash_node_test.rb
316
+ - test/unit/schemacop/v3/integer_node_test.rb
317
+ - test/unit/schemacop/v3/is_not_node_test.rb
318
+ - test/unit/schemacop/v3/node_test.rb
319
+ - test/unit/schemacop/v3/number_node_test.rb
320
+ - test/unit/schemacop/v3/object_node_test.rb
321
+ - test/unit/schemacop/v3/one_of_node_test.rb
322
+ - test/unit/schemacop/v3/reference_node_test.rb
323
+ - test/unit/schemacop/v3/string_node_test.rb
324
+ - test/unit/schemacop/v3/symbol_node_test.rb