rainforestapp/schema_validations

Name: schema_validations

Owner: Rainforest QA

Description: Automatically creates validations basing on the database schema.

Created: 2015-12-27 20:30:53.0

Updated: 2016-08-23 21:26:34.0

Pushed: 2016-08-23 21:26:32.0

Homepage:

Size: 587

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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.

te_table :users do |t|
t.string :email, null: false, limit: 30
t.boolean :confirmed, null: false

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

s 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 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.

s User < ActiveRecord::Base

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

Installation

Simply add schema_validations to your Gemfile.

"schema_validations"
Which validations are covered?

Constraints:

| 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: ... } | | :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.

maValidations.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

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:
s User < ActiveRecord::Base
schema_validations auto_create: false

Use a custom validation rather than schema_validations automatic default:
s User < ActiveRecord::Base
schema_validations except: :email  # don't create default validation for email
validates :email, presence: true, length: { in: 5..30 }

Include validations every field, without a whitelist:
s User < ActiveRecord::Base
schema_validations whitelist: nil

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

ema_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:

Earlier versions of SchemaValidations supported:

Release Notes
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:

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

License

This gem is released under the MIT license.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.