Module: Subvalid::Validator::API

Included in:
BlockValidator::Context
Defined in:
lib/subvalid/validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(object, validation_result = ValidationResult.new, *args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/subvalid/validator.rb', line 40

def validate(object, validation_result=ValidationResult.new, *args)
  validations.each do |attribute, validators|
    attribute_result = if attribute == :base
                         validation_result
                       else
                         ValidationResult.new
                       end

    validators.each do |validator_entry|
      validator = validator_entry.validator
      if attribute == :base
        validator.validate(object, attribute_result, *validator_entry.args)
      elsif object
        validator.validate(object.send(attribute), attribute_result, *validator_entry.args)
      else
        # no-op if we 're asked to validate an attribute for a nil value - that needs to be caught by a user defined `presence` validation instead
      end
    end

    validation_result.merge_child!(attribute, attribute_result) unless attribute == :base
  end
  validation_result
end