scrivener-contrib

Extra validations for Scrivener.

Usage

The extra validations are:

assert_confirmation(:attribute)
assert_minimum_length(:attribute, integer)
assert_maximum_length(:attribute, integer)
assert_exact_length(:attribute, integer)
assert_unique(:attribute, :Model)

This is a basic example:

require "scrivener"
require "scrivener/contrib"
require "scrivener/ohm" # or require "scrivener/sequel"

class SignUp < Scrivener
  attr_accessor :username
  attr_accessor :name
  attr_accessor :idnum
  attr_accessor :password
  attr_accessor :password_confirmation

  def validate
    if assert_present(:password)
      assert_confirmation(:password)
      assert_minimum_length(:password, 6)
    end

    assert_maximum_length(:name, 50)
    assert_exact_length(:idnum, 8)

    if assert_present(:username)
      assert_unique(:username, :User)
    end
  end
end

 = SignUp.new(
  username: "repeated",
  password: "123",
  name: _very_long_name
)

.valid?
# => false

.errors[:password]
# => [:not_confirmed, :too_short]

.errors[:username]
# => [:not_unique]

.errors[:name]
# => [:too_long]

.errors[:idnum]
# => [:wrong_length]

Installation

$ gem install scrivener-contrib