dry-validation 0.13.3 → 1.7.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 (207) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +506 -95
  3. data/LICENSE +1 -1
  4. data/README.md +16 -12
  5. data/config/errors.yml +3 -88
  6. data/dry-validation.gemspec +37 -24
  7. data/lib/dry/validation/config.rb +24 -0
  8. data/lib/dry/validation/constants.rb +43 -0
  9. data/lib/dry/validation/contract/class_interface.rb +230 -0
  10. data/lib/dry/validation/contract.rb +171 -0
  11. data/lib/dry/validation/evaluator.rb +224 -0
  12. data/lib/dry/validation/extensions/hints.rb +67 -0
  13. data/lib/dry/validation/extensions/monads.rb +24 -8
  14. data/lib/dry/validation/extensions/predicates_as_macros.rb +75 -0
  15. data/lib/dry/validation/failures.rb +70 -0
  16. data/lib/dry/validation/function.rb +44 -0
  17. data/lib/dry/validation/macro.rb +38 -0
  18. data/lib/dry/validation/macros.rb +104 -0
  19. data/lib/dry/validation/message.rb +80 -80
  20. data/lib/dry/validation/message_set.rb +80 -105
  21. data/lib/dry/validation/messages/resolver.rb +131 -0
  22. data/lib/dry/validation/result.rb +183 -41
  23. data/lib/dry/validation/rule.rb +135 -0
  24. data/lib/dry/validation/schema_ext.rb +19 -0
  25. data/lib/dry/validation/values.rb +104 -0
  26. data/lib/dry/validation/version.rb +3 -1
  27. data/lib/dry/validation.rb +45 -28
  28. data/lib/dry-validation.rb +3 -1
  29. metadata +46 -344
  30. data/.codeclimate.yml +0 -17
  31. data/.gitignore +0 -9
  32. data/.rspec +0 -3
  33. data/.travis.yml +0 -29
  34. data/CONTRIBUTING.md +0 -31
  35. data/Gemfile +0 -25
  36. data/Rakefile +0 -22
  37. data/benchmarks/benchmark_form_invalid.rb +0 -64
  38. data/benchmarks/benchmark_form_valid.rb +0 -64
  39. data/benchmarks/benchmark_schema_invalid_huge.rb +0 -52
  40. data/benchmarks/profile_schema_call_invalid.rb +0 -20
  41. data/benchmarks/profile_schema_call_valid.rb +0 -20
  42. data/benchmarks/profile_schema_definition.rb +0 -14
  43. data/benchmarks/profile_schema_huge_invalid.rb +0 -30
  44. data/benchmarks/profile_schema_messages_invalid.rb +0 -20
  45. data/benchmarks/suite.rb +0 -5
  46. data/examples/basic.rb +0 -15
  47. data/examples/each.rb +0 -14
  48. data/examples/json.rb +0 -12
  49. data/examples/multiple.rb +0 -27
  50. data/examples/nested.rb +0 -22
  51. data/examples/params.rb +0 -11
  52. data/lib/dry/validation/compat/form.rb +0 -67
  53. data/lib/dry/validation/deprecations.rb +0 -24
  54. data/lib/dry/validation/executor.rb +0 -91
  55. data/lib/dry/validation/extensions/struct.rb +0 -32
  56. data/lib/dry/validation/extensions.rb +0 -7
  57. data/lib/dry/validation/input_processor_compiler/json.rb +0 -45
  58. data/lib/dry/validation/input_processor_compiler/params.rb +0 -49
  59. data/lib/dry/validation/input_processor_compiler/sanitizer.rb +0 -47
  60. data/lib/dry/validation/input_processor_compiler.rb +0 -137
  61. data/lib/dry/validation/message_compiler/visitor_opts.rb +0 -37
  62. data/lib/dry/validation/message_compiler.rb +0 -188
  63. data/lib/dry/validation/messages/abstract.rb +0 -119
  64. data/lib/dry/validation/messages/i18n.rb +0 -47
  65. data/lib/dry/validation/messages/namespaced.rb +0 -39
  66. data/lib/dry/validation/messages/yaml.rb +0 -61
  67. data/lib/dry/validation/messages.rb +0 -14
  68. data/lib/dry/validation/predicate_registry.rb +0 -115
  69. data/lib/dry/validation/predicates.rb +0 -19
  70. data/lib/dry/validation/schema/check.rb +0 -37
  71. data/lib/dry/validation/schema/class_interface.rb +0 -190
  72. data/lib/dry/validation/schema/deprecated.rb +0 -30
  73. data/lib/dry/validation/schema/dsl.rb +0 -118
  74. data/lib/dry/validation/schema/form.rb +0 -9
  75. data/lib/dry/validation/schema/json.rb +0 -21
  76. data/lib/dry/validation/schema/key.rb +0 -71
  77. data/lib/dry/validation/schema/params.rb +0 -22
  78. data/lib/dry/validation/schema/rule.rb +0 -202
  79. data/lib/dry/validation/schema/value.rb +0 -211
  80. data/lib/dry/validation/schema.rb +0 -126
  81. data/lib/dry/validation/schema_compiler.rb +0 -81
  82. data/lib/dry/validation/template.rb +0 -66
  83. data/lib/dry/validation/type_specs.rb +0 -70
  84. data/log/.gitkeep +0 -0
  85. data/spec/extensions/monads/result_spec.rb +0 -40
  86. data/spec/extensions/struct/schema_spec.rb +0 -32
  87. data/spec/fixtures/locales/en.yml +0 -8
  88. data/spec/fixtures/locales/pl.yml +0 -22
  89. data/spec/integration/custom_error_messages_spec.rb +0 -54
  90. data/spec/integration/custom_predicates_spec.rb +0 -228
  91. data/spec/integration/hints_spec.rb +0 -170
  92. data/spec/integration/injecting_rules_spec.rb +0 -30
  93. data/spec/integration/json/defining_base_schema_spec.rb +0 -41
  94. data/spec/integration/localized_error_messages_spec.rb +0 -72
  95. data/spec/integration/message_compiler_spec.rb +0 -405
  96. data/spec/integration/messages/i18n_spec.rb +0 -104
  97. data/spec/integration/optional_keys_spec.rb +0 -28
  98. data/spec/integration/params/predicates/array_spec.rb +0 -287
  99. data/spec/integration/params/predicates/empty_spec.rb +0 -263
  100. data/spec/integration/params/predicates/eql_spec.rb +0 -327
  101. data/spec/integration/params/predicates/even_spec.rb +0 -455
  102. data/spec/integration/params/predicates/excluded_from_spec.rb +0 -455
  103. data/spec/integration/params/predicates/excludes_spec.rb +0 -391
  104. data/spec/integration/params/predicates/false_spec.rb +0 -455
  105. data/spec/integration/params/predicates/filled_spec.rb +0 -467
  106. data/spec/integration/params/predicates/format_spec.rb +0 -454
  107. data/spec/integration/params/predicates/gt_spec.rb +0 -519
  108. data/spec/integration/params/predicates/gteq_spec.rb +0 -519
  109. data/spec/integration/params/predicates/included_in_spec.rb +0 -455
  110. data/spec/integration/params/predicates/includes_spec.rb +0 -391
  111. data/spec/integration/params/predicates/key_spec.rb +0 -67
  112. data/spec/integration/params/predicates/lt_spec.rb +0 -519
  113. data/spec/integration/params/predicates/lteq_spec.rb +0 -519
  114. data/spec/integration/params/predicates/max_size_spec.rb +0 -391
  115. data/spec/integration/params/predicates/min_size_spec.rb +0 -391
  116. data/spec/integration/params/predicates/none_spec.rb +0 -265
  117. data/spec/integration/params/predicates/not_eql_spec.rb +0 -327
  118. data/spec/integration/params/predicates/odd_spec.rb +0 -455
  119. data/spec/integration/params/predicates/size/fixed_spec.rb +0 -393
  120. data/spec/integration/params/predicates/size/range_spec.rb +0 -396
  121. data/spec/integration/params/predicates/true_spec.rb +0 -455
  122. data/spec/integration/params/predicates/type_spec.rb +0 -391
  123. data/spec/integration/result_spec.rb +0 -81
  124. data/spec/integration/schema/array_schema_spec.rb +0 -59
  125. data/spec/integration/schema/check_rules_spec.rb +0 -119
  126. data/spec/integration/schema/check_with_nested_el_spec.rb +0 -37
  127. data/spec/integration/schema/check_with_nth_el_spec.rb +0 -25
  128. data/spec/integration/schema/default_settings_spec.rb +0 -11
  129. data/spec/integration/schema/defining_base_schema_spec.rb +0 -41
  130. data/spec/integration/schema/dynamic_predicate_args_spec.rb +0 -43
  131. data/spec/integration/schema/each_with_set_spec.rb +0 -70
  132. data/spec/integration/schema/extending_dsl_spec.rb +0 -27
  133. data/spec/integration/schema/form_spec.rb +0 -236
  134. data/spec/integration/schema/hash_schema_spec.rb +0 -47
  135. data/spec/integration/schema/inheriting_schema_spec.rb +0 -31
  136. data/spec/integration/schema/input_processor_spec.rb +0 -46
  137. data/spec/integration/schema/json/explicit_types_spec.rb +0 -157
  138. data/spec/integration/schema/json_spec.rb +0 -163
  139. data/spec/integration/schema/macros/confirmation_spec.rb +0 -35
  140. data/spec/integration/schema/macros/each_spec.rb +0 -268
  141. data/spec/integration/schema/macros/filled_spec.rb +0 -87
  142. data/spec/integration/schema/macros/input_spec.rb +0 -139
  143. data/spec/integration/schema/macros/maybe_spec.rb +0 -99
  144. data/spec/integration/schema/macros/rule_spec.rb +0 -75
  145. data/spec/integration/schema/macros/value_spec.rb +0 -119
  146. data/spec/integration/schema/macros/when_spec.rb +0 -62
  147. data/spec/integration/schema/nested_schemas_spec.rb +0 -236
  148. data/spec/integration/schema/nested_values_spec.rb +0 -46
  149. data/spec/integration/schema/not_spec.rb +0 -34
  150. data/spec/integration/schema/numbers_spec.rb +0 -19
  151. data/spec/integration/schema/option_with_default_spec.rb +0 -64
  152. data/spec/integration/schema/or_spec.rb +0 -87
  153. data/spec/integration/schema/params/defining_base_schema_spec.rb +0 -41
  154. data/spec/integration/schema/params/explicit_types_spec.rb +0 -195
  155. data/spec/integration/schema/params_spec.rb +0 -234
  156. data/spec/integration/schema/predicate_verification_spec.rb +0 -9
  157. data/spec/integration/schema/predicates/array_spec.rb +0 -295
  158. data/spec/integration/schema/predicates/custom_spec.rb +0 -103
  159. data/spec/integration/schema/predicates/empty_spec.rb +0 -263
  160. data/spec/integration/schema/predicates/eql_spec.rb +0 -327
  161. data/spec/integration/schema/predicates/even_spec.rb +0 -455
  162. data/spec/integration/schema/predicates/excluded_from/array_spec.rb +0 -459
  163. data/spec/integration/schema/predicates/excluded_from/range_spec.rb +0 -459
  164. data/spec/integration/schema/predicates/excludes_spec.rb +0 -391
  165. data/spec/integration/schema/predicates/filled_spec.rb +0 -467
  166. data/spec/integration/schema/predicates/format_spec.rb +0 -455
  167. data/spec/integration/schema/predicates/gt_spec.rb +0 -519
  168. data/spec/integration/schema/predicates/gteq_spec.rb +0 -519
  169. data/spec/integration/schema/predicates/hash_spec.rb +0 -69
  170. data/spec/integration/schema/predicates/included_in/array_spec.rb +0 -459
  171. data/spec/integration/schema/predicates/included_in/range_spec.rb +0 -459
  172. data/spec/integration/schema/predicates/includes_spec.rb +0 -391
  173. data/spec/integration/schema/predicates/key_spec.rb +0 -88
  174. data/spec/integration/schema/predicates/lt_spec.rb +0 -520
  175. data/spec/integration/schema/predicates/lteq_spec.rb +0 -519
  176. data/spec/integration/schema/predicates/max_size_spec.rb +0 -391
  177. data/spec/integration/schema/predicates/min_size_spec.rb +0 -391
  178. data/spec/integration/schema/predicates/none_spec.rb +0 -265
  179. data/spec/integration/schema/predicates/not_eql_spec.rb +0 -391
  180. data/spec/integration/schema/predicates/odd_spec.rb +0 -455
  181. data/spec/integration/schema/predicates/size/fixed_spec.rb +0 -398
  182. data/spec/integration/schema/predicates/size/range_spec.rb +0 -395
  183. data/spec/integration/schema/predicates/type_spec.rb +0 -413
  184. data/spec/integration/schema/reusing_schema_spec.rb +0 -33
  185. data/spec/integration/schema/using_types_spec.rb +0 -135
  186. data/spec/integration/schema/validate_spec.rb +0 -120
  187. data/spec/integration/schema/xor_spec.rb +0 -35
  188. data/spec/integration/schema_builders_spec.rb +0 -17
  189. data/spec/integration/schema_spec.rb +0 -173
  190. data/spec/shared/message_compiler.rb +0 -11
  191. data/spec/shared/predicate_helper.rb +0 -15
  192. data/spec/shared/rule_compiler.rb +0 -8
  193. data/spec/spec_helper.rb +0 -62
  194. data/spec/support/define_struct.rb +0 -25
  195. data/spec/support/matchers.rb +0 -38
  196. data/spec/support/mutant.rb +0 -9
  197. data/spec/support/predicates_integration.rb +0 -7
  198. data/spec/unit/input_processor_compiler/json_spec.rb +0 -283
  199. data/spec/unit/input_processor_compiler/params_spec.rb +0 -328
  200. data/spec/unit/message_compiler/visit_failure_spec.rb +0 -38
  201. data/spec/unit/message_compiler/visit_spec.rb +0 -16
  202. data/spec/unit/message_compiler_spec.rb +0 -7
  203. data/spec/unit/predicate_registry_spec.rb +0 -34
  204. data/spec/unit/schema/key_spec.rb +0 -38
  205. data/spec/unit/schema/rule_spec.rb +0 -42
  206. data/spec/unit/schema/value_spec.rb +0 -131
  207. data/spec/unit/schema_spec.rb +0 -35
