0.0
The project is in a healthy, maintained state
DbValidator helps identify invalid records in your Rails application that don't meet model validation requirements. It finds records that became invalid after validation rule changes, and validates imported or manually edited data. You can use it to audit records before deploying new validations and catch any data that bypassed validation checks.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.2
~> 0.7.0
~> 0.23.1
~> 0.9.3
 Project Readme

Gem Version RSpec Tests

DbValidator

DbValidator helps identify invalid records in your Rails application that don't meet model validation requirements. It finds records that became invalid after validation rule changes, and validates imported or manually edited data. You can use it to audit records before deploying new validations and catch any data that bypassed validation checks.

Installation

Add this line to your application's Gemfile:

gem 'db_validator'

Then execute:

$ bundle install

Usage

Rake Task

The simplest way to run validation is using the provided rake task:

Validate models in interactive mode

Screenshot 2024-11-07 at 21 50 57
$ rake db_validator:validate

This will start an interactive mode where you can select which models to validate and adjust other options.

Validate specific models

$ rake db_validator:validate models=user,post

Limit the number of records to validate

$ rake db_validator:validate limit=1000

Generate JSON report

$ rake db_validator:validate format=json

Test Mode

You can test new validation rules before applying them to your models:

$ rake db_validator:test model=User rule='validates :name, presence: true'

Testing Email Format Validation

Here's an example of testing email format validation rules:

# Testing invalid email format (without @)
$ rake db_validator:test model=User rule='validates :email, format: { without: /@/, message: "must contain @" }'

Found 100 records that would become invalid out of 100 total records.

# Testing valid email format (with @)
$ rake db_validator:test model=User rule='validates :email, format: { with: /@/, message: "must contain @" }'

No invalid records found.

Error Handling

Trying to test a validation rule for a non-existent attribute will return an error:

❌ Error: Attribute 'i_dont_exist' does not exist for model 'User'
Available columns: id, email, created_at, updated_at, name

Ruby Code

You can also run validation from your Ruby code:

Validate all models

report = DbValidator.validate

Validate with options

report = DbValidator.validate(
  only_models: ['User', 'Post'],
  limit: 1000,
  report_format: :json
)

Report Format

Text Format (Default)

DbValidator Report
==================
Found invalid records:

User: 2 invalid records
ID: 1
  email is invalid (actual value: "invalid-email")
ID: 2
  name can't be blank (actual value: "")

Post: 1 invalid record
ID: 5
  title can't be blank (actual value: "")
  category is not included in the list (allowed values: news, blog, actual value: "invalid")

JSON Format

The JSON report is saved to a file in the db_validator_reports directory.

{
  "User": {
    "error_count": 2,
    "records": [
      {
        "id": 1,
        "errors": [
          "email is invalid (actual value: \"invalid-email\")"
        ]
      },
      {
        "id": 2,
        "errors": [
          "name can't be blank (actual value: \"\")"
        ]
      }
    ]
  }
}