Module: Reform::Contract::Validate

Included in:
Reform::Contract
Defined in:
lib/reform/contract/validate.rb

Instance Method Summary collapse

Instance Method Details

#custom_errorsObject



25
26
27
# File 'lib/reform/contract/validate.rb', line 25

def custom_errors
  @result.to_results.select { |result| result.is_a? Reform::Contract::CustomError }
end

#errors(*args) ⇒ Object

The #errors method will be removed in Reform 3.0 core.



15
16
17
# File 'lib/reform/contract/validate.rb', line 15

def errors(*args)
  Result::Errors.new(@result, self)
end

#initializeObject



3
4
5
6
7
8
# File 'lib/reform/contract/validate.rb', line 3

def initialize(*)
  # this will be removed in Reform 3.0. we need this for the presenting form, form builders
  # call the Form#errors method before validation.
  super
  @result = Result.new([])
end

#to_resultObject

:private: only used in tests so far. this will be the new API in #call, where you will get @result.



21
22
23
# File 'lib/reform/contract/validate.rb', line 21

def to_result
  @result
end

#validateObject



10
11
12
# File 'lib/reform/contract/validate.rb', line 10

def validate
  validate!(nil).success?
end

#validate!(name, pointers = []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/reform/contract/validate.rb', line 29

def validate!(name, pointers = [])
  # run local validations. this could be nested schemas, too.
  local_errors_by_group = Reform::Validation::Groups::Validate.(self.class.validation_groups, self).compact # TODO: discss compact

  # blindly add injected pointers. will be readable via #errors.
  # also, add pointers from local errors here.
  pointers_for_nested = pointers + local_errors_by_group.collect { |errs| Result::Pointer.new(errs, []) }.compact

  nested_errors = validate_nested!(pointers_for_nested)

  # Result: unified interface #success?, #messages, etc.
  @result = Result.new(custom_errors + local_errors_by_group + pointers, nested_errors)
end