class Toolchain::Validations::Validators::Confirmation

Validates the confirmation of an attribute. In the example both `password` and `password_confirmation` attributes must match. One or more `nil` values will result in the validation error being triggered as well.

@example

class User::Creator
  validates :password, confirmation: {
    message: "doesn't match confirmation"
  }
end

Public Instance Methods

validate() click to toggle source
# File lib/toolchain/validations/validators/confirmation.rb, line 17
def validate
  if no_match?
    errors.add(key_path, message || "doesn't match confirmation")
  end
end

Private Instance Methods

confirmation() click to toggle source
# File lib/toolchain/validations/validators/confirmation.rb, line 29
def confirmation
  @confirmation ||= (
    keys = key_path.dup
    keys << :"#{keys.pop}_confirmation"
    keys.inject(object.send(keys.shift)) do |memo, key|
      memo[key] if memo.kind_of?(Hash)
    end
  )
end
no_match?() click to toggle source
# File lib/toolchain/validations/validators/confirmation.rb, line 25
def no_match?
  value.nil? || confirmation.nil? || value != confirmation
end