module Subvalid::Validator::API

Public Instance Methods

validate(object, validation_result=ValidationResult.new, *args) click to toggle source
# 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