NotNaughty - The Validation Framework

NotNaughty extends your ruby Project with a highly custumizable validation API.

Features

Easy to adapt:

require 'rubygems'
require 'not_naughty'
Person = Struct.new(:name) do
  extend NotNaughty
  validates(:name) { presence and length :minimum => 4 }
  validated_before :clone
  validated_before :dup, :without => :exception
end
Person.new('Horst').valid? # => true
Person.new('Foo').valid? # => false
Person.new('Foo').clone # goes *boom* with NotNaughty::ValidationException
Person.new('Foo').dup # => false

Easy to extent:

class BunchynessValidation < NotNaughty::Validation
  def initialize(opts, attributes)
    __message = opts[:message] || '#{"%s".humanize} is not bunchy.'
    super opts, attributes do |o, a, v|
      o.errors.add(a, __message) unless v.respond_to? :to_bunchy
    end
  end
end

Thingy = Struct.new(:bunchy_item) do
  extend NotNaughty
  validates_bunchyness_of :bunchy_item
end

Handle SQL error gracefully:

class Person
    validator.error_handler.handle(SQLError) do |err|
      # do funny things...
    end
end

Syntactical Sugar with Builder methods:

validates(:username, :password) {length :minimum => 6}
validates(:password) {confirmed and complexity :level => :high}
validates(:if => :necessary?) {bunchyness_of :crap}

Beautiful error messages:

validates_presence_of :person,
  :message => '#{"%s".humanize} is gone missing.'

Conditional Validations:

validates(:if => :necessary?) {...}
validates(:unless => proc {|obj| obj.vip?}) {...}

API compatible to Ruby-Sequel[http://code.google.com/p/ruby-sequel/] as plugin:

class User < Sequel::Model
  is :not_naughty
  validates { presence_of :username and length_of :username, :within => 4..16 }
  has_validations? # => true

  # ...
end

See Sequel::Plugins::NotNaughty for details…

Copying

:include: COPYING