Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

missing_presence_validation: ignore columns with defaults #171

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ Supported configuration options:
- `ignore_models` - models whose underlying tables' columns should not be checked.
- `ignore_attributes` - specific attributes, written as Model.attribute, that
should not be checked.
- `ignore_columns_with_default` - set to `true` to ignore columns with default values.

### Detecting Incorrect Presence Validations on Boolean Columns

Expand Down
3 changes: 2 additions & 1 deletion lib/active_record_doctor/config/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
detector :missing_presence_validation,
enabled: true,
ignore_models: [],
ignore_attributes: []
ignore_attributes: [],
ignore_columns_with_default: false

detector :missing_unique_indexes,
enabled: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class MissingPresenceValidation < Base # :nodoc:
},
ignore_attributes: {
description: "specific attributes, written as Model.attribute, that should not be checked"
},
ignore_columns_with_default: {
description: "ignore columns with default values, should be provided as boolean"
}
}

Expand All @@ -35,7 +38,12 @@ def detect

def validator_needed?(model, column)
![model.primary_key, "created_at", "updated_at", "created_on", "updated_on"].include?(column.name) &&
(!column.null || not_null_check_constraint_exists?(model.table_name, column))
(!column.null || not_null_check_constraint_exists?(model.table_name, column)) &&
!default_value_instead_of_validation?(column)
end

def default_value_instead_of_validation?(column)
!column.default.nil? && config(:ignore_columns_with_default)
end

def validator_present?(model, column)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,29 @@ def test_config_ignore_attributes

refute_problems
end

def test_config_ignore_columns_with_default_columns_are_not_ignored_by_default
Context.create_table(:users) do |t|
t.integer :posts_count, null: false, default: 0
end.define_model

assert_problems(<<~OUTPUT)
add a `presence` validator to Context::User.posts_count - it's NOT NULL but lacks a validator
OUTPUT
end

def test_config_ignore_columns_with_default
Context.create_table(:users) do |t|
t.integer :posts_count, null: false, default: 0
end.define_model

config_file(<<-CONFIG)
ActiveRecordDoctor.configure do |config|
config.detector :missing_presence_validation,
ignore_columns_with_default: true
end
CONFIG

refute_problems
end
end