@@ -1,72 +0,0 @@
1
- require 'dry/validation/messages/i18n'
2
-
3
- RSpec.describe Dry::Validation, 'with localized messages' do
4
- before do
5
- I18n.config.available_locales_set << :pl
6
- I18n.load_path.concat(%w(en pl).map { |l| SPEC_ROOT.join("fixtures/locales/#{l}.yml") })
7
- I18n.backend.load_translations
8
- end
9
-
10
- describe 'defining schema' do
11
- context 'without a namespace' do
12
- subject(:schema) do
13
- Dry::Validation.Schema do
14
- configure do
15
- config.messages = :i18n
16
- end
17
-
18
- required(:email) { |email| email.filled? }
19
- end
20
- end
21
-
22
- describe '#messages' do
23
- it 'returns localized error messages' do
24
- expect(schema.(email: '').messages(locale: :pl)).to eql(
25
- email: ['Proszę podać adres email']
26
- )
27
- end
28
- end
29
- end
30
-
31
- context 'with a namespace' do
32
- subject(:schema) do
33
- Dry::Validation.Schema do
34
- configure do
35
- configure do |config|
36
- config.messages = :i18n
37
- config.namespace = :user
38
- end
39
- end
40
-
41
- required(:email) { |email| email.filled? }
42
- end
43
- end
44
-
45
- describe '#messages' do
46
- it 'returns localized error messages' do
47
- expect(schema.(email: '').messages(locale: :pl)).to eql(
48
- email: ['Hej user! Dawaj ten email no!']
49
- )
50
- end
51
- end
52
-
53
- describe '#errors' do
54
- context 'with different locale' do
55
- before do
56
- I18n.locale = :pl
57
- end
58
-
59
- after do
60
- I18n.locale = :en
61
- end
62
-
63
- it 'contains the localized errors' do
64
- expect(schema.(email: '').errors).to eql(
65
- { email: ['Hej user! Dawaj ten email no!'] }
66
- )
67
- end
68
- end
69
- end
70
- end
71
- end
72
- end
@@ -1,405 +0,0 @@
1
- require 'dry/validation/message_compiler'
2
-
3
- RSpec.describe Dry::Validation::MessageCompiler do
4
- subject(:message_compiler) { MessageCompiler.new(messages) }
5
-
6
- include_context 'predicate helper'
7
-
8
- let(:messages) do
9
- Messages.default.merge(
10
- en: {
11
- errors: {
12
- key?: {
13
- arg: {
14
- default: '+%{name}+ key is missing in the hash',
15
- },
16
- value: {
17
- gender: 'Please provide your gender'
18
- }
19
- },
20
- rules: {
21
- address: {
22
- filled?: 'Please provide your address'
23
- }
24
- }
25
- }
26
- },
27
- pl: {
28
- rules: {
29
- email: 'adres email'
30
- },
31
- errors: {
32
- email?: 'nie jest poprawny'
33
- }
34
- }
35
- )
36
- end
37
-
38
- describe '#call with flat inputs' do
39
- let(:ast) do
40
- [
41
- [:failure, [:name, p(:key?, :name)]],
42
- [:failure, [:gender, p(:key?, :gender)]],
43
- [:key, [:age, [:failure, [:age, p(:gt?, 18)]]]],
44
- [:key, [:email, [:failure, [:email, p(:filled?, '')]]]],
45
- [:key, [:address, [:failure, [:address, p(:filled?, '')]]]]
46
- ]
47
- end
48
-
49
- it 'converts error ast into another format' do
50
- expect(message_compiler.(ast).to_h).to eql(
51
- name: ["+name+ key is missing in the hash"],
52
- gender: ["Please provide your gender"],
53
- age: ["must be greater than 18"],
54
- email: ["must be filled"],
55
- address: ["Please provide your address"]
56
- )
57
- end
58
- end
59
-
60
- describe '#visit with an :input node' do
61
- context 'full message' do
62
- it 'returns full message including rule name' do
63
- msg = message_compiler.with(full: true).visit(
64
- [:failure, [:num, [:key, [:num, p(:int?, '2')]]]]
65
- )
66
-
67
- expect(msg).to eql('num must be an integer')
68
- end
69
- end
70
-
71
- context 'rule name translations' do
72
- it 'translates rule name and its message' do
73
- msg = message_compiler.with(locale: :pl, full: true).visit(
74
- [:failure, [:email, [:key, [:email, p(:email?, 'oops')]]]]
75
- )
76
-
77
- expect(msg).to eql('adres email nie jest poprawny')
78
- end
79
- end
80
-
81
- describe ':empty?' do
82
- it 'returns valid message' do
83
- msg = message_compiler.visit(
84
- [:failure, [:tags, [:key, [:tags, p(:empty?, nil)]]]]
85
- )
86
-
87
- expect(msg).to eql('must be empty')
88
- end
89
- end
90
-
91
- describe ':excluded_from?' do
92
- it 'returns valid message' do
93
- msg = message_compiler.visit(
94
- [:failure, [:num, [:key, [:num, p(:excluded_from?, [1, 2, 3], 2)]]]]
95
- )
96
-
97
- expect(msg).to eql('must not be one of: 1, 2, 3')
98
- end
99
- end
100
-
101
- describe ':excludes?' do
102
- it 'returns valid message' do
103
- msg = message_compiler.visit(
104
- [:failure, [:array, [:key, [:array, p(:excludes?, 2, [1, 2])]]]]
105
- )
106
-
107
- expect(msg).to eql('must not include 2')
108
- end
109
- end
110
-
111
- describe ':included_in?' do
112
- it 'returns valid message' do
113
- msg = message_compiler.visit(
114
- [:failure, [:num, [:key, [:num, p(:included_in?, [1, 2, 3], :num)]]]]
115
- )
116
-
117
- expect(msg).to eql('must be one of: 1, 2, 3')
118
- end
119
- end
120
-
121
- describe ':includes?' do
122
- it 'returns valid message' do
123
- msg = message_compiler.visit(
124
- [:failure, [:num, [:key, [:num, p(:includes?, 2, [1])]]]]
125
- )
126
-
127
- expect(msg).to eql('must include 2')
128
- end
129
- end
130
-
131
- describe ':gt?' do
132
- it 'returns valid message' do
133
- msg = message_compiler.visit(
134
- [:failure, [:num, [:key, [:num, p(:gt?, 3, 2)]]]]
135
- )
136
-
137
- expect(msg).to eql('must be greater than 3')
138
- end
139
- end
140
-
141
- describe ':gteq?' do
142
- it 'returns valid message' do
143
- msg = message_compiler.visit(
144
- [:failure, [:num, [:key, [:num, p(:gteq?, 3, 2)]]]]
145
- )
146
-
147
- expect(msg).to eql('must be greater than or equal to 3')
148
- end
149
- end
150
-
151
- describe ':lt?' do
152
- it 'returns valid message' do
153
- msg = message_compiler.visit(
154
- [:failure, [:num, [:key, [:num, p(:lt?, 3, 2)]]]]
155
- )
156
-
157
- expect(msg).to eql('must be less than 3')
158
- end
159
- end
160
-
161
- describe ':lteq?' do
162
- it 'returns valid message' do
163
- msg = message_compiler.visit(
164
- [:failure, [:num, [:key, [:num, p(:lteq?, 3, 2)]]]]
165
- )
166
-
167
- expect(msg).to eql('must be less than or equal to 3')
168
- end
169
- end
170
-
171
- describe ':hash?' do
172
- it 'returns valid message' do
173
- msg = message_compiler.visit(
174
- [:failure, [:address, [:key, [:address, p(:hash?, '')]]]]
175
- )
176
-
177
- expect(msg).to eql('must be a hash')
178
- end
179
- end
180
-
181
- describe ':array?' do
182
- it 'returns valid message' do
183
- msg = message_compiler.visit(
184
- [:failure, [:phone_numbers, [:key, [:phone, p(:array?,'')]]]]
185
- )
186
-
187
- expect(msg).to eql('must be an array')
188
- end
189
- end
190
-
191
- describe ':int?' do
192
- it 'returns valid message' do
193
- msg = message_compiler.visit(
194
- [:failure, [:num, [:key, [:num, p(:int?, '2')]]]]
195
- )
196
-
197
- expect(msg).to eql('must be an integer')
198
- end
199
- end
200
-
201
- describe ':float?' do
202
- it 'returns valid message' do
203
- msg = message_compiler.visit(
204
- [:failure, [:num, [:key, [:num, p(:float?, '2')]]]]
205
- )
206
-
207
- expect(msg).to eql('must be a float')
208
- end
209
- end
210
-
211
- describe ':decimal?' do
212
- it 'returns valid message' do
213
- msg = message_compiler.visit(
214
- [:failure, [:num, [:key, [:num, p(:decimal?, '2')]]]]
215
- )
216
-
217
- expect(msg).to eql('must be a decimal')
218
- end
219
- end
220
-
221
- describe ':date?' do
222
- it 'returns valid message' do
223
- msg = message_compiler.visit(
224
- [:failure, [:num, [:key, [:num, p(:date?, '2')]]]]
225
- )
226
-
227
- expect(msg).to eql('must be a date')
228
- end
229
- end
230
-
231
- describe ':date_time?' do
232
- it 'returns valid message' do
233
- msg = message_compiler.visit(
234
- [:failure, [:num, [:key, [:num, p(:date_time?, '2')]]]]
235
- )
236
-
237
- expect(msg).to eql('must be a date time')
238
- end
239
- end
240
-
241
- describe ':time?' do
242
- it 'returns valid message' do
243
- msg = message_compiler.visit(
244
- [:failure, [:num, [:key, [:num, p(:time?, '2')]]]]
245
- )
246
-
247
- expect(msg).to eql('must be a time')
248
- end
249
- end
250
-
251
- describe ':max_size?' do
252
- it 'returns valid message' do
253
- msg = message_compiler.visit(
254
- [:failure, [:num, [:key, [:num, p(:max_size?, 3, 'abcd')]]]]
255
- )
256
-
257
- expect(msg).to eql('size cannot be greater than 3')
258
- end
259
- end
260
-
261
- describe ':min_size?' do
262
- it 'returns valid message' do
263
- msg = message_compiler.visit(
264
- [:failure, [:num, [:key, [:num, p(:min_size?, 3, 'ab')]]]]
265
- )
266
-
267
- expect(msg).to eql('size cannot be less than 3')
268
- end
269
- end
270
-
271
- describe ':none?' do
272
- it 'returns valid message' do
273
- msg = message_compiler.visit(
274
- [:failure, [:num, [:key, [:num, p(:none?, nil)]]]]
275
- )
276
-
277
- expect(msg).to eql('cannot be defined')
278
- end
279
- end
280
-
281
- describe ':size?' do
282
- it 'returns valid message when val is array and arg is int' do
283
- msg = message_compiler.visit(
284
- [:failure, [:numbers, [:key, [:numbers, p(:size?, 3, [1])]]]]
285
- )
286
-
287
- expect(msg).to eql('size must be 3')
288
- end
289
-
290
- it 'returns valid message when val is array and arg is range' do
291
- msg = message_compiler.visit(
292
- [:failure, [:numbers, [:key, [:numbers, p(:size?, 3..4, [1])]]]]
293
- )
294
-
295
- expect(msg).to eql('size must be within 3 - 4')
296
- end
297
-
298
- it 'returns valid message when arg is int' do
299
- msg = message_compiler.visit(
300
- [:failure, [:num, [:key, [:num, p(:size?, 3, 'ab')]]]]
301
- )
302
-
303
- expect(msg).to eql('length must be 3')
304
- end
305
-
306
- it 'returns valid message when arg is range' do
307
- msg = message_compiler.visit(
308
- [:failure, [:num, [:key, [:num, p(:size?, 3..4, 'ab')]]]]
309
- )
310
-
311
- expect(msg).to eql('length must be within 3 - 4')
312
- end
313
- end
314
-
315
- describe ':str?' do
316
- it 'returns valid message' do
317
- msg = message_compiler.visit(
318
- [:failure, [:num, [:key, [:num, p(:str?, 3)]]]]
319
- )
320
-
321
- expect(msg).to eql('must be a string')
322
- end
323
- end
324
-
325
- describe ':bool?' do
326
- it 'returns valid message' do
327
- msg = message_compiler.visit(
328
- [:failure, [:num, [:key, [:num, p(:bool?, 3)]]]]
329
- )
330
-
331
- expect(msg).to eql('must be boolean')
332
- end
333
- end
334
-
335
- describe ':format?' do
336
- it 'returns valid message' do
337
- msg = message_compiler.visit(
338
- [:failure, [:str, [:key, [:str, p(:format?, /^F/, 'Bar')]]]]
339
- )
340
-
341
- expect(msg).to eql('is in invalid format')
342
- end
343
- end
344
-
345
- describe ':number?' do
346
- it 'returns valid message' do
347
- msg = message_compiler.visit(
348
- [:failure, [:str, [:key, [:str, p(:number?, 'not a number')]]]]
349
- )
350
-
351
- expect(msg).to eql('must be a number')
352
- end
353
- end
354
-
355
- describe ':odd?' do
356
- it 'returns valid message' do
357
- msg = message_compiler.visit(
358
- [:failure, [:str, [:key, [:str, p(:odd?, 1)]]]]
359
- )
360
-
361
- expect(msg).to eql('must be odd')
362
- end
363
- end
364
-
365
- describe ':even?' do
366
- it 'returns valid message' do
367
- msg = message_compiler.visit(
368
- [:failure, [:str, [:key, [:str, p(:even?, 2)]]]]
369
- )
370
-
371
- expect(msg).to eql('must be even')
372
- end
373
- end
374
-
375
- describe ':eql?' do
376
- it 'returns valid message' do
377
- msg = message_compiler.visit(
378
- [:failure, [:str, [:key, [:str, p(:eql?, 'Bar', 'Foo')]]]]
379
- )
380
-
381
- expect(msg).to eql('must be equal to Bar')
382
- end
383
- end
384
-
385
- describe ':not_eql?' do
386
- it 'returns valid message' do
387
- msg = message_compiler.visit(
388
- [:failure, [:str, [:key, [:str, p(:not_eql?, 'Foo', 'Foo')]]]]
389
- )
390
-
391
- expect(msg).to eql('must not be equal to Foo')
392
- end
393
- end
394
-
395
- describe ':type?' do
396
- it 'returns valid message' do
397
- msg = message_compiler.visit(
398
- [:failure, [:age, [:key, [:age, p(:type?, Integer, '1')]]]]
399
- )
400
-
401
- expect(msg).to eql('must be Integer')
402
- end
403
- end
404
- end
405
- end
@@ -1,104 +0,0 @@
1
- require 'dry/validation/messages/i18n'
2
-
3
- RSpec.describe Messages::I18n do
4
- subject(:messages) { Messages::I18n.new }
5
-
6
- before do
7
- I18n.config.available_locales_set << :pl
8
- I18n.load_path.concat(%w(en pl).map { |l| SPEC_ROOT.join("fixtures/locales/#{l}.yml") })
9
- I18n.backend.load_translations
10
- I18n.locale = :pl
11
- end
12
-
13
- describe '#[]' do
14
- context 'with the default locale' do
15
- it 'returns nil when message is not defined' do
16
- expect(messages[:not_here, rule: :srsly]).to be(nil)
17
- end
18
-
19
- it 'returns a message for a predicate' do
20
- message = messages[:filled?, rule: :name].text
21
-
22
- expect(message).to eql("nie może być pusty")
23
- end
24
-
25
- it 'returns a message for a specific rule' do
26
- message = messages[:filled?, rule: :email].text
27
-
28
- expect(message).to eql("Proszę podać adres email")
29
- end
30
-
31
- it 'returns a message for a specific val type' do
32
- message = messages[:size?, rule: :pages, val_type: String].text
33
-
34
- expect(message).to eql("wielkość musi być równa \#{size}")
35
- end
36
-
37
- it 'returns a message for a specific rule and its default arg type' do
38
- message = messages[:size?, rule: :pages].text
39
-
40
- expect(message).to eql("wielkość musi być równa \#{size}")
41
- end
42
-
43
- it 'returns a message for a specific rule and its arg type' do
44
- message = messages[:size?, rule: :pages, arg_type: Range].text
45
-
46
- expect(message).to eql("wielkość musi być między \#{size_left} a \#{size_right}")
47
- end
48
- end
49
-
50
- context 'rule name translations' do
51
- it 'translates rule name' do
52
- msg = messages.rule('email')
53
-
54
- expect(msg).to eql('Adres email')
55
- end
56
- end
57
-
58
- context 'with a different locale' do
59
- it 'returns a message for a predicate' do
60
- message = messages[:filled?, rule: :name, locale: :en].text
61
-
62
- expect(message).to eql("must be filled")
63
- end
64
-
65
- it 'returns a message for a specific rule' do
66
- message = messages[:filled?, rule: :email, locale: :en].text
67
-
68
- expect(message).to eql("Please provide your email")
69
- end
70
-
71
- it 'returns a message for a specific rule and its default arg type' do
72
- message = messages[:size?, rule: :pages, locale: :en].text
73
-
74
- expect(message).to eql("size must be \#{size}")
75
- end
76
-
77
- it 'returns a message for a specific rule and its arg type' do
78
- message = messages[:size?, rule: :pages, arg_type: Range, locale: :en].text
79
-
80
- expect(message).to eql("size must be within \#{size_left} - \#{size_right}")
81
- end
82
- end
83
-
84
- context 'fallbacking to I18n.default_locale with fallback backend config' do
85
- before do
86
- require "i18n/backend/fallbacks"
87
- I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
88
- end
89
-
90
- it 'returns a message for a predicate in the default_locale' do
91
- pending 'FIXME: this got broken for some reason, probably an I18n issue'
92
-
93
- message = messages[:even?, rule: :some_number].text
94
-
95
- expect(I18n.locale).to eql(:pl)
96
- expect(message).to eql("must be even")
97
- end
98
- end
99
- end
100
-
101
- after(:all) do
102
- I18n.locale = I18n.default_locale
103
- end
104
- end
@@ -1,28 +0,0 @@
1
- RSpec.describe Dry::Validation::Schema do
2
- describe 'defining schema with optional keys' do
3
- subject(:schema) do
4
- Dry::Validation.Schema do
5
- optional(:email) { |email| email.filled? }
6
-
7
- required(:address).schema do
8
- required(:city, &:filled?)
9
- required(:street, &:filled?)
10
-
11
- optional(:phone_number) do
12
- none? | str?
13
- end
14
- end
15
- end
16
- end
17
-
18
- describe '#call' do
19
- it 'skips rules when key is not present' do
20
- expect(schema.(address: { city: 'NYC', street: 'Street 1/2' })).to be_success
21
- end
22
-
23
- it 'applies rules when key is present' do
24
- expect(schema.(email: '')).to_not be_success
25
- end
26
- end
27
- end
28
- end