Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Improve hexcode format validation error message

Alexander Brandon Coles2d94f9201d64
diff --git a/app/models/color.rb b/app/models/color.rb
index 1fefd536f7b..133c2bfceba 100644
--- a/app/models/color.rb
+++ b/app/models/color.rb
@@ -39,7 +39,7 @@ class Color < ApplicationRecord
validates :name, :hexcode, presence: true
validates :name, length: { maximum: 255 }
- validates :hexcode, format: { with: RGB_HEX_FORMAT, allow_blank: true }
+ validates :hexcode, format: { with: RGB_HEX_FORMAT, message: :hexcode_invalid, allow_blank: true }
normalizes :hexcode, with: ::Colors::HexColor::Normalizer
end
diff --git a/app/models/design_color.rb b/app/models/design_color.rb
index 10cff1b7619..5c83c1daab0 100644
--- a/app/models/design_color.rb
+++ b/app/models/design_color.rb
@@ -45,7 +45,7 @@ class DesignColor < ApplicationRecord
validates :variable, :hexcode, presence: true
validates :variable, uniqueness: true
- validates :hexcode, format: { with: RGB_HEX_FORMAT, allow_blank: true }
+ validates :hexcode, format: { with: RGB_HEX_FORMAT, message: :hexcode_invalid, allow_blank: true }
normalizes :hexcode, with: ::Colors::HexColor::Normalizer
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 72fef6083ff..fdc4a5ce834 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1370,6 +1370,7 @@ en:
odd: "must be odd."
regex_match_failed: "does not match the regular expression %{expression}."
regex_invalid: "could not be validated with the associated regular expression."
+ hexcode_invalid: "is not a valid 6-digit hexadecimal color code."
smaller_than_or_equal_to_max_length: "must be smaller than or equal to maximum length."
taken: "has already been taken."
too_long: "is too long (maximum is %{count} characters)."
diff --git a/spec/models/color_spec.rb b/spec/models/color_spec.rb
index 6fc29522757..ffbe288e9ae 100644
--- a/spec/models/color_spec.rb
+++ b/spec/models/color_spec.rb
@@ -91,7 +91,9 @@ RSpec.describe Color do
end
it "does not allow malformed hexcodes" do
- expect(subject).not_to allow_values("0#FFFFFF", "#FFFFFF0", "white").for(:hexcode)
+ expect(subject).not_to allow_values("0#FFFFFF", "#FFFFFF0", "white")
+ .for(:hexcode)
+ .with_message("is not a valid 6-digit hexadecimal color code.")
end
it "allows valid hexcodes" do
diff --git a/spec/models/design_color_spec.rb b/spec/models/design_color_spec.rb
index 25b443d2038..4c2e8ce0d98 100644
--- a/spec/models/design_color_spec.rb
+++ b/spec/models/design_color_spec.rb
@@ -61,7 +61,9 @@ RSpec.describe DesignColor do
"#1",
"#1111111",
"#HHHHHH"
- ).for(:hexcode)
+ )
+ .for(:hexcode)
+ .with_message("is not a valid 6-digit hexadecimal color code.")
end
it "allows valid hexcodes" do
diff --git a/spec/seeders/env_data/custom_design_seeder_spec.rb b/spec/seeders/env_data/custom_design_seeder_spec.rb
index 8703bc81517..72eeab43a1a 100644
--- a/spec/seeders/env_data/custom_design_seeder_spec.rb
+++ b/spec/seeders/env_data/custom_design_seeder_spec.rb
@@ -162,7 +162,7 @@ RSpec.describe EnvData::CustomDesignSeeder, :webmock do
it "uses those variables" do
reset(:seed_design)
- expect { seeder.seed! }.to raise_error /Hex code is invalid/
+ expect { seeder.seed! }.to raise_error /Hex code is not a valid 6-digit hexadecimal color code./
end
end