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
|
# File 'lib/ree/contracts/arg_contracts/splat.rb', line 104
def message(value, name, lvl = 1)
unless value.is_a?(Array)
return "expected #{to_s}, got #{value.class} => #{truncate(value.inspect)}"
end
errors = []
sps = " " * lvl
if @validators.size - 1 > value.size
return "expected at least #{@validators.size - 1} #{pluralize(@validators.size, 'value', 'values')} for #{to_s}, got #{value.size} #{pluralize(value.size, 'value', 'values')} => #{truncate(value.inspect)}"
end
if @first_splat_of
rest_validators = @validators[1..-1]
rest_validator_count = rest_validators.size
rest_values = if value.size >= @validators.size
value[(value.size - rest_validator_count)..-1]
else
value
end
if rest_validator_count > 0
rest_validators.each_with_index do |validator, idx|
val = rest_values[idx]
next if validator.call(val)
msg = validator.message(val, "#{name}[#{idx + 1}]", lvl + 1)
errors << "\n\t#{sps} - #{name}[#{idx + 1}]: #{msg}"
if errors.size > 3
errors << "\n\t#{sps} - ..."
break
end
end
end
if rest_values.size != value.size
validator = @validators.first
idx = value.size - rest_validator_count - 1
val = value[0..idx]
if !validator.call(val)
msg = validator.message(val, "#{name}[0..#{idx}]", lvl + 1)
errors << "\n\t#{sps} - #{name}[0..#{idx}]: #{msg}"
end
end
elsif @last_splat_of
rest_validators = @validators[0..-2]
rest_validator_count = rest_validators.size
rest_values = if value.size >= @validators.size
value[0..-2]
else
value
end
if rest_validator_count > 0
rest_validators.each_with_index do |validator, idx|
val = rest_values[idx]
next if validator.call(val)
msg = validator.message(val, "#{name}[#{idx}]", lvl + 1)
errors << "\n\t#{sps} - #{name}[#{idx}]: #{msg}"
if errors.size > 3
errors << "\n\t#{sps} - ..."
break
end
end
end
if rest_values.size != value.size
validator = @validators.last
idx = rest_validator_count
val = value[idx..-1]
if !validator.call(val)
msg = validator.message(val, "#{name}[#{idx}..#{value.size - 1}]", lvl + 1)
errors << "\n\t#{sps} - #{name}[#{idx}..#{value.size - 1}]: #{msg}"
end
end
elsif @middle_splat_of
left_contract_count = @middle_splat_of_index
right_contract_count = @validators.size - @middle_splat_of_index - 1
value[0..left_contract_count - 1].each_with_index do |value, idx|
validator = @validators[idx]
next if validator.call(value)
msg = validator.message(value, "#{name}[#{idx}]", lvl + 1)
errors << "\n\t#{sps} - #{name}[#{idx}]: #{msg}"
if errors.size > 3
errors << "\n\t#{sps} - ..."
break
end
end
splat_values = if left_contract_count + right_contract_count == value.size
[]
else
value[@middle_splat_of_index..value.size - right_contract_count - 1]
end
if !splat_values.empty?
validator = @validators[@middle_splat_of_index]
if !validator.call(splat_values)
splat_name = "#{name}[#{@middle_splat_of_index}..#{value.size - right_contract_count - 1}]"
msg = validator.message(splat_values, "#{splat_name}", lvl + 1)
errors << "\n\t#{sps} - #{splat_name}: #{msg}"
end
end
is_valid = value[value.size - right_contract_count..-1].each_with_index.all? do |value, idx|
@validators[@middle_splat_of_index + idx + 1].call(value)
end
pos = value.size - right_contract_count
value[pos..-1].each_with_index do |value, idx|
validator = @validators[@middle_splat_of_index + idx + 1]
next if validator.call(value)
msg = validator.message(value, "#{name}[#{pos + idx}]", lvl + 1)
errors << "\n\t#{sps} - #{name}[#{pos + idx}]: #{msg}"
if errors.size > 3
errors << "\n\t#{sps} - ..."
break
end
end
end
errors.join
end
|