rest_my_case 1.10.6 → 1.10.7
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.
- checksums.yaml +8 -8
- data/lib/rest_my_case/validator.rb +30 -10
- data/lib/rest_my_case/version.rb +1 -1
- data/spec/rest_my_case/validator_spec.rb +71 -12
- data/spec/support/validator/models/ruby_post_with_comments.rb +2 -11
- data/spec/support/validator/usecases/hierarchy_validation.rb +1 -1
- data/spec/support/validator/usecases/nested_validation.rb +7 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YmJmYmE1OGE0MGY2ZjA3MjMwOTE2ODQxYTIxYWZmM2E1M2IyZDkxOA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
Zjk0NGQwMGRiNjRhODFmMDQ3MmM5N2RiODRlMThmMDBiYjljOGI1NA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZjNkYTcxMGJhMzcxZDZlNjMwNDE5ZGQxMjQxMWJmMjNiODhiZGRlMjFiMzNm
|
10
|
+
MTdiODRkOTNjOTdhMTgyMmZmY2VlZTRjNTQ3ZGQyMzc2ZDY1ZjY0N2VjMDM0
|
11
|
+
NzNmMWMzMjQ0MzQ5NTAwYmZjMDg2MmVlOGE3ZmRjZGFiNWZjMWE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZDViMmUzY2MyNzBiMTBkMjg5Y2Q5OTBhZGE0ZmExNmJjY2ZiYWY0ZDdhYzU4
|
14
|
+
OTFiM2I3OTk3ZDQ4MDI2MTE4ODU1OTEyMWEzMjEzMzU0OTk3ZjNkYjVhNDFj
|
15
|
+
NjAwOTZjYjk3NDRiMGM2M2YwMTE5NjE1ODViYmU3MjJlNDA5MmI=
|
@@ -11,12 +11,13 @@ module RestMyCase
|
|
11
11
|
Judge::Base, DefenseAttorney::Base, Validator, Context::Base
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
@
|
14
|
+
def target_options
|
15
|
+
@target_options || Helpers.super_method(self, :target_options)
|
16
16
|
end
|
17
17
|
|
18
|
-
def target(target_name)
|
19
|
-
@
|
18
|
+
def target(target_name, target_options = {})
|
19
|
+
@target_options = target_options || {}
|
20
|
+
@target_options[:name] = target_name
|
20
21
|
end
|
21
22
|
|
22
23
|
def validators
|
@@ -62,26 +63,45 @@ module RestMyCase
|
|
62
63
|
|
63
64
|
extend AccusationAttorneys::HelperMethods
|
64
65
|
|
65
|
-
def
|
66
|
-
self.class.
|
66
|
+
def target_options
|
67
|
+
self.class.target_options || {}
|
68
|
+
end
|
69
|
+
|
70
|
+
def parent_target
|
71
|
+
@parent_target ||=
|
72
|
+
target_options[:in] ? get_target(target_options[:in]) : nil
|
67
73
|
end
|
68
74
|
|
69
75
|
def target
|
70
|
-
return nil if
|
76
|
+
return nil if target_options[:name].nil?
|
71
77
|
|
72
|
-
|
78
|
+
if parent_target
|
79
|
+
extend_errors_if_necessary(parent_target)
|
80
|
+
|
81
|
+
parent_target.send(target_options[:name])
|
82
|
+
else
|
83
|
+
get_target(target_options[:name])
|
84
|
+
end
|
73
85
|
end
|
74
86
|
|
75
87
|
def perform
|
76
88
|
targets = [*target]
|
77
89
|
|
78
|
-
return if Helpers.blank?(targets)
|
90
|
+
return if Helpers.blank?(targets) || all_validations_green?(targets)
|
79
91
|
|
80
|
-
|
92
|
+
if parent_target
|
93
|
+
parent_target.errors.add(target_options[:name], :invalid)
|
94
|
+
end
|
95
|
+
|
96
|
+
error('unprocessable_entity')
|
81
97
|
end
|
82
98
|
|
83
99
|
protected ######################## PROTECTED ###############################
|
84
100
|
|
101
|
+
def get_target(method)
|
102
|
+
respond_to?(method) ? send(method) : context.send(method)
|
103
|
+
end
|
104
|
+
|
85
105
|
def all_validations_green?(targets)
|
86
106
|
targets.map do |object_to_validate|
|
87
107
|
extend_errors_if_necessary(object_to_validate)
|
data/lib/rest_my_case/version.rb
CHANGED
@@ -102,11 +102,11 @@ describe RestMyCase::Validator do
|
|
102
102
|
expect(@context.ok?).to be true
|
103
103
|
end
|
104
104
|
|
105
|
-
it "@post1 should not
|
105
|
+
it "@post1 should not contain errors" do
|
106
106
|
expect(@post1.errors.count).to be 0
|
107
107
|
end
|
108
108
|
|
109
|
-
it "@post2 should not
|
109
|
+
it "@post2 should not contain errors" do
|
110
110
|
expect(@post2.errors.count).to be 0
|
111
111
|
end
|
112
112
|
end
|
@@ -122,11 +122,11 @@ describe RestMyCase::Validator do
|
|
122
122
|
expect(@context.ok?).to be false
|
123
123
|
end
|
124
124
|
|
125
|
-
it "@post1 should not
|
125
|
+
it "@post1 should not contain errors" do
|
126
126
|
expect(@post1.errors.count).to be 0
|
127
127
|
end
|
128
128
|
|
129
|
-
it "@post2 should not
|
129
|
+
it "@post2 should not contain errors" do
|
130
130
|
expect(@post2.errors.count).to be 1
|
131
131
|
expect(@post2.errors.added?(:body, :blank)).to be true
|
132
132
|
end
|
@@ -143,12 +143,12 @@ describe RestMyCase::Validator do
|
|
143
143
|
expect(@context.ok?).to be false
|
144
144
|
end
|
145
145
|
|
146
|
-
it "@post1 should not
|
146
|
+
it "@post1 should not contain errors" do
|
147
147
|
expect(@post1.errors.count).to be 1
|
148
148
|
expect(@post1.errors.added?(:title, :blank)).to be true
|
149
149
|
end
|
150
150
|
|
151
|
-
it "@post2 should not
|
151
|
+
it "@post2 should not contain errors" do
|
152
152
|
expect(@post2.errors.count).to be 1
|
153
153
|
expect(@post2.errors.keys).to eq [:body]
|
154
154
|
end
|
@@ -165,11 +165,11 @@ describe RestMyCase::Validator do
|
|
165
165
|
expect(@context.ok?).to be false
|
166
166
|
end
|
167
167
|
|
168
|
-
it "@post1 should not
|
168
|
+
it "@post1 should not contain errors" do
|
169
169
|
expect(@post1.errors.count).to be 0
|
170
170
|
end
|
171
171
|
|
172
|
-
it "@post2 should not
|
172
|
+
it "@post2 should not contain errors" do
|
173
173
|
expect(@post2.errors.count).to be 1
|
174
174
|
expect(@post2.errors.include?(:body)).to be true
|
175
175
|
end
|
@@ -186,11 +186,11 @@ describe RestMyCase::Validator do
|
|
186
186
|
expect(@context.ok?).to be true
|
187
187
|
end
|
188
188
|
|
189
|
-
it "@post1 should not
|
189
|
+
it "@post1 should not contain errors" do
|
190
190
|
expect(@post1.errors.count).to be 0
|
191
191
|
end
|
192
192
|
|
193
|
-
it "@post2 should not
|
193
|
+
it "@post2 should not contain errors" do
|
194
194
|
expect(@post2.errors.count).to be 0
|
195
195
|
end
|
196
196
|
end
|
@@ -206,15 +206,74 @@ describe RestMyCase::Validator do
|
|
206
206
|
expect(@context.ok?).to be true
|
207
207
|
end
|
208
208
|
|
209
|
-
it "@post1 should not
|
209
|
+
it "@post1 should not contain errors" do
|
210
210
|
expect(@post1.errors.count).to be 0
|
211
211
|
end
|
212
212
|
|
213
|
-
it "@post2 should not
|
213
|
+
it "@post2 should not contain errors" do
|
214
214
|
expect(@post2.errors.count).to be 0
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
218
218
|
end
|
219
|
+
|
220
|
+
context "When passing an object with nested objects" do
|
221
|
+
|
222
|
+
context "and both of the nested object are valid" do
|
223
|
+
before do
|
224
|
+
@post_with_comments = RubyPostWithComments.new([
|
225
|
+
{ title: 'first comment' },
|
226
|
+
{ title: 'second comment' }
|
227
|
+
])
|
228
|
+
|
229
|
+
@context = NestedValidation.perform(post: @post_with_comments)
|
230
|
+
end
|
231
|
+
|
232
|
+
it "@context.ok? should be true" do
|
233
|
+
expect(@context.ok?).to be true
|
234
|
+
end
|
235
|
+
|
236
|
+
it "@post_with_comments should not contain errors" do
|
237
|
+
expect(@post_with_comments.errors.count).to be 0
|
238
|
+
end
|
239
|
+
|
240
|
+
it "@post_with_comments.comments should not contain errors" do
|
241
|
+
@post_with_comments.comments.each do |comment|
|
242
|
+
expect(comment.errors.count).to be 0
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
context "and one of them is invalid" do
|
248
|
+
before do
|
249
|
+
@post_with_comments = RubyPostWithComments.new([
|
250
|
+
{ title: 'first comment' },
|
251
|
+
{}
|
252
|
+
])
|
253
|
+
|
254
|
+
@context = NestedValidation.perform(post: @post_with_comments)
|
255
|
+
end
|
256
|
+
|
257
|
+
it "@context.ok? should be false" do
|
258
|
+
expect(@context.ok?).to be false
|
259
|
+
end
|
260
|
+
|
261
|
+
it "@post_with_comments should contain one error" do
|
262
|
+
expect(@post_with_comments.errors.count).to be 1
|
263
|
+
expect(@post_with_comments.errors.added?(:comments, :invalid)).to be true
|
264
|
+
end
|
265
|
+
|
266
|
+
it "@post_with_comments.comments[0] should not contain errors" do
|
267
|
+
expect(@post_with_comments.comments[0].errors.count).to be 0
|
268
|
+
end
|
269
|
+
|
270
|
+
it "@post_with_comments.comments[1] should contain one error" do
|
271
|
+
expect(@post_with_comments.comments[1].errors.count).to be 1
|
272
|
+
expect(@post_with_comments.comments[1].errors.added?(:title, :blank)).to be true
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
end
|
277
|
+
|
219
278
|
end
|
220
279
|
|
@@ -2,7 +2,7 @@ class RubyPostWithComments
|
|
2
2
|
|
3
3
|
class RubyComment
|
4
4
|
|
5
|
-
attr_accessor :title
|
5
|
+
attr_accessor :title
|
6
6
|
|
7
7
|
def initialize(attributes = {})
|
8
8
|
(attributes || {}).each { |name, value| send("#{name}=", value) }
|
@@ -10,19 +10,10 @@ class RubyPostWithComments
|
|
10
10
|
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
13
|
attr_reader :comments
|
15
14
|
|
16
|
-
def initialize(comments =
|
15
|
+
def initialize(comments = [])
|
17
16
|
@comments = comments.map { |comment| RubyComment.new(comment) }
|
18
|
-
@comments = [] if @comments.nil?
|
19
|
-
end
|
20
|
-
|
21
|
-
def first_two_comments
|
22
|
-
[
|
23
|
-
comments[0],
|
24
|
-
comments[1]
|
25
|
-
]
|
26
17
|
end
|
27
18
|
|
28
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_my_case
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.10.
|
4
|
+
version: 1.10.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- goncalvesjoao
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- spec/support/validator/usecases/custom_validator.rb
|
165
165
|
- spec/support/validator/usecases/hierarchy_validation.rb
|
166
166
|
- spec/support/validator/usecases/length_validator.rb
|
167
|
+
- spec/support/validator/usecases/nested_validation.rb
|
167
168
|
- spec/support/validator/usecases/numericality_validator.rb
|
168
169
|
homepage: https://github.com/goncalvesjoao/rest_my_case
|
169
170
|
licenses:
|
@@ -212,4 +213,5 @@ test_files:
|
|
212
213
|
- spec/support/validator/usecases/custom_validator.rb
|
213
214
|
- spec/support/validator/usecases/hierarchy_validation.rb
|
214
215
|
- spec/support/validator/usecases/length_validator.rb
|
216
|
+
- spec/support/validator/usecases/nested_validation.rb
|
215
217
|
- spec/support/validator/usecases/numericality_validator.rb
|