Louche
Louche adds common validators for ActiveModel/ActiveRecord classes.


Installation

Add this line to your application's Gemfile:

gem 'louche'

Usage

Louche provides a few validators to use in your ActiveModel/ActiveRecord classes:

EmailValidator

Example

class User < ActiveRecord::Base
  validates :email, email: true
end

User.new(email: '[email protected]').valid? # => true
User.new(email: 'foo@example').valid? # => false

Options

Option Description
:regex The regex used to validate the email (default: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
:message The ActiveRecord message added to the record errors (default: :invalid_email)

URLValidator

Example

class User < ActiveRecord::Base
  validates :website, url: true
end

User.new(website: 'http://example.com').valid? # => true
User.new(website: 'example.$$$').valid? # => false

Options

Option Description
:schemes The URI schemes to allow (default: %w(http https))
:message The ActiveRecord message added to the record errors (default: :invalid_url)

PhoneNumberValidator

Example

class User < ActiveRecord::Base
  validates :phone_number, phone_number: true
end

user = User.new(phone_number: '514 555-2525')
user.valid? # => true
user.phone_number # => '5145552525'

user = User.new(phone_number: '555-2525')
user.valid? # => false
user.phone_number # '5552525'

Options

Option Description
:regex The regex used to validate the number (default: /\A\d{10,}\z/)
:cleanup_regex The regex used to validate clean the input before validating/saving it (default: /[^\d]/)
:message The ActiveRecord message added to the record errors (default: :invalid_phone_number)

PostalCodeValidator

Example

class User < ActiveRecord::Base
  validates :postal_code, postal_code: true
end

user = User.new(postal_code: 'G0R 2T0')
user.valid? # => true
user.postal_code # => 'G0R2T0'

user = User.new(postal_code: 'L -0- L')
user.valid? # => false
user.postal_code # => 'L0L'

Options

Option Description
:regex The regex used to validate the code (default: /\A[a-z]\d[a-z]\d[a-z]\d\z/i)
:cleanup_regex The regex used to validate clean the input before validating/saving it (default: /[^a-z0-9]/i)
:message The ActiveRecord message added to the record errors (default: :invalid_postal_code)

ArrayValidator

Example

class Tag < Struct.new(:name)
  def valid?
    name.present?
  end
end

class User < ActiveRecord::Base
  validates :tags, array: true

  def tags=(tags)
    super tags.map { |tag| Tag.new(tag) }
  end
end

User.new(tags: ['food', 'beer', 'code']).valid? # => true
User.new(tags: ['food', '', 'code']).valid? # => false

Options

Option Description
:message The ActiveRecord message added to the record errors (default: :invalid_array)
:validity_method The method that will be sent to each array item (default: :valid?)

Localized error messages

Louche uses standard ActiveRecord messages. Here’s what your config/locales/activerecord.en.yml file could look like:

en:
  errors:
    messages:
      invalid_email: is not a valid email address
      invalid_url: is not a valid URL
      invalid_phone_number: is not a valid phone number
      invalid_postal_code: is not a valid postal code
      invalid_array: contains invalid items

License

Louche is © 2014 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.