From a84757fd91f7b9dc341ec7e25b895c3d433ce4fc Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Mon, 1 Apr 2024 10:11:40 +0800 Subject: [PATCH] FIX: Error not being raised for required typed categories property (#26443) Why this change? For a schema like this: ``` schema = { name: "section", properties: { category_property: { type: "categories", required: true, }, }, } ``` When the value of the property is set to an empty array, we are not raising an error which we should because the property is marked as required. --- lib/theme_settings_object_validator.rb | 2 +- .../theme_settings_object_validator_spec.rb | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/theme_settings_object_validator.rb b/lib/theme_settings_object_validator.rb index 2435d06cf16..96618c6fb65 100644 --- a/lib/theme_settings_object_validator.rb +++ b/lib/theme_settings_object_validator.rb @@ -208,7 +208,7 @@ class ThemeSettingsObjectValidator end def is_property_present?(property_name) - if @object[property_name].nil? + if @object[property_name].blank? add_error(property_name, :required) false else diff --git a/spec/lib/theme_settings_object_validator_spec.rb b/spec/lib/theme_settings_object_validator_spec.rb index b992b8a1c0d..7d0e6be09a3 100644 --- a/spec/lib/theme_settings_object_validator_spec.rb +++ b/spec/lib/theme_settings_object_validator_spec.rb @@ -1068,7 +1068,7 @@ RSpec.describe ThemeSettingsObjectValidator do end end - context "for category properties" do + context "for categories properties" do fab!(:category_1) { Fabricate(:category) } fab!(:category_2) { Fabricate(:category) } @@ -1090,6 +1090,22 @@ RSpec.describe ThemeSettingsObjectValidator do expect(described_class.new(schema: schema, object: {}).validate).to eq({}) end + it "should return the right hash of error messages when value of property is present but empty and it's required" do + schema = { + name: "section", + properties: { + category_property: { + type: "categories", + required: true, + }, + }, + } + errors = described_class.new(schema: schema, object: { category_property: [] }).validate + + expect(errors.keys).to eq(["/category_property"]) + expect(errors["/category_property"].full_messages).to contain_exactly("must be present") + end + it "should return the right hash of error messages when value of property is not present and it's required" do schema = { name: "section",