Home

Awesome

SchemaValidations

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

Overview

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
end

In that case the constraints null: false, limit: 30 and :boolean must be validated on the model level, to avoid ugly database exceptions:

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

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

SchemaValidations aims to DRY up your models, doing that boring work for you. It inspects the database and automatically creates validations based on the schema. After installing it your model is as simple as it can be.

class User < ActiveRecord::Base
end

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

Installation

Simply add schema_validations to your Gemfile.

gem "schema_validations"

Which validations are covered?

Constraints:

ConstraintValidation
null: falsevalidates ... presence: true
limit: 100validates ... length: { maximum: 100 }
unique: truevalidates ... uniqueness: true
unique: true, case_sensitive: false <br>(If schema_plus_pg_indexes is also in use)validates ... uniqueness: { case_sensitive: false }

Data types:

TypeValidation
:boolean:validates ... inclusion: { in: [true, false] }
:float:validates ... numericality: true
:integer:validates ... numericality: { only_integer: true, greater_than_or_equal_to: ..., less_than: ... }
:decimal, precision: ...:validates ... numericality: { greater_than: ..., less_than: ... }

What if I want something special?

SchemaValidations' behavior can be configured globally and per-model.

Global configuration

In an initializer, such as config/initializers/schema_validations.rb, you can set any of these options. The default values are shown.

SchemaValidations.setup do |config|

    # Whether to automatically create validations based on database constraints.
    # (Can be set false globally to disable the gem by default, and set true per-model to enable.)
    config.auto_create = true
    
    # Restricts the set of field names to include in automatic validation.
    # Value is a single name, an array of names, or nil.
    config.only = nil

    # Restricts the set of validation types to include in automatic validation.
    # Value is a single type, an array of types, or nil.
    # A type is specified as, e.g., `:validates_presence_of` or simply `:presence`.
    config.only_type = nil
    
    # A list of field names to exclude from automatic validation.
    # Value is a single name, an array of names, or nil.
    # (Providing a value per-model will completely replace a globally-configured list)
    config.except = nil
    
    # A list of validation types to exclude from automatic validation.
    # Value is a single type, an array of types, or nil.
    # (Providing a value per-model will completely replace a globally-configured list)
    config.except_type = nil
       
    # The base set of field names to always exclude from automatic validation.
    # Value is a single name, an array of names, or nil.
    # (This whitelist applies after all other considerations, global or per-model)
    config.whitelist = [:created_at, :updated_at, :created_on, :updated_on]
       
    # The base set of validation types to always exclude from automatic validation.
    # Value is a single type, an array of types, or nil.
    # (This whitelist applies after all other considerations, global or per-model)
    config.whitelist_type = nil
end

Per-model validation

You can override the global configuration per-model, using the schema_validations class method. All global configuration options are available as keyword options. For example:

Disable per model:
class User < ActiveRecord::Base
    schema_validations auto_create: false
end
Use a custom validation rather than schema_validations automatic default:
class User < ActiveRecord::Base
    schema_validations except: :email  # don't create default validation for email
    validates :email, presence: true, length: { in: 5..30 }
end
Include validations every field, without a whitelist:
class User < ActiveRecord::Base
    schema_validations whitelist: nil
end

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 (in order 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.

Compatibility

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

<!-- SCHEMA_DEV: MATRIX - begin --> <!-- These lines are auto-generated by schema_dev based on schema_dev.yml --> <!-- SCHEMA_DEV: MATRIX - end -->

Earlier versions of SchemaValidations supported:

Release Notes

2.4.1

2.4.0

2.3.0

2.2.1

2.2.0

2.1.1

2.1.0

2.0.2

2.0.1

2.0.0

This major version is backwards compatible for most uses. Only those who specified a per-model :except clause would be affected.

1.4.0

1.3.1

1.3.0

1.2.0

1.1.0

1.0.1

1.0.0

0.2.2

0.2.0

History

Testing

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: TEMPLATE USES SCHEMA_DEV - begin --> <!-- These lines are auto-inserted from a schema_dev template --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_DEV - end -->

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

License

This gem is released under the MIT license.