Module: CouchRest::Validation::ValidatesWithMethod

Included in:
ClassMethods
Defined in:
lib/couchrest/validation/validators/method_validator.rb

Overview

class MethodValidator

Instance Method Summary (collapse)

Instance Method Details

- (Object) validates_with_method(*fields)

Validate using the given method. The method given needs to return:

result::<Boolean>, Error Message::<String>

Examples:

Usage


class Page

  property :zip_code, String

  validates_with_method :in_the_right_location?

  def in_the_right_location?
    if @zip_code == "94301"
      return true
    else
      return [false, "You're in the wrong zip code"]
    end
  end

  # A call to valid? will return false and
  # populate the object's errors with "You're in the
  # wrong zip code" unless zip_code == "94301"

  # You can also specify field:

  validates_with_method :zip_code, :in_the_right_location?

  # it will add returned error message to :zip_code field


82
83
84
85
# File 'lib/couchrest/validation/validators/method_validator.rb', line 82

def validates_with_method(*fields)
  opts = opts_from_validator_args(fields)
  add_validator_to_context(opts, fields, CouchRest::Validation::MethodValidator)
end