Module: DataMapper::Validate::ValidatesFormat

Included in:
ClassMethods
Defined in:
lib/dm-validations/format_validator.rb

Overview

class FormatValidator

Instance Method Summary collapse

Instance Method Details

#validates_format(*fields) ⇒ Object

Validates that the attribute is in the specified format. You may use the :as (or :with, it's an alias) option to specify the pre-defined format that you want to validate against. You may also specify your own format via a Proc or Regexp passed to the the :as or :with options.

Examples:

Usage

require 'dm-validations'

class Page
  include DataMapper::Resource

  property :email, String
  property :zip_code, String

  validates_format :email, :as => :email_address
  validates_format :zip_code, :with => /^\d{5}$/

  # a call to valid? will return false unless:
  # email is formatted like an email address
  # and
  # zip_code is a string of 5 digits

Parameters:

  • :allow_nil<Boolean> (Hash)

    a customizable set of options

  • :as<Format, (Hash)

    a customizable set of options

  • :with<Format, (Hash)

    a customizable set of options


90
91
92
93
# File 'lib/dm-validations/format_validator.rb', line 90

def validates_format(*fields)
  opts = opts_from_validator_args(fields)
  add_validator_to_context(opts, fields, DataMapper::Validate::FormatValidator)
end