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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/subvalid/validator.rb', line 52

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|
      if validator_entry.modifiers.map{|m| m.call(object)}.all?
        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
    end

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