reform 2.2.2 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b35ec0ff9ab6debc47f103a87e25e7d85cf6da49
4
- data.tar.gz: 336c602448cd94cc9dbbcb4dcac9ae4838257e6e
3
+ metadata.gz: b1b845d6f45f5131183408c9cea079d554d6983c
4
+ data.tar.gz: 2cc7e0c7444007024bcd4e7eab8b6d2f18c7868e
5
5
  SHA512:
6
- metadata.gz: c0c481a59d666118b8c754117ce8af5d4fa60876304699d703f89dd44490bfeb411c44185ef9b9f0b3ec0c417da789c5895a3afaa87d81ab587fd3e7ece4878a
7
- data.tar.gz: 8a19c0cc218711b8810c346a83a6611efb9d132432403df639a9f7dcfb886bb60eeb1fc46dd9f1cf469733295b24eee20a0be093b5776e4ac23d491ca0c2eb3d
6
+ metadata.gz: b9e001d2215c2923d99d75c69db17615f65c7b896b1d815e90bfa0fc86b4fb13ef037a2aa6342f1fc13794b0a0171a628be2655a91307cd197c656a3180ea4a6
7
+ data.tar.gz: f919c2d2f8e8f9f21258bcc467613d1734f7c365ef81cf9b13b076321de33e5286a5307ed4c1ec4f4d09189fca0690d82f49777103bb03e287de9201742be26a
data/CHANGES.md CHANGED
@@ -1,6 +1,10 @@
1
+ ## 2.2.3
2
+
3
+ * Add `Form#call` as an alias for `validate` and the `Result` object.
4
+
1
5
  ## 2.2.2
2
6
 
3
- ** Loosen `uber` dependency.
7
+ * Loosen `uber` dependency.
4
8
 
5
9
  ## 2.2.1
6
10
 
@@ -87,5 +87,8 @@ module Reform
87
87
  def skip!
88
88
  Representable::Pipeline::Stop
89
89
  end
90
+
91
+ require "reform/form/call"
92
+ include Call
90
93
  end
91
94
  end
@@ -0,0 +1,23 @@
1
+ module Reform::Form::Call
2
+ def call(params, &block)
3
+ bool = validate(params, &block)
4
+
5
+ Result.new(bool, self)
6
+ end
7
+
8
+ # TODO: the result object might soon come from dry.
9
+ class Result < SimpleDelegator
10
+ def initialize(success, data)
11
+ @success = success
12
+ super(data)
13
+ end
14
+
15
+ def success?
16
+ @success
17
+ end
18
+
19
+ def failure?
20
+ ! @success
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Reform
2
- VERSION = "2.2.2"
2
+ VERSION = "2.2.3"
3
3
  end
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+
3
+ class CallTest < Minitest::Spec
4
+ Song = Struct.new(:title)
5
+
6
+ class SongForm < Reform::Form
7
+ property :title
8
+
9
+ validation do
10
+ key(:title).required
11
+ end
12
+ end
13
+
14
+ let (:form) { SongForm.new(Song.new) }
15
+
16
+ it { form.(title: "True North").success?.must_equal true }
17
+ it { form.(title: "True North").failure?.must_equal false }
18
+ it { form.(title: "").success?.must_equal false }
19
+ it { form.(title: "").failure?.must_equal true }
20
+
21
+ it { form.(title: "True North").errors.messages.must_equal({}) }
22
+ it { form.(title: "").errors.messages.must_equal({:title=>["must be filled"]}) }
23
+ end
@@ -1,165 +1,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
- key(:title).required
11
- end
12
- end
13
-
14
- collection :songs do
15
- property :title
16
- validation do
17
- key(:title).required
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
- key(:name).required
27
- end
28
- end
29
- # TODO: make band a required object.
30
-
31
- validation do
32
- key(:name).required(: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
- key(:title).required
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
+ # 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
+ # key(:title).required
11
+ # end
12
+ # end
13
+
14
+ # collection :songs do
15
+ # property :title
16
+ # validation do
17
+ # key(:title).required
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
+ # key(:name).required
27
+ # end
28
+ # end
29
+ # # TODO: make band a required object.
30
+
31
+ # validation do
32
+ # key(:name).required(: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
+ # key(:title).required
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
- 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
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.2
4
+ version: 2.2.3
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: 2016-11-09 00:00:00.000000000 Z
12
+ date: 2016-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: disposable
@@ -177,6 +177,7 @@ files:
177
177
  - lib/reform/contract/errors.rb
178
178
  - lib/reform/contract/validate.rb
179
179
  - lib/reform/form.rb
180
+ - lib/reform/form/call.rb
180
181
  - lib/reform/form/coercion.rb
181
182
  - lib/reform/form/composition.rb
182
183
  - lib/reform/form/dry.rb
@@ -192,6 +193,7 @@ files:
192
193
  - lib/reform/version.rb
193
194
  - reform.gemspec
194
195
  - test/benchmarking.rb
196
+ - test/call_test.rb
195
197
  - test/changed_test.rb
196
198
  - test/coercion_test.rb
197
199
  - test/composition_test.rb
@@ -252,6 +254,7 @@ specification_version: 4
252
254
  summary: Form object decoupled from models with validation, population and presentation.
253
255
  test_files:
254
256
  - test/benchmarking.rb
257
+ - test/call_test.rb
255
258
  - test/changed_test.rb
256
259
  - test/coercion_test.rb
257
260
  - test/composition_test.rb