Sha256: f6d5542d3b8fb7832d801b0bb5ceb6e49f97aa40d052cbad56f06132d2e32262

Contents?: true

Size: 812 Bytes

Versions: 8

Compression:

Stored size: 812 Bytes

Contents

module Reform::Contract::Validate
  def initialize(*)
    super
    @errors = build_errors
  end

  attr_reader :errors

  def validate
    validate!(errors, [])

    errors.empty?
  end

  def validate!(errors, prefix)
    validate_nested!(nested_errors = build_errors, prefix) # call valid? recursively and collect nested errors.

    valid?  # calls AM/Lotus validators and invokes self.errors=.

    errors.merge!(self.errors, prefix) # local errors.
    errors.merge!(nested_errors, [])
  end

private

  # runs form.validate! on all nested forms
  def validate_nested!(errors, prefixes)
    schema.each(twin: true) do |dfn|
      # recursively call valid? on nested form.
      Disposable::Twin::PropertyProcessor.new(dfn, self).() { |form| form.validate!(errors, prefixes+[dfn[:name]]) }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
reform-2.2.4 lib/reform/contract/validate.rb
reform-2.2.3 lib/reform/contract/validate.rb
reform-2.2.2 lib/reform/contract/validate.rb
reform-2.2.1 lib/reform/contract/validate.rb
reform-2.2.0 lib/reform/contract/validate.rb
reform-2.2.0.rc1 lib/reform/contract/validate.rb
reform-2.1.0 lib/reform/contract/validate.rb
reform-2.1.0.rc1 lib/reform/contract/validate.rb