0.0
No commit activity in last 3 years
No release in over 3 years
Find models in database which violate Active Record validation rules. Invalid models may raise unexpected error when updated.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 13.0
~> 3.0
~> 1.7
~> 0.5.1
~> 1.4

Runtime

>= 5.0
 Project Readme

Build Status Coverage Status Gem Version

model_validator

This gem can validate database against Active Record validation rules. This is useful since error may occur when manipulating such data.

How this can happen?

  • when database is modified without through Active Record api
  • when migrations modify database

How this gem can prevent such unexpected error?

Here is how this gem works:

  1. This gem search for all models (all final descendants of ApplicationRecord, which are not abstract).
  2. For each model, each record in database is fetch
  3. Active Record validations are played on these records
  4. All violations are logged

Here is the recommended way to use it:

  1. restore production database in a staging/preprod/non-production environment
  2. validate the database
  3. add missing migrations in case of violations
  4. repeat validation and fix until there is no more violation
  5. then, you are ready to deploy your application in production

Limitations

This gem fetch all record in database, and, for each record run the Active Record validation. So, because of performances reason, this may be only acceptable for tiny databases (thousand of records).

Installation

Add this line to your application"s Gemfile:

gem "model_validator"

And then execute:

$ bundle install

Or install it yourself as:

$ gem install model_validator

Rake task usage

Run rails db:validate to validate database of current environment. This is a rake task (see Rails -T db:validate)

You can skip some model with MODEL_VALIDATOR_SKIPPED_MODELS env var (ex: DB_VALIDATE_SKIP=Model1,Model2).

Ruby usage

Validation engine can also be used anywhere in your code:

require "model_validator"
ModelValidator.validate_all

This will skip ModelA and Model2 for validation:

ModelValidator.validate_all skipped_models: [Model1,Model2]

Development

After checking out the repo, run make install to install dependencies.

Then, run make test to run the tests (test coverage is available in coverage/index.html).

Then, run make lint to run linters (rubocop).

To install this gem onto your local machine, run make install-locally.

How to release new version?

Make sure your are in main branch. Then, run:

rake release:make[major|minor|patch|x.y.z]

Example for building a new minor release: rake release:make[minor]

Why not contributing to existing gem?

Many already existing gems may do the same, but none are satisfying:

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jibidus/model_validator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the ModelValidator project"s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.