class Toolchain::Validations::Validators::Format

Validates the format of an attribute using a regular expression.

@example

class Company::Creator
  validates :credit_card, format: {
    with: /^\d+{4}-$\d+{4}-\d+{4}-\d+{4}/,
    message: "invalid credit card format"
  }
end

Public Instance Methods

validate() click to toggle source
# File lib/toolchain/validations/validators/format.rb, line 15
def validate
  errors.add(key_path, message || "is invalid") if no_match?
end

Private Instance Methods

no_match?() click to toggle source
# File lib/toolchain/validations/validators/format.rb, line 21
def no_match?
  value.nil? || !value.match(data[:with])
end