Custom validators for your ActiveModel objects
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.travis.yml
Gemfile
Guardfile
README.md
Rakefile
active_model_validators.gemspec

README.md

ActiveModel Validators Build Status

A suite of validators for ActiveModel objects.


Validators

  • validates_as_email uses the Mail gem to parse and validate email addresses.

Supported Ruby versions

Ruby 1.8 is not supported and will not work

The library should work with MRI, JRuby, and Rubinius

Installation

You can install via RubyGems

gem install active_model_validators

And then require the gem in your application.

require 'active_model_validators'

If you're using Bundler to manage your gem dependencies you can add the library to your Gemfile.

gem 'active_model_validators'

Example

require 'active_model'
require 'active_model_validators'

class Person
  include ActiveModel::Serialization
  include ActiveModel::Validations

  attr_accessor :attributes

  validates_as_email :email

  def initialize(attributes = {})
    self.attributes = attributes
  end

  def email
    attributes[:email]
  end

  def email=(address)
    attributes[:email] = address
  end
end

james = Person.new(email: 'james@logi.cl')
james.valid? # => true

james.email = 'invalid'
james.valid?         # => false
james.errors[:email] # => ["is invalid"]

TODO

  • TomDoc up!
  • Add UK postcode validator
  • Add US Zip code validator
  • Add IBAN number validator
  • Modularise each validator and share a common base validator before v1.0.