Class: Ippon::Validate::Form

Inherits:
Schema
  • Object
show all
Defined in:
lib/ippon/validate.rb

Overview

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Schema

#&, #unhalt, #validate, #validate!, #|

Constructor Details

#initialize(fields, partial: false) ⇒ Form

Returns a new instance of Form.


737
738
739
740
# File 'lib/ippon/validate.rb', line 737

def initialize(fields, partial: false)
  @fields = fields
  @partial = partial
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields


735
736
737
# File 'lib/ippon/validate.rb', line 735

def fields
  @fields
end

Instance Method Details

#process(result) ⇒ Object

Implements the Schema#process interface.


743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/ippon/validate.rb', line 743

def process(result)
  values = {}

  # Process all fields:
  results = @fields.map do |key, field|
    field_result = result.dup.push_path(key)
    field.process(field_result)
    [key, field_result]
  end

  # Propgate state:
  results.each do |key, field_result|
    if @partial && field_result.errors.any? { |e| e.step.type == :fetch }      # do nothing

    else
      values[key] = field_result.value
      result.add_errors_from(field_result)
    end
  end

  result.value = values
end