From 412b36cc9355eac18130c60429d7f9c29f3995fa Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Tue, 27 Feb 2024 10:18:38 +0800 Subject: [PATCH] FIX: Error when integer values are set as default of string type settings (#25898) Why this change? ``` some_setting: default: 0 type: string ``` A theme setting like the above will cause an error to be thrown on the server when importing the theme because the default would be parsed as an integer which caused an error to be thrown when we are validating the value of the setting. What does this change do? Convert the value to a string when working with string typed theme settings. --- lib/theme_settings_validator.rb | 2 +- spec/fixtures/theme_settings/valid_settings.yaml | 4 ++++ spec/lib/theme_settings_validator_spec.rb | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 spec/lib/theme_settings_validator_spec.rb diff --git a/lib/theme_settings_validator.rb b/lib/theme_settings_validator.rb index 03385884376..db6725e54dd 100644 --- a/lib/theme_settings_validator.rb +++ b/lib/theme_settings_validator.rb @@ -45,7 +45,7 @@ class ThemeSettingsValidator ) when types[:string] validate_value_in_range!( - value.length, + value.to_s.length, min: opts[:min], max: opts[:max], errors:, diff --git a/spec/fixtures/theme_settings/valid_settings.yaml b/spec/fixtures/theme_settings/valid_settings.yaml index 4c3e3b480d4..6a39e85924e 100644 --- a/spec/fixtures/theme_settings/valid_settings.yaml +++ b/spec/fixtures/theme_settings/valid_settings.yaml @@ -14,6 +14,10 @@ string_setting_03: default: "string value" textarea: true +string_setting_04: + default: 0 + type: string + integer_setting: 51 integer_setting_02: diff --git a/spec/lib/theme_settings_validator_spec.rb b/spec/lib/theme_settings_validator_spec.rb new file mode 100644 index 00000000000..a47d423fc43 --- /dev/null +++ b/spec/lib/theme_settings_validator_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +RSpec.describe ThemeSettingsValidator do + describe ".validate_value" do + it "does not throw an error when an integer value is given with type `string`" do + errors = described_class.validate_value(1, ThemeSetting.types[:string], {}) + + expect(errors).to eq([]) + end + end +end