Skip to content
Branch: master
Go to file

Latest commit


Failed to load latest commit information.
Latest commit message
Commit time

Gem Version Build Status Code Climate


Plain Old Ruby Objects + Rails Validations = self-checking Ruby objects.


  • Very readable error messages
  • Clean, minimal syntax

This is a small layer around ActiveModel::Validations. (About 18 lines of code.) So if you know how to use Rails Validations, you're good to go. I wrote this to help with CSV data imports and website microdata generation.


Writing a self-validating object

All of the ActiveModel::Validations are available, plus a new one, TypeValidator.

class Dog < ValidatedObject::Base
  # Plain old Ruby
  attr_accessor :name, :birthday  # attr_reader is supported as well for read-only attributes

  # Plain old Rails
  validates :name, presence: true
  # A new type-validation if you'd like to use it
  validates :birthday, type: Date, allow_nil: true  # Strongly typed but optional

The included TypeValidator is what enables type: Date, above. All classes can be checked, as well as a pseudo-class Boolean. E.g.:

validates :premium_membership, type: Boolean

Instantiating and automatically validating

# This Dog instance validates itself at the end of instantiation.
spot = 'Spot')
# We can also explicitly test for validity because all of
# ActiveModel::Validations is available.
spot.valid?  # => true

spot.birthday =, 1, 23)
spot.valid?  # => true

Good error messages

Any of the standard Validations methods can be used to test an instance, plus the custom check_validations! convenience method:

spot.birthday = '2015-01-23'
spot.valid?  # => false
spot.check_validations!  # => ArgumentError: Birthday is a String, not a Date

Note the clear, explicit error message. These are great when reading a log file following a data import. It describes all the invalid conditions. Let's test it by making another attribute invalid: = nil
spot.check_validations!  # => ArgumentError: Name can't be blank; Birthday is a String, not a Date

Use in parsing data

I often use a validated object in a loop to import data, e.g.:

# Import a CSV file of dogs
dogs = []
csv.next_row do |row|
    dogs <<
  rescue ArgumentError => e

The result is that dogs is an array of guaranteed valid Dog objects. And the error log lists unparseable rows with good info for tracking down problems in the data.

Use in code generation

My microdata generation gem uses ValidatedObjects to recursively create well formed HTML / JSON-LD.


Add this line to your application's Gemfile:

gem 'validated_object'

And then execute:

$ bundle

Or install it yourself as:

$ gem install validated_object


(TODO: Verify these instructions.) After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at


The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.