Class: Apipie::Validator::HashValidator
Instance Attribute Summary
Attributes included from DSL::Base
#api_params, #apipie_resource_descriptions
#param_description
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from DSL::Param
#param, #param_group
#error, find, inherited, #param_name, #to_json, #to_s, #valid?
Constructor Details
#initialize(param_description, argument, param_group) ⇒ HashValidator
Returns a new instance of HashValidator.
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/apipie/validator.rb', line 211
def initialize(param_description, argument, param_group)
super(param_description)
@proc = argument
@param_group = param_group
self.instance_exec(&@proc)
if param_description.options[:action_aware] && param_description.options[:required]
param_description.required = true
end
prepare_hash_params
end
|
Class Method Details
.build(param_description, argument, options, block) ⇒ Object
207
208
209
|
# File 'lib/apipie/validator.rb', line 207
def self.build(param_description, argument, options, block)
self.new(param_description, block, options[:param_group]) if block.is_a?(Proc) && block.arity <= 0 && argument == Hash
end
|
Instance Method Details
#_default_param_group_scope ⇒ Object
where the group definition should be looked up when no scope given. This is expected to return a controller.
268
269
270
|
# File 'lib/apipie/validator.rb', line 268
def _default_param_group_scope
@param_group && @param_group[:scope]
end
|
#description ⇒ Object
258
259
260
|
# File 'lib/apipie/validator.rb', line 258
def description
"Must be a Hash"
end
|
#expected_type ⇒ Object
262
263
264
|
# File 'lib/apipie/validator.rb', line 262
def expected_type
'hash'
end
|
#merge_with(other_validator) ⇒ Object
272
273
274
275
276
277
278
279
|
# File 'lib/apipie/validator.rb', line 272
def merge_with(other_validator)
if other_validator.is_a? HashValidator
@params_ordered = ParamDescription.unify(self.params_ordered + other_validator.params_ordered)
prepare_hash_params
else
super
end
end
|
#params_ordered ⇒ Object
225
226
227
228
229
230
231
|
# File 'lib/apipie/validator.rb', line 225
def params_ordered
@params_ordered ||= _apipie_dsl_data[:params].map do |args|
options = args.find { |arg| arg.is_a? Hash }
options[:parent] = self.param_description
Apipie::ParamDescription.from_dsl_data(param_description.method_description, args)
end
end
|
#prepare_hash_params ⇒ Object
281
282
283
284
285
|
# File 'lib/apipie/validator.rb', line 281
def prepare_hash_params
@hash_params = params_ordered.reduce({}) do |h, param|
h.update(param.name.to_sym => param)
end
end
|
#process_value(value) ⇒ Object
248
249
250
251
252
253
254
255
256
|
# File 'lib/apipie/validator.rb', line 248
def process_value(value)
if @hash_params && value
return @hash_params.each_with_object({}) do |(key, param), api_params|
if value.has_key?(key)
api_params[param.as] = param.process_value(value[key])
end
end
end
end
|
#validate(value) ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/apipie/validator.rb', line 233
def validate(value)
return false if !value.is_a? Hash
if @hash_params
@hash_params.each do |k, p|
if Apipie.configuration.validate_presence?
raise ParamMissing.new(k) if p.required && !value.has_key?(k)
end
if Apipie.configuration.validate_value?
p.validate(value[k]) if value.has_key?(k)
end
end
end
return true
end
|