Module: Ohm::Validations

Included in:
Model::Validations
Defined in:
lib/ohm/validations.rb

Overview

Provides a base implementation for extensible validation routines. Validations currently only provides the following assertions:

  • assert

  • assert_present

  • assert_format

  • assert_numeric

The core tenets that Ohm::Validations advocates can be summed up in a few bullet points:

  1. Validations are much simpler and better done using composition rather than macros.

  2. Error messages should be kept separate and possibly in the view or presenter layer.

  3. It should be easy to write your own validation routine.

Since Ohm’s philosophy is to keep the core code small, other validations are simply added on a per-model or per-project basis.

If you want other validations you may want to take a peek at Ohm::Contrib and all of the validation modules it provides.

Examples:

class Product < Ohm::Model
  attribute :title
  attribute :price
  attribute :date

  def validate
    assert_present :title
    assert_numeric :price
    assert_format  :date, /\A[\d]{4}-[\d]{1,2}-[\d]{1,2}\z
  end
end

product = Product.new
product.valid? == false
# => true

product.errors == [[:title, :not_present], [:price, :not_numeric],
                   [:date, :format]]
# => true

See Also:

Defined Under Namespace

Classes: Errors, Presenter

Instance Method Summary (collapse)

Instance Method Details

- (Object) assert(value, error) (protected)

The grand daddy of all assertions. If you want to build custom assertions, or even quick and dirty ones, you can simply use this method.

Examples:

class Post < Ohm::Model
  attribute :slug
  attribute :votes

  def validate
    assert_slug :slug
    assert votes.to_i > 0, [:votes, :not_valid]
  end

protected
  def assert_slug(att, error = [att, :not_slug])
    assert send(att).to_s =~ /\A[a-z\-0-9]+\z/, error
  end
end


208
209
210
# File 'lib/ohm/validations.rb', line 208

def assert(value, error)
  value or errors.push(error) && false
end

- (Object) assert_format(att, format, error = [att, :format]) (protected)

Allows you to do a validation check against a regular expression. It’s important to note that this internally calls #assert_present, therefore you need not structure your regular expression to check for a non-empty value.

Parameters:

  • att (Symbol)

    The attribute you want to verify the format of.

  • format (Regexp)

    The regular expression with which to compare the value of att with.

  • error (Array<Symbol, Symbol>) (defaults to: [att, :format])

    The error that should be returned when the validation fails.



159
160
161
162
163
# File 'lib/ohm/validations.rb', line 159

def assert_format(att, format, error = [att, :format])
  if assert_present(att, error)
    assert(send(att).to_s.match(format), error)
  end
end

- (Object) assert_numeric(att, error = [att, :not_numeric]) (protected)

Checks if all the characters of an attribute is a digit. If you want to verify that a value is a decimal, try looking at Ohm::Contrib’s assert_decimal assertion.

Parameters:

  • att (Symbol)

    The attribute you wish to verify the numeric format.

  • error (Array<Symbol, Symbol>) (defaults to: [att, :not_numeric])

    The error that should be returned when the validation fails.

See Also:



183
184
185
186
187
# File 'lib/ohm/validations.rb', line 183

def assert_numeric(att, error = [att, :not_numeric])
  if assert_present(att, error)
    assert_format(att, /^\d+$/, error)
  end
end

- (Object) assert_present(att, error = [att, :not_present]) (protected)

The most basic and highly useful assertion. Simply checks if the value of the attribute is empty.

Parameters:

  • att (Symbol)

    The attribute you wish to verify the presence of.

  • error (Array<Symbol, Symbol>) (defaults to: [att, :not_present])

    The error that should be returned when the validation fails.



171
172
173
# File 'lib/ohm/validations.rb', line 171

def assert_present(att, error = [att, :not_present])
  assert(!send(att).to_s.empty?, error)
end

- (Object) errors

All errors for this model.



143
144
145
# File 'lib/ohm/validations.rb', line 143

def errors
  @errors ||= Errors.new(self)
end

- (Boolean) valid?

Check if the current model state is valid. Each call to #valid? will reset the #errors array.

All model validations should be declared in a `validate` method.

Examples:

class Post < Ohm::Model
  attribute :title

  def validate
    assert_present :title
  end
end

Returns:

  • (Boolean)


132
133
134
135
136
# File 'lib/ohm/validations.rb', line 132

def valid?
  errors.clear
  validate
  errors.empty?
end

- (Object) validate

Base validate implementation.



139
140
# File 'lib/ohm/validations.rb', line 139

def validate
end