Sunday, February 11, 2007

Ruby: Validatable

I finished up the 1.1.0 release this morning of Validatable. Validatable is a module that you can mix into your classes to add validations.
class Person
include Validatable
attr_accessor :name
validates_presence_of :name
end

person = Person.new
person.valid? #=> false
person.errors.on(:name) #=> "can't be empty"
Validatable currently supports
  • validates_presence_of
  • validates_length_of
  • validates_format_of
  • validates_confirmation_of
  • validates_acceptance_of
The validations are very similar to the validations that Rails provides. In addition to the traditional Rails functionality, Validatable provides 3 additional features.

Validation of an entire hierarchy of objects with errors aggregated at the root object.
class Person
include Validatable
validates_presence_of :name
attr_accessor :name
end

class PersonPresenter
include Validatable
include_validations_for :person
attr_accessor :person

def initialize(person)
@person = person
end
end

presenter = PersonPresenter.new(Person.new)
presenter.valid? #=> false
presenter.errors.on(:name) #=> "can't be blank"
Validations that turn off after X times of failed attempts.
class Person
include Validatable
validates_presence_of :name, :times => 1
attr_accessor :name
end

person = Person.new
person.valid? #=> false
person.valid? #=> true
Validations can be given levels. If a validation fails on a level the validations for subsequent levels will not be executed.
class Person
include Validatable
validates_presence_of :name, :level => 1, :message => "name message"
validates_presence_of :address, :level => 2
attr_accessor :name, :address
end

person = Person.new
person.valid? #=> false
person.errors.on(:name) #=> "name message"
person.errors.on(:address) #=> nil
Similar to Rails, Validatable also supports conditional validation.
class Person
include Validatable
attr_accessor :name
validates_format_of :name, :with => /.+/, :if => Proc.new { !name.nil? }
end
Person.new.valid? #=> true

6 comments:

  1. Anonymous5:44 AM

    Hey Jay!
    Been to validatable.rubyforge.org, installed it but seems there is smthg not working!

    ReplyDelete
  2. Anonymous9:09 AM

    Hello

    Thanks for letting me know. Can you be more specific about what's not working?

    Cheers, Jay

    ReplyDelete
  3. Hello Jay,

    is it compatible with ActiveRecord's validations ?

    ReplyDelete
  4. Anonymous7:04 AM

    Hello Stevatil,

    It's not meant to replace or add to AR validations. It's meant to add the same validation functionality to any object.

    Cheers, Jay

    ReplyDelete
  5. Hm. Installed it, but where did it go?
    % gem list validatable
    *** LOCAL GEMS ***
    validatable (1.5.2)
    Validatable is a library for adding validations.

    % irb
    irb(main):001:0> require 'rubygems'
    => true
    irb(main):005:0> gem 'validatable'
    Gem::LoadError: Could not find RubyGem validatable (>= 0.0.0)
    ...
    irb(main):003:0> require 'validatable'
    LoadError: no such file to load -- validatable
    ...

    kwerle@pobox.com

    ReplyDelete
  6. My fault. Was mixing ruby & jruby - which is fine, as long as you remember jruby's gem command is also gem, so you must specify the right path.

    ReplyDelete

Note: Only a member of this blog may post a comment.