Class: Apipie::Validator::HashValidator

Inherits:
BaseValidator show all
Includes:
DSL::Base, DSL::Param
Defined in:
lib/apipie/validator.rb

Instance Attribute Summary

Attributes included from DSL::Base

#api_params, #apipie_resource_descriptions

Attributes inherited from BaseValidator

#param_description

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Param

#param, #param_group, #property, #returns

Methods included from DSL::Base

#_apipie_eval_dsl

Methods inherited from BaseValidator

#==, #error, find, #format_description_value, #ignore_allow_blank?, inherited, #inspect, #inspected_fields, #param_name, raise_if_missing_params, #to_json, #to_s, #valid?

Constructor Details

#initialize(param_description, argument, param_group) ⇒ HashValidator

Returns a new instance of HashValidator.



333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/apipie/validator.rb', line 333

def initialize(param_description, argument, param_group)
  super(param_description)
  @proc = argument
  @param_group = param_group
  self.instance_exec(&@proc)
  # specifying action_aware on Hash influences the child params,
  # not the hash param itself: assuming it's required when
  # updating as well
  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



329
330
331
# File 'lib/apipie/validator.rb', line 329

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_scopeObject

where the group definition should be looked up when no scope given. This is expected to return a controller.



393
394
395
# File 'lib/apipie/validator.rb', line 393

def _default_param_group_scope
  @param_group && @param_group[:scope]
end

#descriptionObject



383
384
385
# File 'lib/apipie/validator.rb', line 383

def description
  "Must be a Hash"
end

#expected_typeObject



387
388
389
# File 'lib/apipie/validator.rb', line 387

def expected_type
  'hash'
end

#merge_with(other_validator) ⇒ Object



397
398
399
400
401
402
403
404
# File 'lib/apipie/validator.rb', line 397

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_orderedObject



347
348
349
350
351
352
353
354
# File 'lib/apipie/validator.rb', line 347

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
    options[:param_group] = @param_group
    Apipie::ParamDescription.from_dsl_data(param_description.method_description, args)
  end
end

#prepare_hash_paramsObject



406
407
408
409
410
# File 'lib/apipie/validator.rb', line 406

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



373
374
375
376
377
378
379
380
381
# File 'lib/apipie/validator.rb', line 373

def process_value(value)
  if @hash_params && value
    return @hash_params.each_with_object({}) do |(key, param), api_params|
      if value.key?(key)
        api_params[param.as] = param.process_value(value[key])
      end
    end
  end
end

#validate(value) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/apipie/validator.rb', line 356

def validate(value)
  return false if !value.is_a? Hash

  BaseValidator.raise_if_missing_params do |missing|
    @hash_params&.each do |k, p|
      if Apipie.configuration.validate_presence?
        missing << p if p.required && !value.key?(k)
      end
      if Apipie.configuration.validate_value?
        p.validate(value[k]) if value.key?(k)
      end
    end
  end

  return true
end