DEV: Enforce dominant_color length in validation (#18309)
The `add_column` `limit` parameter has no effect on a postgres `text` column. Instead we can perform the check in ActiveRecord. We never expect this condition to be hit - users cannot control this value. It's just a safety net.
This commit is contained in:
parent
c73ca74585
commit
0f5a8cc526
|
@ -37,6 +37,7 @@ class Upload < ActiveRecord::Base
|
||||||
|
|
||||||
validates_presence_of :filesize
|
validates_presence_of :filesize
|
||||||
validates_presence_of :original_filename
|
validates_presence_of :original_filename
|
||||||
|
validates :dominant_color, length: { is: 6 }, allow_blank: true, allow_nil: true
|
||||||
|
|
||||||
validates_with UploadValidator
|
validates_with UploadValidator
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
class AddDominantColorToUploads < ActiveRecord::Migration[7.0]
|
class AddDominantColorToUploads < ActiveRecord::Migration[7.0]
|
||||||
def change
|
def change
|
||||||
add_column :uploads, :dominant_color, :text, limit: 6, null: true
|
add_column :uploads, :dominant_color, :text, null: true
|
||||||
add_index :uploads, :id, where: "dominant_color IS NULL"
|
add_index :uploads, :id, where: "dominant_color IS NULL"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -689,5 +689,22 @@ RSpec.describe Upload do
|
||||||
|
|
||||||
expect(invalid_image.dominant_color).to eq(nil)
|
expect(invalid_image.dominant_color).to eq(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "is validated for length" do
|
||||||
|
u = Fabricate(:upload)
|
||||||
|
|
||||||
|
# Acceptable values
|
||||||
|
u.update!(dominant_color: nil)
|
||||||
|
u.update!(dominant_color: "")
|
||||||
|
u.update!(dominant_color: "abcdef")
|
||||||
|
|
||||||
|
expect {
|
||||||
|
u.update!(dominant_color: "toomanycharacters")
|
||||||
|
}.to raise_error(ActiveRecord::RecordInvalid)
|
||||||
|
|
||||||
|
expect {
|
||||||
|
u.update!(dominant_color: "abcd")
|
||||||
|
}.to raise_error(ActiveRecord::RecordInvalid)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue