dry-validation 0.7.4 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +9 -6
  4. data/CHANGELOG.md +58 -0
  5. data/Gemfile +3 -3
  6. data/benchmarks/benchmark_form_invalid.rb +64 -0
  7. data/benchmarks/benchmark_form_valid.rb +64 -0
  8. data/benchmarks/profile_schema_call_invalid.rb +20 -0
  9. data/benchmarks/profile_schema_call_valid.rb +20 -0
  10. data/benchmarks/profile_schema_definition.rb +14 -0
  11. data/benchmarks/profile_schema_messages_invalid.rb +20 -0
  12. data/benchmarks/suite.rb +5 -0
  13. data/config/errors.yml +12 -5
  14. data/dry-validation.gemspec +2 -2
  15. data/examples/basic.rb +2 -2
  16. data/examples/form.rb +2 -2
  17. data/examples/json.rb +2 -2
  18. data/examples/nested.rb +6 -6
  19. data/lib/dry/validation.rb +22 -3
  20. data/lib/dry/validation/deprecations.rb +23 -0
  21. data/lib/dry/validation/error_compiler.rb +31 -11
  22. data/lib/dry/validation/error_compiler/input.rb +44 -57
  23. data/lib/dry/validation/hint_compiler.rb +15 -7
  24. data/lib/dry/validation/input_processor_compiler.rb +13 -6
  25. data/lib/dry/validation/input_processor_compiler/form.rb +2 -0
  26. data/lib/dry/validation/input_processor_compiler/sanitizer.rb +1 -1
  27. data/lib/dry/validation/messages/abstract.rb +9 -1
  28. data/lib/dry/validation/predicate_registry.rb +101 -0
  29. data/lib/dry/validation/result.rb +8 -1
  30. data/lib/dry/validation/schema.rb +110 -44
  31. data/lib/dry/validation/schema/check.rb +4 -2
  32. data/lib/dry/validation/schema/deprecated.rb +31 -0
  33. data/lib/dry/validation/schema/dsl.rb +18 -11
  34. data/lib/dry/validation/schema/form.rb +1 -0
  35. data/lib/dry/validation/schema/json.rb +1 -0
  36. data/lib/dry/validation/schema/key.rb +23 -10
  37. data/lib/dry/validation/schema/rule.rb +81 -20
  38. data/lib/dry/validation/schema/value.rb +110 -25
  39. data/lib/dry/validation/version.rb +1 -1
  40. data/spec/fixtures/locales/en.yml +1 -0
  41. data/spec/fixtures/locales/pl.yml +1 -1
  42. data/spec/integration/custom_error_messages_spec.rb +2 -2
  43. data/spec/integration/custom_predicates_spec.rb +98 -15
  44. data/spec/integration/error_compiler_spec.rb +111 -96
  45. data/spec/integration/form/predicates/array_spec.rb +243 -0
  46. data/spec/integration/form/predicates/empty_spec.rb +263 -0
  47. data/spec/integration/form/predicates/eql_spec.rb +327 -0
  48. data/spec/integration/form/predicates/even_spec.rb +455 -0
  49. data/spec/integration/form/predicates/excluded_from_spec.rb +455 -0
  50. data/spec/integration/form/predicates/excludes_spec.rb +391 -0
  51. data/spec/integration/form/predicates/false_spec.rb +455 -0
  52. data/spec/integration/form/predicates/filled_spec.rb +467 -0
  53. data/spec/integration/form/predicates/format_spec.rb +454 -0
  54. data/spec/integration/form/predicates/gt_spec.rb +519 -0
  55. data/spec/integration/form/predicates/gteq_spec.rb +519 -0
  56. data/spec/integration/form/predicates/included_in_spec.rb +455 -0
  57. data/spec/integration/form/predicates/includes_spec.rb +391 -0
  58. data/spec/integration/form/predicates/key_spec.rb +75 -0
  59. data/spec/integration/form/predicates/lt_spec.rb +519 -0
  60. data/spec/integration/form/predicates/lteq_spec.rb +519 -0
  61. data/spec/integration/form/predicates/max_size_spec.rb +391 -0
  62. data/spec/integration/form/predicates/min_size_spec.rb +391 -0
  63. data/spec/integration/form/predicates/none_spec.rb +265 -0
  64. data/spec/integration/form/predicates/not_eql_spec.rb +327 -0
  65. data/spec/integration/form/predicates/odd_spec.rb +455 -0
  66. data/spec/integration/form/predicates/size/fixed_spec.rb +399 -0
  67. data/spec/integration/form/predicates/size/range_spec.rb +398 -0
  68. data/spec/integration/form/predicates/true_spec.rb +455 -0
  69. data/spec/integration/form/predicates/type_spec.rb +391 -0
  70. data/spec/integration/hints_spec.rb +90 -4
  71. data/spec/integration/injecting_rules_spec.rb +2 -2
  72. data/spec/integration/localized_error_messages_spec.rb +2 -2
  73. data/spec/integration/messages/i18n_spec.rb +3 -3
  74. data/spec/integration/optional_keys_spec.rb +3 -3
  75. data/spec/integration/schema/array_schema_spec.rb +49 -13
  76. data/spec/integration/schema/check_rules_spec.rb +6 -6
  77. data/spec/integration/schema/check_with_nested_el_spec.rb +3 -3
  78. data/spec/integration/schema/check_with_nth_el_spec.rb +1 -1
  79. data/spec/integration/schema/each_with_set_spec.rb +3 -3
  80. data/spec/integration/schema/extending_dsl_spec.rb +27 -0
  81. data/spec/integration/schema/form/explicit_types_spec.rb +182 -0
  82. data/spec/integration/schema/form_spec.rb +90 -18
  83. data/spec/integration/schema/hash_schema_spec.rb +47 -0
  84. data/spec/integration/schema/inheriting_schema_spec.rb +4 -4
  85. data/spec/integration/schema/input_processor_spec.rb +8 -8
  86. data/spec/integration/schema/json/explicit_types_spec.rb +157 -0
  87. data/spec/integration/schema/json_spec.rb +18 -18
  88. data/spec/integration/schema/macros/confirmation_spec.rb +1 -1
  89. data/spec/integration/schema/macros/each_spec.rb +177 -43
  90. data/spec/integration/schema/macros/{required_spec.rb → filled_spec.rb} +34 -6
  91. data/spec/integration/schema/macros/input_spec.rb +21 -0
  92. data/spec/integration/schema/macros/maybe_spec.rb +39 -2
  93. data/spec/integration/schema/macros/value_spec.rb +105 -0
  94. data/spec/integration/schema/macros/when_spec.rb +28 -8
  95. data/spec/integration/schema/nested_values_spec.rb +11 -8
  96. data/spec/integration/schema/not_spec.rb +2 -2
  97. data/spec/integration/schema/numbers_spec.rb +1 -1
  98. data/spec/integration/schema/option_with_default_spec.rb +1 -1
  99. data/spec/integration/schema/predicate_verification_spec.rb +9 -0
  100. data/spec/integration/schema/predicates/array_spec.rb +295 -0
  101. data/spec/integration/schema/predicates/custom_spec.rb +103 -0
  102. data/spec/integration/schema/predicates/empty_spec.rb +263 -0
  103. data/spec/integration/schema/predicates/eql_spec.rb +327 -0
  104. data/spec/integration/schema/predicates/even_spec.rb +455 -0
  105. data/spec/integration/schema/predicates/excluded_from_spec.rb +455 -0
  106. data/spec/integration/schema/predicates/excludes_spec.rb +391 -0
  107. data/spec/integration/schema/predicates/filled_spec.rb +467 -0
  108. data/spec/integration/schema/predicates/format_spec.rb +455 -0
  109. data/spec/integration/schema/predicates/gt_spec.rb +519 -0
  110. data/spec/integration/schema/predicates/gteq_spec.rb +519 -0
  111. data/spec/integration/schema/predicates/hash_spec.rb +69 -0
  112. data/spec/integration/schema/predicates/included_in_spec.rb +455 -0
  113. data/spec/integration/schema/predicates/includes_spec.rb +391 -0
  114. data/spec/integration/schema/predicates/key_spec.rb +88 -0
  115. data/spec/integration/schema/predicates/lt_spec.rb +520 -0
  116. data/spec/integration/schema/predicates/lteq_spec.rb +519 -0
  117. data/spec/integration/schema/predicates/max_size_spec.rb +391 -0
  118. data/spec/integration/schema/predicates/min_size_spec.rb +391 -0
  119. data/spec/integration/schema/predicates/none_spec.rb +265 -0
  120. data/spec/integration/schema/predicates/not_eql_spec.rb +391 -0
  121. data/spec/integration/schema/predicates/odd_spec.rb +455 -0
  122. data/spec/integration/schema/predicates/size/fixed_spec.rb +401 -0
  123. data/spec/integration/schema/predicates/size/range_spec.rb +399 -0
  124. data/spec/integration/schema/predicates/type_spec.rb +391 -0
  125. data/spec/integration/schema/reusing_schema_spec.rb +4 -4
  126. data/spec/integration/schema/using_types_spec.rb +24 -6
  127. data/spec/integration/schema/xor_spec.rb +2 -2
  128. data/spec/integration/schema_builders_spec.rb +15 -0
  129. data/spec/integration/schema_spec.rb +37 -12
  130. data/spec/shared/predicate_helper.rb +13 -0
  131. data/spec/spec_helper.rb +10 -0
  132. data/spec/support/matchers.rb +38 -0
  133. data/spec/support/predicates_integration.rb +7 -0
  134. data/spec/unit/hint_compiler_spec.rb +10 -8
  135. data/spec/unit/input_processor_compiler/form_spec.rb +45 -43
  136. data/spec/unit/input_processor_compiler/json_spec.rb +37 -35
  137. data/spec/unit/predicate_registry_spec.rb +34 -0
  138. data/spec/unit/schema/key_spec.rb +12 -14
  139. data/spec/unit/schema/rule_spec.rb +4 -2
  140. data/spec/unit/schema/value_spec.rb +38 -121
  141. metadata +150 -16
  142. data/lib/dry/validation/schema/attr.rb +0 -15
  143. data/spec/integration/attr_spec.rb +0 -122
