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.
Add this line to your application's Gemfile:
gem 'db_validator'
Then execute:
$ bundle install
The simplest way to run validation is using the provided rake task:

$ rake db_validator:validate
This will start an interactive mode where you can select which models to validate and adjust other options.
$ rake db_validator:validate models=user,post
$ rake db_validator:validate limit=1000
$ rake db_validator:validate format=json
You can test new validation rules before applying them to your models:
$ rake db_validator:test model=User rule='validates :name, presence: true'
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.
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
You can also run validation from your Ruby code:
report = DbValidator.validate
report = DbValidator.validate(
only_models: ['User', 'Post'],
limit: 1000,
report_format: :json
)
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")
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: \"\")"
]
}
]
}
}