The project is in a healthy, maintained state
After each migration it generates a file with some validations. Each active record should include this file and can uns generated validations.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

GeneratedSchemaValidations

This Gem helps to transfer the defaults from the database schema to the rails validations. To do this, it uses the information in schema.rb and transfers it to a concern file. This file should be included in the version control.

Installation

Add this line to your application's Gemfile:

gem 'generated_schema_validations'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install generated_schema_validations

Generate concern file

To generate app/models/concerns/schema_validations.rb:

$ rails db:migrate

Use validations

Add to app/models/application_record.rb:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  include SchemaValidations # to include auto generated validations
end

Use it in a model after you defined the associations:

class User < ApplicationRecord
  belongs_to :client
  belongs_to :other

  validate :email_address, email_format: true

  schema_validations # to use auto generated validations

  def stuff; end
end

Exclude from rubocop and simplecov

Add to simplecov config file (e.g. .simplecov):

  add_filter '/app/models/concerns/schema_validations.rb'

Add to rubocop config file (e. g. config/rubocop.rb):

AllCops:
  Exclude:
  - app/models/concerns/schema_validations.rb

Usage

Every time you change schema.rb file with rake tasks db:schema:dump the generated file schema_validations.rb will fresh created. The task db:migrate use intern db:schema:dump.

It is hardcoded, that this will only run on development environment.

How it works

t.text :stuff, null: false
# results in
validate :stuff, presence: true

t.string :stuff, limit: 10
# results in
validate :stuff, length: { maximum: 10 }

t.integer :stuff
# results in
validate :stuff, numericality: true

You can watch changes on schema_validations.rb to understand the generated validations.

Changelog

0.3.0

  • Check usage of schema_validations (see #3)
  • Force excluding unique validations with where clause (see #2)

0.2.3

  • Add jsonb and xml as possible field type

0.2.2

  • Enable rails 7.0 usage

0.2.1

  • Close tempfile before reading

0.2.0

  • Add validations of date and datetime columns to be in database range

0.1.2

  • Exclude columns in unique validations

0.1.1

  • Add column type binary

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Lichtbit/generated_schema_validations.

License

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