Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Automatically creates validations basing on the database schema.
Latest commit 4d910cf @ronen ronen version bump 1.4.0
Failed to load latest commit information.
gemfiles test against ActiveRecord 4.2 (latest) rather than 4.2.1
spec Add specs
.gitignore ignore Gemfile.local
.travis.yml test against ActiveRecord 4.2 (latest) rather than 4.2.1
Gemfile removed reference to github repository in Gemfile
MIT-LICENSE extracted schema_validations from schema_plus version bump 1.4.0
Rakefile use schema_dev
init.rb extracted schema_validations from schema_plus
schema_dev.yml test against ActiveRecord 4.2 (latest) rather than 4.2.1
schema_validations.gemspec Switch to using schema_plus_columns rather than schema_plus, for AR 4…


SchemaValidations is an ActiveRecord extension that keeps your model class definitions simpler and more DRY, by automatically defining validations based on the database schema.

Gem Version Build Status Coverage Status


One of the great things about Rails (ActiveRecord, in particular) is that it inspects the database and automatically defines accessors for all your columns, keeping your model class definitions simple and DRY. That's great for simple data columns, but where it falls down is when your table contains constraints.

create_table :users do |t|
  t.string :email, :null => false, :limit => 30
  t.boolean :confirmed, :null => false

In that case :null => false, :limit => 30 and :boolean must be covered on the model level.

class User < ActiveRecord::Base
  validates :email, :presence => true, :length => { :maximum => 30 }
  validates :confirmed, :presence => true, :inclusion => { :in => [true, false] }

...which isn't the most DRY approach.

SchemaValidations aims to cover that and does boring work for you. It inspect the database and automatically creates validations basing on the schema. After installing it your model is as simple as it can be.

class User < ActiveRecord::Base

Validations are there but they are created by schema_validations under the hood.


As of version 1.2.0, SchemaValidations supports and is tested on:

  • ruby 2.1.5 with activerecord 4.2, using mysql2, postgresql or sqlite3

Earlier versions of SchemaValidations supported:

  • rails 3.2, 4.1, and 4.2.0
  • MRI ruby 1.9.3 and 2.1.5


Simply add schema_validations to your Gemfile.

gem "schema_validations"

What if I want something special?

SchemaValidations is highly customizable. You can configure behavior globally via SchemaValidations.setup or per-model via SchemaValidations::ActiveRecord::schema_validations, such as:

class User < ActiveRecord::Base
  schema_validations :except => :email
  validates :email, :presence => true, :length => { :in => 5..30 }

See SchemaValidations::Config for the available options.

This seems cool, but I'm worried about too much automagic

You can globally turn off automatic creation in config/initializers/schema_validations.rb:

SchemaValidations.setup do |config|
  config.auto_create = false

Then in any model where you want automatic validations, just do

class Post < ActiveRecord::Base

You can also pass options as per above.

Which validations are covered?


Constraint Validation
:null => false validates ... :presence => true
:limit => 100 validates ... :length => { :maximum => 100 }
:unique => true validates ... :uniqueness => true
:unique => true, :case_sensitive => false
(If schema_plus_pg_indexes is also in use)
validates ... :uniqueness => { :case_sensitive => false }

Data types:

Type Validation
:boolean :validates ... :inclusion => { :in => [true, false] }
:float :validates ... :numericality => true
:integer :validates ... :numericality => { :only_integer => true, :greater_than_or_equal_to => ..., :less_than => ... }

How do I know what it did?

If you're curious (or dubious) about what validations SchemaValidations defines, you can check the log file. For every assocation that SchemaValidations defines, it generates a debug entry in the log such as

[schema_validations] Article.validates_length_of :title, :allow_nil=>true, :maximum=>50

which shows the exact validation definition call.

SchemaValidations defines the validations lazily for each class, only creating them when they are needed (to validate a record of the class, or in response to introspection on the class). So you may need to search through the log file for "schema_validations" to find all the validations, and some classes' validations may not be defined at all if they were never needed for the logged use case.

Release Notes


  • Add support for case-insensitive uniqueness. Thanks to allenwq


  • Change log level from 'info' to 'debug', since there's no need to clutter production logs with this sort of development info. Thanks to @obduk


  • Add range checks to integer validations. Thanks to @lowjoel


  • No longer pull in schema_plus's auto-foreign key behavior. Limited to AR >= 4.2.1


  • Works with Rails 4.2.


  • Fix enums in Rails 4.1. Thanks to @lowjoel


  • Works with Rails 4.0. Thanks to @davll
  • No longer support Rails < 3.2 or Ruby < 1.9.3



  • New feature: ActiveRecord#validators and ActiveRecord#validators_on now ensure schema_validations are loaded


  • SchemaValidations is derived from the "Red Hill On Rails" plugin schema_validations originally created by harukizaemon (

  • SchemaValidations was created in 2011 by Michał Łomnicki and Ronen Barzel


Are you interested in contributing to schema_validations? Thanks! Please follow the standard protocol: fork, feature branch, develop, push, and issue pull request.

Some things to know about to help you develop and test:

  • schema_dev: SchemaValidations uses schema_dev to facilitate running rspec tests on the matrix of ruby, activerecord, and database versions that the gem supports, both locally and on travis-ci

    To to run rspec locally on the full matrix, do:

      $ schema_dev bundle install
      $ schema_dev rspec

    You can also run on just one configuration at a time; For info, see schema_dev --help or the schema_dev README.

    The matrix of configurations is specified in schema_dev.yml in the project root.

Code coverage results will be in coverage/index.html -- it should be at 100% coverage.


This gem is released under the MIT license.

Something went wrong with that request. Please try again.