@@ -0,0 +1,399 @@
1
+ RSpec.describe 'Predicates: Size' do
2
+ context 'Fixed (integer)' do
3
+ context 'with required' do
4
+ subject(:schema) do
5
+ Dry::Validation.Form do
6
+ required(:foo) { size?(3) }
7
+ end
8
+ end
9
+
10
+ context 'with valid input' do
11
+ let(:input) { { 'foo' => %w(1 2 3) } }
12
+
13
+ it 'is successful' do
14
+ expect(result).to be_successful
15
+ end
16
+ end
17
+
18
+ context 'with missing input' do
19
+ let(:input) { {} }
20
+
21
+ it 'is not successful' do
22
+ expect(result).to be_failing ['is missing', 'size must be 3']
23
+ end
24
+ end
25
+
26
+ context 'with nil input' do
27
+ let(:input) { { 'foo' => nil } }
28
+
29
+ it 'is raises error' do
30
+ expect { result }.to raise_error(NoMethodError)
31
+ end
32
+ end
33
+
34
+ context 'with blank input' do
35
+ let(:input) { { 'foo' => '' } }
36
+
37
+ #see: https://github.com/dry-rb/dry-validation/issues/121
38
+ xit 'is not successful' do
39
+ expect(result).to be_failing ['length must be 3']
40
+ end
41
+ end
42
+
43
+ context 'with invalid input' do
44
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
45
+
46
+ it 'is not successful' do
47
+ expect(result).to be_failing ['size must be 3']
48
+ end
49
+ end
50
+ end
51
+
52
+ context 'with optional' do
53
+ subject(:schema) do
54
+ Dry::Validation.Form do
55
+ optional(:foo) { size?(3) }
56
+ end
57
+ end
58
+
59
+ context 'with valid input' do
60
+ let(:input) { { 'foo' => %w(1 2 3) } }
61
+
62
+ it 'is successful' do
63
+ expect(result).to be_successful
64
+ end
65
+ end
66
+
67
+ context 'with missing input' do
68
+ let(:input) { {} }
69
+
70
+ it 'is successful' do
71
+ expect(result).to be_successful
72
+ end
73
+ end
74
+
75
+ context 'with nil input' do
76
+ let(:input) { { 'foo' => nil } }
77
+
78
+ it 'is raises error' do
79
+ expect { result }.to raise_error(NoMethodError)
80
+ end
81
+ end
82
+
83
+ context 'with blank input' do
84
+ let(:input) { { 'foo' => '' } }
85
+
86
+ #see: https://github.com/dry-rb/dry-validation/issues/121
87
+ xit 'is not successful' do
88
+ expect(result).to be_failing ['length must be 3']
89
+ end
90
+ end
91
+
92
+ context 'with invalid input' do
93
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
94
+
95
+ it 'is not successful' do
96
+ expect(result).to be_failing ['size must be 3']
97
+ end
98
+ end
99
+ end
100
+
101
+ context 'as macro' do
102
+ context 'with required' do
103
+ context 'with value' do
104
+ subject(:schema) do
105
+ Dry::Validation.Form do
106
+ required(:foo).value(size?: 3)
107
+ end
108
+ end
109
+
110
+ context 'with valid input' do
111
+ let(:input) { { 'foo' => %w(1 2 3) } }
112
+
113
+ it 'is successful' do
114
+ expect(result).to be_successful
115
+ end
116
+ end
117
+
118
+ context 'with missing input' do
119
+ let(:input) { {} }
120
+
121
+ it 'is not successful' do
122
+ expect(result).to be_failing ['is missing', 'size must be 3']
123
+ end
124
+ end
125
+
126
+ context 'with nil input' do
127
+ let(:input) { { 'foo' => nil } }
128
+
129
+ it 'is raises error' do
130
+ expect { result }.to raise_error(NoMethodError)
131
+ end
132
+ end
133
+
134
+ context 'with blank input' do
135
+ let(:input) { { 'foo' => '' } }
136
+
137
+ #see: https://github.com/dry-rb/dry-validation/issues/121
138
+ xit 'is not successful' do
139
+ expect(result).to be_failing ['length must be 3']
140
+ end
141
+ end
142
+
143
+ context 'with invalid input' do
144
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
145
+
146
+ it 'is not successful' do
147
+ expect(result).to be_failing ['size must be 3']
148
+ end
149
+ end
150
+ end
151
+
152
+ context 'with filled' do
153
+ subject(:schema) do
154
+ Dry::Validation.Form do
155
+ required(:foo).filled(size?: 3)
156
+ end
157
+ end
158
+
159
+ context 'with valid input' do
160
+ let(:input) { { 'foo' => %w(1 2 3) } }
161
+
162
+ it 'is successful' do
163
+ expect(result).to be_successful
164
+ end
165
+ end
166
+
167
+ context 'with missing input' do
168
+ let(:input) { {} }
169
+
170
+ it 'is not successful' do
171
+ expect(result).to be_failing ['is missing', 'size must be 3']
172
+ end
173
+ end
174
+
175
+ context 'with nil input' do
176
+ let(:input) { { 'foo' => nil } }
177
+
178
+ it 'is not successful' do
179
+ expect(result).to be_failing ['must be filled', 'size must be 3']
180
+ end
181
+ end
182
+
183
+ context 'with blank input' do
184
+ let(:input) { { 'foo' => '' } }
185
+
186
+ #see: https://github.com/dry-rb/dry-validation/issues/121
187
+ xit 'is not successful' do
188
+ expect(result).to be_failing ['must be filled', 'length must be 3']
189
+ end
190
+ end
191
+
192
+ context 'with invalid input' do
193
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
194
+
195
+ it 'is not successful' do
196
+ expect(result).to be_failing ['size must be 3']
197
+ end
198
+ end
199
+ end
200
+
201
+ context 'with maybe' do
202
+ subject(:schema) do
203
+ Dry::Validation.Form do
204
+ required(:foo).maybe(size?: 3)
205
+ end
206
+ end
207
+
208
+ context 'with valid input' do
209
+ let(:input) { { 'foo' => %w(1 2 3) } }
210
+
211
+ it 'is successful' do
212
+ expect(result).to be_successful
213
+ end
214
+ end
215
+
216
+ context 'with missing input' do
217
+ let(:input) { {} }
218
+
219
+ it 'is not successful' do
220
+ expect(result).to be_failing ['is missing', 'size must be 3']
221
+ end
222
+ end
223
+
224
+ context 'with nil input' do
225
+ let(:input) { { 'foo' => nil } }
226
+
227
+ it 'is successful' do
228
+ expect(result).to be_successful
229
+ end
230
+ end
231
+
232
+ context 'with blank input' do
233
+ let(:input) { { 'foo' => '' } }
234
+
235
+ it 'is successful' do
236
+ expect(result).to be_successful
237
+ end
238
+ end
239
+
240
+ context 'with invalid input' do
241
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
242
+
243
+ it 'is not successful' do
244
+ expect(result).to be_failing ['size must be 3']
245
+ end
246
+ end
247
+ end
248
+ end
249
+
250
+ context 'with optional' do
251
+ context 'with value' do
252
+ subject(:schema) do
253
+ Dry::Validation.Form do
254
+ optional(:foo).value(size?: 3)
255
+ end
256
+ end
257
+
258
+ context 'with valid input' do
259
+ let(:input) { { 'foo' => %w(1 2 3) } }
260
+
261
+ it 'is successful' do
262
+ expect(result).to be_successful
263
+ end
264
+ end
265
+
266
+ context 'with missing input' do
267
+ let(:input) { {} }
268
+
269
+ it 'is successful' do
270
+ expect(result).to be_successful
271
+ end
272
+ end
273
+
274
+ context 'with nil input' do
275
+ let(:input) { { 'foo' => nil } }
276
+
277
+ it 'is raises error' do
278
+ expect { result }.to raise_error(NoMethodError)
279
+ end
280
+ end
281
+
282
+ context 'with blank input' do
283
+ let(:input) { { 'foo' => '' } }
284
+
285
+ #see: https://github.com/dry-rb/dry-validation/issues/121
286
+ xit 'is not successful' do
287
+ expect(result).to be_failing ['length must be 3']
288
+ end
289
+ end
290
+
291
+ context 'with invalid input' do
292
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
293
+
294
+ it 'is not successful' do
295
+ expect(result).to be_failing ['size must be 3']
296
+ end
297
+ end
298
+ end
299
+
300
+ context 'with filled' do
301
+ subject(:schema) do
302
+ Dry::Validation.Form do
303
+ optional(:foo).filled(size?: 3)
304
+ end
305
+ end
306
+
307
+ context 'with valid input' do
308
+ let(:input) { { 'foo' => %w(1 2 3) } }
309
+
310
+ it 'is successful' do
311
+ expect(result).to be_successful
312
+ end
313
+ end
314
+
315
+ context 'with missing input' do
316
+ let(:input) { {} }
317
+
318
+ it 'is successful' do
319
+ expect(result).to be_successful
320
+ end
321
+ end
322
+
323
+ context 'with nil input' do
324
+ let(:input) { { 'foo' => nil } }
325
+
326
+ it 'is not successful' do
327
+ expect(result).to be_failing ['must be filled', 'size must be 3']
328
+ end
329
+ end
330
+
331
+ context 'with blank input' do
332
+ let(:input) { { 'foo' => '' } }
333
+
334
+ #see: https://github.com/dry-rb/dry-validation/issues/121
335
+ xit 'is not successful' do
336
+ expect(result).to be_failing ['must be filled', 'length must be 3']
337
+ end
338
+ end
339
+
340
+ context 'with invalid input' do
341
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
342
+
343
+ it 'is not successful' do
344
+ expect(result).to be_failing ['size must be 3']
345
+ end
346
+ end
347
+ end
348
+
349
+ context 'with maybe' do
350
+ subject(:schema) do
351
+ Dry::Validation.Form do
352
+ optional(:foo).maybe(size?: 3)
353
+ end
354
+ end
355
+
356
+ context 'with valid input' do
357
+ let(:input) { { 'foo' => %w(1 2 3) } }
358
+
359
+ it 'is successful' do
360
+ expect(result).to be_successful
361
+ end
362
+ end
363
+
364
+ context 'with missing input' do
365
+ let(:input) { {} }
366
+
367
+ it 'is successful' do
368
+ expect(result).to be_successful
369
+ end
370
+ end
371
+
372
+ context 'with nil input' do
373
+ let(:input) { { 'foo' => nil } }
374
+
375
+ it 'is successful' do
376
+ expect(result).to be_successful
377
+ end
378
+ end
379
+
380
+ context 'with blank input' do
381
+ let(:input) { { 'foo' => '' } }
382
+
383
+ it 'is successful' do
384
+ expect(result).to be_successful
385
+ end
386
+ end
387
+
388
+ context 'with invalid input' do
389
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
390
+
391
+ it 'is not successful' do
392
+ expect(result).to be_failing ['size must be 3']
393
+ end
394
+ end
395
+ end
396
+ end
397
+ end
398
+ end
399
+ end
@@ -0,0 +1,398 @@
1
+ RSpec.describe 'Predicates: Size' do
2
+ context 'Range' do
3
+ context 'with required' do
4
+ subject(:schema) do
5
+ Dry::Validation.Form do
6
+ required(:foo) { size?(2..3) }
7
+ end
8
+ end
9
+
10
+ context 'with valid input' do
11
+ let(:input) { { 'foo' => %w(1 2 3) } }
12
+
13
+ it 'is successful' do
14
+ expect(result).to be_successful
15
+ end
16
+ end
17
+
18
+ context 'with missing input' do
19
+ let(:input) { {} }
20
+
21
+ it 'is not successful' do
22
+ expect(result).to be_failing ['is missing', 'size must be within 2 - 3']
23
+ end
24
+ end
25
+
26
+ context 'with nil input' do
27
+ let(:input) { { 'foo' => nil } }
28
+
29
+ it 'is raises error' do
30
+ expect { result }.to raise_error(NoMethodError)
31
+ end
32
+ end
33
+
34
+ context 'with blank input' do
35
+ let(:input) { { 'foo' => '' } }
36
+
37
+ #see: https://github.com/dry-rb/dry-validation/issues/121
38
+ xit 'is not successful' do
39
+ expect(result).to be_failing ['length must be within 2 - 3']
40
+ end
41
+ end
42
+
43
+ context 'with invalid input' do
44
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
45
+
46
+ it 'is not successful' do
47
+ expect(result).to be_failing ['size must be within 2 - 3']
48
+ end
49
+ end
50
+ end
51
+
52
+ context 'with optional' do
53
+ subject(:schema) do
54
+ Dry::Validation.Form do
55
+ optional(:foo) { size?(2..3) }
56
+ end
57
+ end
58
+
59
+ context 'with valid input' do
60
+ let(:input) { { 'foo' => %w(1 2 3) } }
61
+
62
+ it 'is successful' do
63
+ expect(result).to be_successful
64
+ end
65
+ end
66
+
67
+ context 'with missing input' do
68
+ let(:input) { {} }
69
+
70
+ it 'is successful' do
71
+ expect(result).to be_successful
72
+ end
73
+ end
74
+
75
+ context 'with nil input' do
76
+ let(:input) { { 'foo' => nil } }
77
+
78
+ it 'is raises error' do
79
+ expect { result }.to raise_error(NoMethodError)
80
+ end
81
+ end
82
+
83
+ context 'with blank input' do
84
+ let(:input) { { 'foo' => '' } }
85
+
86
+ #see: https://github.com/dry-rb/dry-validation/issues/121
87
+ xit 'is not successful' do
88
+ expect(result).to be_failing ['length must be within 2 - 3']
89
+ end
90
+ end
91
+
92
+ context 'with invalid input' do
93
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
94
+
95
+ it 'is not successful' do
96
+ expect(result).to be_failing ['size must be within 2 - 3']
97
+ end
98
+ end
99
+ end
100
+
101
+ context 'as macro' do
102
+ context 'with required' do
103
+ context 'with value' do
104
+ subject(:schema) do
105
+ Dry::Validation.Form do
106
+ required(:foo).value(size?: 2..3)
107
+ end
108
+ end
109
+
110
+ context 'with valid input' do
111
+ let(:input) { { 'foo' => %w(1 2 3) } }
112
+
113
+ it 'is successful' do
114
+ expect(result).to be_successful
115
+ end
116
+ end
117
+
118
+ context 'with missing input' do
119
+ let(:input) { {} }
120
+
121
+ it 'is not successful' do
122
+ expect(result).to be_failing ['is missing', 'size must be within 2 - 3']
123
+ end
124
+ end
125
+
126
+ context 'with nil input' do
127
+ let(:input) { { 'foo' => nil } }
128
+
129
+ it 'is raises error' do
130
+ expect { result }.to raise_error(NoMethodError)
131
+ end
132
+ end
133
+
134
+ context 'with blank input' do
135
+ let(:input) { { 'foo' => '' } }
136
+
137
+ it 'is not successful' do
138
+ expect(result).to be_failing ['length must be within 2 - 3']
139
+ end
140
+ end
141
+
142
+ context 'with invalid input' do
143
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
144
+
145
+ it 'is not successful' do
146
+ expect(result).to be_failing ['size must be within 2 - 3']
147
+ end
148
+ end
149
+ end
150
+
151
+ context 'with filled' do
152
+ subject(:schema) do
153
+ Dry::Validation.Form do
154
+ required(:foo).filled(size?: 2..3)
155
+ end
156
+ end
157
+
158
+ context 'with valid input' do
159
+ let(:input) { { 'foo' => %w(1 2 3) } }
160
+
161
+ it 'is successful' do
162
+ expect(result).to be_successful
163
+ end
164
+ end
165
+
166
+ context 'with missing input' do
167
+ let(:input) { {} }
168
+
169
+ it 'is not successful' do
170
+ expect(result).to be_failing ['is missing', 'size must be within 2 - 3']
171
+ end
172
+ end
173
+
174
+ context 'with nil input' do
175
+ let(:input) { { 'foo' => nil } }
176
+
177
+ it 'is not successful' do
178
+ expect(result).to be_failing ['must be filled', 'size must be within 2 - 3']
179
+ end
180
+ end
181
+
182
+ context 'with blank input' do
183
+ let(:input) { { 'foo' => '' } }
184
+
185
+ #see: https://github.com/dry-rb/dry-validation/issues/121
186
+ xit 'is not successful' do
187
+ expect(result).to be_failing ['must be filled', 'length must be within 2 - 3']
188
+ end
189
+ end
190
+
191
+ context 'with invalid input' do
192
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
193
+
194
+ it 'is not successful' do
195
+ expect(result).to be_failing ['size must be within 2 - 3']
196
+ end
197
+ end
198
+ end
199
+
200
+ context 'with maybe' do
201
+ subject(:schema) do
202
+ Dry::Validation.Form do
203
+ required(:foo).maybe(size?: 2..3)
204
+ end
205
+ end
206
+
207
+ context 'with valid input' do
208
+ let(:input) { { 'foo' => %w(1 2 3) } }
209
+
210
+ it 'is successful' do
211
+ expect(result).to be_successful
212
+ end
213
+ end
214
+
215
+ context 'with missing input' do
216
+ let(:input) { {} }
217
+
218
+ it 'is not successful' do
219
+ expect(result).to be_failing ['is missing', 'size must be within 2 - 3']
220
+ end
221
+ end
222
+
223
+ context 'with nil input' do
224
+ let(:input) { { 'foo' => nil } }
225
+
226
+ it 'is successful' do
227
+ expect(result).to be_successful
228
+ end
229
+ end
230
+
231
+ context 'with blank input' do
232
+ let(:input) { { 'foo' => '' } }
233
+
234
+ it 'is successful' do
235
+ expect(result).to be_successful
236
+ end
237
+ end
238
+
239
+ context 'with invalid input' do
240
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
241
+
242
+ it 'is not successful' do
243
+ expect(result).to be_failing ['size must be within 2 - 3']
244
+ end
245
+ end
246
+ end
247
+ end
248
+
249
+ context 'with optional' do
250
+ context 'with value' do
251
+ subject(:schema) do
252
+ Dry::Validation.Form do
253
+ optional(:foo).value(size?: 2..3)
254
+ end
255
+ end
256
+
257
+ context 'with valid input' do
258
+ let(:input) { { 'foo' => %w(1 2 3) } }
259
+
260
+ it 'is successful' do
261
+ expect(result).to be_successful
262
+ end
263
+ end
264
+
265
+ context 'with missing input' do
266
+ let(:input) { {} }
267
+
268
+ it 'is successful' do
269
+ expect(result).to be_successful
270
+ end
271
+ end
272
+
273
+ context 'with nil input' do
274
+ let(:input) { { 'foo' => nil } }
275
+
276
+ it 'is raises error' do
277
+ expect { result }.to raise_error(NoMethodError)
278
+ end
279
+ end
280
+
281
+ context 'with blank input' do
282
+ let(:input) { { 'foo' => '' } }
283
+
284
+ #see: https://github.com/dry-rb/dry-validation/issues/121
285
+ xit 'is not successful' do
286
+ expect(result).to be_failing ['length must be within 2 - 3']
287
+ end
288
+ end
289
+
290
+ context 'with invalid input' do
291
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
292
+
293
+ it 'is not successful' do
294
+ expect(result).to be_failing ['size must be within 2 - 3']
295
+ end
296
+ end
297
+ end
298
+
299
+ context 'with filled' do
300
+ subject(:schema) do
301
+ Dry::Validation.Form do
302
+ optional(:foo).filled(size?: 2..3)
303
+ end
304
+ end
305
+
306
+ context 'with valid input' do
307
+ let(:input) { { 'foo' => %w(1 2 3) } }
308
+
309
+ it 'is successful' do
310
+ expect(result).to be_successful
311
+ end
312
+ end
313
+
314
+ context 'with missing input' do
315
+ let(:input) { {} }
316
+
317
+ it 'is successful' do
318
+ expect(result).to be_successful
319
+ end
320
+ end
321
+
322
+ context 'with nil input' do
323
+ let(:input) { { 'foo' => nil } }
324
+
325
+ it 'is not successful' do
326
+ expect(result).to be_failing ['must be filled', 'size must be within 2 - 3']
327
+ end
328
+ end
329
+
330
+ context 'with blank input' do
331
+ let(:input) { { 'foo' => '' } }
332
+
333
+ #see: https://github.com/dry-rb/dry-validation/issues/121
334
+ xit 'is not successful' do
335
+ expect(result).to be_failing ['must be filled', 'length must be within 2 - 3']
336
+ end
337
+ end
338
+
339
+ context 'with invalid input' do
340
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
341
+
342
+ it 'is not successful' do
343
+ expect(result).to be_failing ['size must be within 2 - 3']
344
+ end
345
+ end
346
+ end
347
+
348
+ context 'with maybe' do
349
+ subject(:schema) do
350
+ Dry::Validation.Form do
351
+ optional(:foo).maybe(size?: 2..3)
352
+ end
353
+ end
354
+
355
+ context 'with valid input' do
356
+ let(:input) { { 'foo' => %w(1 2 3) } }
357
+
358
+ it 'is successful' do
359
+ expect(result).to be_successful
360
+ end
361
+ end
362
+
363
+ context 'with missing input' do
364
+ let(:input) { {} }
365
+
366
+ it 'is successful' do
367
+ expect(result).to be_successful
368
+ end
369
+ end
370
+
371
+ context 'with nil input' do
372
+ let(:input) { { 'foo' => nil } }
373
+
374
+ it 'is successful' do
375
+ expect(result).to be_successful
376
+ end
377
+ end
378
+
379
+ context 'with blank input' do
380
+ let(:input) { { 'foo' => '' } }
381
+
382
+ it 'is successful' do
383
+ expect(result).to be_successful
384
+ end
385
+ end
386
+
387
+ context 'with invalid input' do
388
+ let(:input) { { 'foo' => { 'a' => '1', 'b' => '2', 'c' => '3', 'd' => '4' } } }
389
+
390
+ it 'is not successful' do
391
+ expect(result).to be_failing ['size must be within 2 - 3']
392
+ end
393
+ end
394
+ end
395
+ end
396
+ end
397
+ end
398
+ end