Class: Apipie::Validator::NestedValidator

Inherits:
BaseValidator show all
Defined in:
lib/apipie/validator.rb

Instance Attribute Summary

Attributes inherited from BaseValidator

#param_description

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseValidator

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

Constructor Details

#initialize(param_description, argument, param_group) ⇒ NestedValidator

Returns a new instance of NestedValidator.



514
515
516
517
518
# File 'lib/apipie/validator.rb', line 514

def initialize(param_description, argument, param_group)
  super(param_description)
  @validator = Apipie::Validator::HashValidator.new(param_description, argument, param_group)
  @type = argument
end

Class Method Details

.build(param_description, argument, options, block) ⇒ Object



538
539
540
541
542
# File 'lib/apipie/validator.rb', line 538

def self.build(param_description, argument, options, block)
  # in Ruby 1.8.x the arity on block without args is -1
  # while in Ruby 1.9+ it is 0
  self.new(param_description, block, options[:param_group]) if block.is_a?(Proc) && block.arity <= 0 && argument == Array
end

Instance Method Details

#descriptionObject



548
549
550
# File 'lib/apipie/validator.rb', line 548

def description
  "Must be an Array of nested elements"
end

#expected_typeObject



544
545
546
# File 'lib/apipie/validator.rb', line 544

def expected_type
  'array'
end

#params_orderedObject



552
553
554
# File 'lib/apipie/validator.rb', line 552

def params_ordered
  @validator.params_ordered
end

#process_value(value) ⇒ Object



529
530
531
532
533
534
535
536
# File 'lib/apipie/validator.rb', line 529

def process_value(value)
  value ||= [] # Rails convert empty array to nil
  @values = []
  value.each do |child|
    @values << @validator.process_value(child)
  end
  @values
end

#validate(value) ⇒ Object



520
521
522
523
524
525
526
527
# File 'lib/apipie/validator.rb', line 520

def validate(value)
  value ||= [] # Rails convert empty array to nil
  return false if value.class != Array
  value.each do |child|
    return false unless @validator.validate(child)
  end
  true
end