Skip to content

Commit 51b593e

Browse files
committed
incorrect_length_validation: ignore STI models
1 parent 7b349fe commit 51b593e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/active_record_doctor/detectors/incorrect_length_validation.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def detect
3636
model_maximum = maximum_allowed_by_validations(model, column.name.to_sym)
3737
next if model_maximum == column.limit
3838

39+
# Add violation only to the root model of STI.
40+
next if (model_maximum.nil? || column.limit.nil?) && sti_subclass?(model)
41+
3942
problem!(
4043
model: model.name,
4144
attribute: column.name,
@@ -55,6 +58,11 @@ def maximum_allowed_by_validations(model, column)
5558
end
5659
length_validator ? length_validator.options[:maximum] : nil
5760
end
61+
62+
def sti_subclass?(model)
63+
model.columns_hash.include?(model.inheritance_column.to_s) &&
64+
model.base_class != model
65+
end
5866
end
5967
end
6068
end

test/active_record_doctor/detectors/incorrect_length_validation_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ def test_no_validation_and_limit_is_error
5050
OUTPUT
5151
end
5252

53+
def test_sti_subclasses_are_ignored
54+
Context.create_table(:users) do |t|
55+
t.string :email, limit: 64
56+
t.string :type, null: false
57+
end.define_model do
58+
end
59+
60+
Context.define_model(:Client, Context::User)
61+
62+
assert_problems(<<~OUTPUT)
63+
the schema limits users.email to 64 characters but there's no length validator on Context::User.email - remove the database limit or add the validator
64+
OUTPUT
65+
end
66+
5367
def test_no_validation_and_no_limit_is_ok
5468
require_arbitrary_long_text_columns!
5569

0 commit comments

Comments
 (0)