reform 2.2.3 → 2.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +7 -0
- data/Gemfile +1 -1
- data/lib/reform/form/populator.rb +1 -3
- data/lib/reform/form/prepopulate.rb +1 -1
- data/lib/reform/version.rb +1 -1
- data/reform.gemspec +2 -3
- data/test/errors_test.rb +165 -165
- data/test/validation/dry_validation_test.rb +352 -352
- metadata +5 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 851795d3f1959935496c4a582f24dffc6020b848
|
4
|
+
data.tar.gz: f62755eb2d9e6302a00790b703698149838474af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 865c755aea555fedc28f61ae04a7903544ad637c621e656c68a64fc9f952b0a105906f4abc77571dce490099227224cd966c12ccc802b8ae7e830c0b27d108f3
|
7
|
+
data.tar.gz: a90c092125bd9f2a7db2a6092267ba3ac439c6d92bd3b2e657a50156128c4de5b8f28e3f62cc2eefc47b6a8f36b1e8d5f94e393f31ab115cf297a60210fc58c4
|
data/CHANGES.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## 2.2.4
|
2
|
+
|
3
|
+
* Always require `disposable` >= 0.4.1.
|
4
|
+
|
5
|
+
The only difference here is that `Form#sync`/`#save` with a block will include `nil` properties into the nested hash.
|
6
|
+
* Remove `uber` dependency.
|
7
|
+
|
1
8
|
## 2.2.3
|
2
9
|
|
3
10
|
* Add `Form#call` as an alias for `validate` and the `Result` object.
|
data/Gemfile
CHANGED
@@ -5,11 +5,9 @@
|
|
5
5
|
#
|
6
6
|
# For collections, the entire collection and the currently deserialised index is passed in.
|
7
7
|
class Reform::Form::Populator
|
8
|
-
include Uber::Callable
|
9
|
-
|
10
8
|
def initialize(user_proc)
|
11
9
|
@user_proc = user_proc # the actual `populator: ->{}` block from the user, via ::property.
|
12
|
-
@value =
|
10
|
+
@value = Declarative::Option(user_proc, instance_exec: true) # we can now process Callable, procs, :symbol.
|
13
11
|
end
|
14
12
|
|
15
13
|
def call(input, options)
|
data/lib/reform/version.rb
CHANGED
data/reform.gemspec
CHANGED
@@ -17,9 +17,8 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_dependency "disposable",
|
21
|
-
spec.add_dependency "
|
22
|
-
spec.add_dependency "representable", ">= 2.4.0", "< 3.1.0"
|
20
|
+
spec.add_dependency "disposable", ">= 0.4.1"
|
21
|
+
spec.add_dependency "representable", ">= 2.4.0", "< 3.1.0"
|
23
22
|
|
24
23
|
spec.add_development_dependency "bundler"
|
25
24
|
spec.add_development_dependency "rake"
|
data/test/errors_test.rb
CHANGED
@@ -1,165 +1,165 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
#
|
30
|
-
|
31
|
-
|
32
|
-
#
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
#
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
#
|
44
|
-
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
#
|
87
|
-
#
|
88
|
-
|
89
|
-
|
90
|
-
#
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
#
|
163
|
-
|
164
|
-
|
165
|
-
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ErrorsTest < MiniTest::Spec
|
4
|
+
class AlbumForm < Reform::Form
|
5
|
+
property :title
|
6
|
+
|
7
|
+
property :hit do
|
8
|
+
property :title
|
9
|
+
validation do
|
10
|
+
required(:title).filled
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
collection :songs do
|
15
|
+
property :title
|
16
|
+
validation do
|
17
|
+
required(:title).filled
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
property :band do # yepp, people do crazy stuff like that.
|
22
|
+
property :name
|
23
|
+
property :label do
|
24
|
+
property :name
|
25
|
+
validation do
|
26
|
+
required(:name).filled
|
27
|
+
end
|
28
|
+
end
|
29
|
+
# TODO: make band a required object.
|
30
|
+
|
31
|
+
validation do
|
32
|
+
# required(:name).filled(:music_taste_ok?)
|
33
|
+
|
34
|
+
configure do
|
35
|
+
config.messages_file = "test/validation/errors.yml"
|
36
|
+
|
37
|
+
def music_taste_ok?(value)
|
38
|
+
value != "Nickelback"
|
39
|
+
# errors.add(:base, "You are a bad person") if name == "Nickelback"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
# validate :music_taste_ok?
|
44
|
+
|
45
|
+
# private
|
46
|
+
# def music_taste_ok?
|
47
|
+
# errors.add(:base, "You are a bad person") if name == "Nickelback"
|
48
|
+
# end
|
49
|
+
end
|
50
|
+
|
51
|
+
validation do
|
52
|
+
required(:title).filled
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
let (:album) do
|
57
|
+
OpenStruct.new(
|
58
|
+
:title => "Blackhawks Over Los Angeles",
|
59
|
+
:hit => song,
|
60
|
+
:songs => songs, # TODO: document this requirement,
|
61
|
+
|
62
|
+
:band => Struct.new(:name, :label).new("Epitaph", OpenStruct.new),
|
63
|
+
)
|
64
|
+
end
|
65
|
+
let (:song) { OpenStruct.new(:title => "Downtown") }
|
66
|
+
let (:songs) { [song=OpenStruct.new(:title => "Calling"), song] }
|
67
|
+
let (:form) { AlbumForm.new(album) }
|
68
|
+
|
69
|
+
|
70
|
+
describe "incorrect #validate" do
|
71
|
+
before { form.validate(
|
72
|
+
"hit" =>{"title" => ""},
|
73
|
+
"title" => "",
|
74
|
+
"songs" => [{"title" => ""}, {"title" => ""}]) } # FIXME: what happens if item is missing?
|
75
|
+
|
76
|
+
it do
|
77
|
+
form.errors.messages.must_equal({
|
78
|
+
:title => ["must be filled"],
|
79
|
+
:"hit.title"=>["must be filled"],
|
80
|
+
:"songs.title"=>["must be filled"],
|
81
|
+
:"band.label.name"=>["is missing"]
|
82
|
+
})
|
83
|
+
end
|
84
|
+
|
85
|
+
it do
|
86
|
+
#form.errors.must_equal({:title => ["must be filled"]})
|
87
|
+
# TODO: this should only contain local errors?
|
88
|
+
end
|
89
|
+
|
90
|
+
# nested forms keep their own Errors:
|
91
|
+
it { form.hit.errors.messages.must_equal({:title=>["must be filled"]}) }
|
92
|
+
it { form.songs[0].errors.messages.must_equal({:title=>["must be filled"]}) }
|
93
|
+
|
94
|
+
it do
|
95
|
+
form.errors.messages.must_equal({
|
96
|
+
:title => ["must be filled"],
|
97
|
+
:"hit.title" => ["must be filled"],
|
98
|
+
:"songs.title"=> ["must be filled"],
|
99
|
+
:"band.label.name"=>["is missing"]
|
100
|
+
})
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
describe "#validate with main form invalid" do
|
106
|
+
it do
|
107
|
+
form.validate("title"=>"", "band"=>{"label"=>{:name => "Fat Wreck"}}).must_equal false
|
108
|
+
form.errors.messages.must_equal({:title=>["must be filled"]})
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
describe "#validate with middle nested form invalid" do
|
114
|
+
before { @result = form.validate("hit"=>{"title" => ""}, "band"=>{"label"=>{:name => "Fat Wreck"}}) }
|
115
|
+
|
116
|
+
it { @result.must_equal false }
|
117
|
+
it { form.errors.messages.must_equal({:"hit.title"=>["must be filled"]}) }
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
describe "#validate with collection form invalid" do
|
122
|
+
before { @result = form.validate("songs"=>[{"title" => ""}], "band"=>{"label"=>{:name => "Fat Wreck"}}) }
|
123
|
+
|
124
|
+
it { @result.must_equal false }
|
125
|
+
it { form.errors.messages.must_equal({:"songs.title"=>["must be filled"]}) }
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
describe "#validate with collection and 2-level-nested invalid" do
|
130
|
+
before { @result = form.validate("songs"=>[{"title" => ""}], "band" => {"label" => {}}) }
|
131
|
+
|
132
|
+
it { @result.must_equal false }
|
133
|
+
it { form.errors.messages.must_equal({:"songs.title"=>["must be filled"], :"band.label.name"=>["is missing"]}) }
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "#validate with nested form using :base invalid" do
|
137
|
+
it do
|
138
|
+
result = form.validate("songs"=>[{"title" => "Someday"}], "band" => {"name" => "Nickelback", "label" => {"name" => "Roadrunner Records"}})
|
139
|
+
result.must_equal false
|
140
|
+
form.errors.messages.must_equal({:"band.name"=>["You are a bad person"]})
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "correct #validate" do
|
145
|
+
before { @result = form.validate(
|
146
|
+
"hit" => {"title" => "Sacrifice"},
|
147
|
+
"title" => "Second Heat",
|
148
|
+
"songs" => [{"title"=>"Heart Of A Lion"}],
|
149
|
+
"band" => {"label"=>{:name => "Fat Wreck"}}
|
150
|
+
) }
|
151
|
+
|
152
|
+
it { @result.must_equal true }
|
153
|
+
it { form.hit.title.must_equal "Sacrifice" }
|
154
|
+
it { form.title.must_equal "Second Heat" }
|
155
|
+
it { form.songs.first.title.must_equal "Heart Of A Lion" }
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
describe "Errors#to_s" do
|
160
|
+
before { form.validate("songs"=>[{"title" => ""}], "band" => {"label" => {}}) }
|
161
|
+
|
162
|
+
# to_s is aliased to messages
|
163
|
+
it { form.errors.to_s.must_equal "{:\"songs.title\"=>[\"must be filled\"], :\"band.label.name\"=>[\"is missing\"]}" }
|
164
|
+
end
|
165
|
+
end
|
@@ -1,352 +1,352 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
#
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
#
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
#
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
#
|
79
|
-
#
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
#
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
#
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
#
|
138
|
-
#
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
#
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
#
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
#
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
#
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
#
|
228
|
-
#
|
229
|
-
#
|
230
|
-
|
231
|
-
#
|
232
|
-
#
|
233
|
-
|
234
|
-
#
|
235
|
-
#
|
236
|
-
#
|
237
|
-
|
238
|
-
#
|
239
|
-
#
|
240
|
-
#
|
241
|
-
#
|
242
|
-
|
243
|
-
#
|
244
|
-
|
245
|
-
# #
|
246
|
-
#
|
247
|
-
#
|
248
|
-
#
|
249
|
-
|
250
|
-
# #
|
251
|
-
#
|
252
|
-
#
|
253
|
-
#
|
254
|
-
#
|
255
|
-
#
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
#
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
#
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
#
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
#
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
#
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
#
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
#
|
328
|
-
#
|
329
|
-
#
|
330
|
-
#
|
331
|
-
#
|
332
|
-
#
|
333
|
-
#
|
334
|
-
|
335
|
-
#
|
336
|
-
|
337
|
-
#
|
338
|
-
#
|
339
|
-
#
|
340
|
-
#
|
341
|
-
#
|
342
|
-
#
|
343
|
-
|
344
|
-
#
|
345
|
-
|
346
|
-
# #
|
347
|
-
#
|
348
|
-
#
|
349
|
-
#
|
350
|
-
#
|
351
|
-
#
|
352
|
-
|
1
|
+
require "test_helper"
|
2
|
+
require "reform/form/dry"
|
3
|
+
|
4
|
+
class DryValidationDefaultGroupTest < Minitest::Spec
|
5
|
+
Session = Struct.new(:username, :email, :password, :confirm_password)
|
6
|
+
|
7
|
+
class SessionForm < Reform::Form
|
8
|
+
include Reform::Form::Dry
|
9
|
+
|
10
|
+
property :username
|
11
|
+
property :email
|
12
|
+
property :password
|
13
|
+
property :confirm_password
|
14
|
+
|
15
|
+
validation do
|
16
|
+
key(:username).required
|
17
|
+
key(:email).required
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
let (:form) { SessionForm.new(Session.new) }
|
22
|
+
|
23
|
+
# valid.
|
24
|
+
it do
|
25
|
+
form.validate(username: "Helloween",
|
26
|
+
email: "yep").must_equal true
|
27
|
+
form.errors.messages.inspect.must_equal "{}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class ValidationGroupsTest < MiniTest::Spec
|
32
|
+
describe "basic validations" do
|
33
|
+
Session = Struct.new(:username, :email, :password, :confirm_password)
|
34
|
+
|
35
|
+
class SessionForm < Reform::Form
|
36
|
+
include Reform::Form::Dry::Validations
|
37
|
+
|
38
|
+
property :username
|
39
|
+
property :email
|
40
|
+
property :password
|
41
|
+
property :confirm_password
|
42
|
+
|
43
|
+
validation :default do
|
44
|
+
key(:username).required
|
45
|
+
key(:email).required
|
46
|
+
end
|
47
|
+
|
48
|
+
validation :email, if: :default do
|
49
|
+
key(:email).required(min_size?: 3)
|
50
|
+
end
|
51
|
+
|
52
|
+
validation :nested, if: :default do
|
53
|
+
key(:password).required(min_size?: 2)
|
54
|
+
end
|
55
|
+
|
56
|
+
validation :confirm, if: :default, after: :email do
|
57
|
+
key(:confirm_password).required(min_size?: 2)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
let (:form) { SessionForm.new(Session.new) }
|
62
|
+
|
63
|
+
# valid.
|
64
|
+
it do
|
65
|
+
form.validate({ username: "Helloween",
|
66
|
+
email: "yep",
|
67
|
+
password: "99",
|
68
|
+
confirm_password: "99" }).must_equal true
|
69
|
+
form.errors.messages.inspect.must_equal "{}"
|
70
|
+
end
|
71
|
+
|
72
|
+
# invalid.
|
73
|
+
it do
|
74
|
+
form.validate({}).must_equal false
|
75
|
+
form.errors.messages.inspect.must_equal "{:username=>[\"is missing\"], :email=>[\"is missing\"]}"
|
76
|
+
end
|
77
|
+
|
78
|
+
# partially invalid.
|
79
|
+
# 2nd group fails.
|
80
|
+
it do
|
81
|
+
form.validate(username: "Helloween", email: "yo", confirm_password:"9").must_equal false
|
82
|
+
form.errors.messages.inspect.must_equal "{:email=>[\"size cannot be less than 3\"], :confirm_password=>[\"size cannot be less than 2\"], :password=>[\"is missing\", \"size cannot be less than 2\"]}"
|
83
|
+
end
|
84
|
+
# 3rd group fails.
|
85
|
+
it do
|
86
|
+
form.validate(username: "Helloween", email: "yo!", confirm_password:"9").must_equal false
|
87
|
+
form.errors.messages.inspect
|
88
|
+
.must_equal "{:confirm_password=>[\"size cannot be less than 2\"], :password=>[\"is missing\", \"size cannot be less than 2\"]}"
|
89
|
+
end
|
90
|
+
# 4th group with after: fails.
|
91
|
+
it do
|
92
|
+
form.validate(username: "Helloween", email: "yo!", password: "", confirm_password: "9").must_equal false
|
93
|
+
form.errors.messages.inspect.must_equal "{:confirm_password=>[\"size cannot be less than 2\"], :password=>[\"must be filled\", \"size cannot be less than 2\"]}"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "Nested validations" do
|
98
|
+
class AlbumForm < Reform::Form
|
99
|
+
include Reform::Form::Dry::Validations
|
100
|
+
|
101
|
+
property :title
|
102
|
+
|
103
|
+
property :hit do
|
104
|
+
property :title
|
105
|
+
|
106
|
+
# FIX ME: this doesn't work now, @apotonick said he knows why
|
107
|
+
# The error is that this validation block act as an AM:V instead of the Dry one.
|
108
|
+
# validation :default do
|
109
|
+
# key(:title, &:filled?)
|
110
|
+
# end
|
111
|
+
end
|
112
|
+
|
113
|
+
collection :songs do
|
114
|
+
property :title
|
115
|
+
end
|
116
|
+
|
117
|
+
property :band do
|
118
|
+
property :name
|
119
|
+
property :label do
|
120
|
+
property :name
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
validation :default do
|
125
|
+
|
126
|
+
key(:title).required
|
127
|
+
|
128
|
+
key(:band).schema do
|
129
|
+
key(:name).required
|
130
|
+
key(:label).schema do
|
131
|
+
key(:name).required
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
configure do
|
136
|
+
option :form
|
137
|
+
# message need to be defined on fixtures/dry_error_messages
|
138
|
+
# d-v expects you to define your custome messages on the .yml file
|
139
|
+
def good_musical_taste?(value)
|
140
|
+
value != 'Nickelback'
|
141
|
+
end
|
142
|
+
|
143
|
+
def form_access_validation?(value)
|
144
|
+
form.title == 'Reform'
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
key(:title).required(:good_musical_taste?)
|
149
|
+
key(:title).required(:form_access_validation?)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
let (:album) do
|
154
|
+
OpenStruct.new(
|
155
|
+
:title => "Blackhawks Over Los Angeles",
|
156
|
+
:hit => song,
|
157
|
+
:songs => songs,
|
158
|
+
:band => Struct.new(:name, :label).new("Epitaph", OpenStruct.new),
|
159
|
+
)
|
160
|
+
end
|
161
|
+
let (:song) { OpenStruct.new(:title => "Downtown") }
|
162
|
+
let (:songs) { [ song = OpenStruct.new(:title => "Calling"), song ] }
|
163
|
+
let (:form) { AlbumForm.new(album) }
|
164
|
+
|
165
|
+
# correct #validate.
|
166
|
+
it do
|
167
|
+
result = form.validate(
|
168
|
+
"title" => "Reform",
|
169
|
+
"songs" => [
|
170
|
+
{"title" => "Fallout"},
|
171
|
+
{"title" => "Roxanne", "composer" => {"name" => "Sting"}}
|
172
|
+
],
|
173
|
+
"band" => {"label" => {"name" => "Epitaph"}},
|
174
|
+
)
|
175
|
+
|
176
|
+
result.must_equal true
|
177
|
+
form.errors.messages.inspect.must_equal "{}"
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
describe "fails with :validate, :validates and :validates_with" do
|
183
|
+
|
184
|
+
it "throws a goddamn error" do
|
185
|
+
e = proc do
|
186
|
+
class FailingForm < Reform::Form
|
187
|
+
include Reform::Form::Dry::Validations
|
188
|
+
|
189
|
+
property :username
|
190
|
+
|
191
|
+
validation :email do
|
192
|
+
validates(:email, &:filled?)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end.must_raise(NoMethodError)
|
196
|
+
# e.message.must_equal 'validates() is not supported by Dry Validation backend.'
|
197
|
+
|
198
|
+
e = proc do
|
199
|
+
class FailingForm < Reform::Form
|
200
|
+
include Reform::Form::Dry::Validations
|
201
|
+
|
202
|
+
property :username
|
203
|
+
|
204
|
+
validation :email do
|
205
|
+
validate(:email, &:filled?)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end.must_raise(NoMethodError)
|
209
|
+
# e.message.must_equal 'validate() is not supported by Dry Validation backend.'
|
210
|
+
|
211
|
+
e = proc do
|
212
|
+
class FailingForm < Reform::Form
|
213
|
+
include Reform::Form::Dry::Validations
|
214
|
+
|
215
|
+
property :username
|
216
|
+
|
217
|
+
validation :email do
|
218
|
+
validates_with(:email, &:filled?)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end.must_raise(NoMethodError)
|
222
|
+
# e.message.must_equal (NoMethodError)'validates_with() is not supported by Dry Validation backend.'
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
|
227
|
+
# describe "same-named group" do
|
228
|
+
# class OverwritingForm < Reform::Form
|
229
|
+
# include Reform::Form::Dry::Validations
|
230
|
+
|
231
|
+
# property :username
|
232
|
+
# property :email
|
233
|
+
|
234
|
+
# validation :email do # FIX ME: is this working for other validator or just bugging here?
|
235
|
+
# key(:email, &:filled?) # it's not considered, overitten
|
236
|
+
# end
|
237
|
+
|
238
|
+
# validation :email do # just another group.
|
239
|
+
# key(:username, &:filled?)
|
240
|
+
# end
|
241
|
+
# end
|
242
|
+
|
243
|
+
# let (:form) { OverwritingForm.new(Session.new) }
|
244
|
+
|
245
|
+
# # valid.
|
246
|
+
# it do
|
247
|
+
# form.validate({username: "Helloween"}).must_equal true
|
248
|
+
# end
|
249
|
+
|
250
|
+
# # invalid.
|
251
|
+
# it "whoo" do
|
252
|
+
# form.validate({}).must_equal false
|
253
|
+
# form.errors.messages.inspect.must_equal "{:username=>[\"username can't be blank\"]}"
|
254
|
+
# end
|
255
|
+
# end
|
256
|
+
|
257
|
+
|
258
|
+
describe "inherit: true in same group" do
|
259
|
+
class InheritSameGroupForm < Reform::Form
|
260
|
+
include Reform::Form::Dry::Validations
|
261
|
+
|
262
|
+
property :username
|
263
|
+
property :email
|
264
|
+
|
265
|
+
validation :email do
|
266
|
+
key(:email).required
|
267
|
+
end
|
268
|
+
|
269
|
+
validation :email, inherit: true do # extends the above.
|
270
|
+
key(:username).required
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
let (:form) { InheritSameGroupForm.new(Session.new) }
|
275
|
+
|
276
|
+
# valid.
|
277
|
+
it do
|
278
|
+
form.validate({username: "Helloween", email: 9}).must_equal true
|
279
|
+
end
|
280
|
+
|
281
|
+
# invalid.
|
282
|
+
it do
|
283
|
+
form.validate({}).must_equal false
|
284
|
+
form.errors.messages.inspect.must_equal "{:email=>[\"is missing\"], :username=>[\"is missing\"]}"
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
|
289
|
+
describe "if: with lambda" do
|
290
|
+
class IfWithLambdaForm < Reform::Form
|
291
|
+
include Reform::Form::Dry::Validations # ::build_errors.
|
292
|
+
|
293
|
+
property :username
|
294
|
+
property :email
|
295
|
+
property :password
|
296
|
+
|
297
|
+
validation :email do
|
298
|
+
key(:email).required
|
299
|
+
end
|
300
|
+
|
301
|
+
# run this is :email group is true.
|
302
|
+
validation :after_email, if: lambda { |results| results[:email]==true } do # extends the above.
|
303
|
+
key(:username).required
|
304
|
+
end
|
305
|
+
|
306
|
+
# block gets evaled in form instance context.
|
307
|
+
validation :password, if: lambda { |results| email == "john@trb.org" } do
|
308
|
+
key(:password).required
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
let (:form) { IfWithLambdaForm.new(Session.new) }
|
313
|
+
|
314
|
+
# valid.
|
315
|
+
it do
|
316
|
+
form.validate({username: "Strung Out", email: 9}).must_equal true
|
317
|
+
end
|
318
|
+
|
319
|
+
# invalid.
|
320
|
+
it do
|
321
|
+
form.validate({email: 9}).must_equal false
|
322
|
+
form.errors.messages.inspect.must_equal "{:username=>[\"is missing\"]}"
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
|
327
|
+
# Currenty dry-v don't support that option, it doesn't make sense
|
328
|
+
# I've talked to @solnic and he plans to add a "hint" feature to show
|
329
|
+
# more errors messages than only those that have failed.
|
330
|
+
#
|
331
|
+
# describe "multiple errors for property" do
|
332
|
+
# class MultipleErrorsForPropertyForm < Reform::Form
|
333
|
+
# include Reform::Form::Dry::Validations
|
334
|
+
|
335
|
+
# property :username
|
336
|
+
|
337
|
+
# validation :default do
|
338
|
+
# key(:username) do |username|
|
339
|
+
# username.filled? | (username.min_size?(2) & username.max_size?(3))
|
340
|
+
# end
|
341
|
+
# end
|
342
|
+
# end
|
343
|
+
|
344
|
+
# let (:form) { MultipleErrorsForPropertyForm.new(Session.new) }
|
345
|
+
|
346
|
+
# # valid.
|
347
|
+
# it do
|
348
|
+
# form.validate({username: ""}).must_equal false
|
349
|
+
# form.errors.messages.inspect.must_equal "{:username=>[\"username must be filled\", \"username is not proper size\"]}"
|
350
|
+
# end
|
351
|
+
# end
|
352
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-01-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: disposable
|
@@ -17,40 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 0.
|
21
|
-
- - "<"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.4.0
|
20
|
+
version: 0.4.1
|
24
21
|
type: :runtime
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
27
24
|
requirements:
|
28
25
|
- - ">="
|
29
26
|
- !ruby/object:Gem::Version
|
30
|
-
version: 0.
|
31
|
-
- - "<"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 0.4.0
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: uber
|
36
|
-
requirement: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: 0.0.15
|
41
|
-
- - "<"
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 0.2.0
|
44
|
-
type: :runtime
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: !ruby/object:Gem::Requirement
|
47
|
-
requirements:
|
48
|
-
- - ">="
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
version: 0.0.15
|
51
|
-
- - "<"
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: 0.2.0
|
27
|
+
version: 0.4.1
|
54
28
|
- !ruby/object:Gem::Dependency
|
55
29
|
name: representable
|
56
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -248,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
248
222
|
version: '0'
|
249
223
|
requirements: []
|
250
224
|
rubyforge_project:
|
251
|
-
rubygems_version: 2.
|
225
|
+
rubygems_version: 2.5.1
|
252
226
|
signing_key:
|
253
227
|
specification_version: 4
|
254
228
|
summary: Form object decoupled from models with validation, population and presentation.
|