Skip to content

A platform for community discussion. Free, open, simple.

FIX: Allow nil for properties values when they're not required (#26112)

Osama Sayeghf8964f8f8ff1
diff --git a/lib/theme_settings_object_validator.rb b/lib/theme_settings_object_validator.rb
index 9fe134374f2..3a2b9b9ec98 100644
--- a/lib/theme_settings_object_validator.rb
+++ b/lib/theme_settings_object_validator.rb
@@ -146,6 +146,8 @@ class ThemeSettingsObjectValidator
type = property_attributes[:type]
value = @object[property_name]
+ return true if value.nil?
+
case type
when "topic", "category", "upload", "post", "group", "tag"
if !valid_ids(type).include?(value)
diff --git a/spec/lib/theme_settings_object_validator_spec.rb b/spec/lib/theme_settings_object_validator_spec.rb
index d575648cf05..6d9c0fda162 100644
--- a/spec/lib/theme_settings_object_validator_spec.rb
+++ b/spec/lib/theme_settings_object_validator_spec.rb
@@ -230,6 +230,26 @@ RSpec.describe ThemeSettingsObjectValidator do
)
end
+ it "should not return any error messages when the value is not present and it's not required in the schema" 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 not present and it's required" do
+ schema = {
+ name: "section",
+ properties: {
+ float_property: {
+ type: "float",
+ required: true,
+ },
+ },
+ }
+ errors = described_class.new(schema: schema, object: {}).validate
+
+ expect(errors.keys).to eq(["/float_property"])
+ expect(errors["/float_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 of type float" do
errors = described_class.new(schema: schema, object: { float_property: "string" }).validate
@@ -278,6 +298,26 @@ RSpec.describe ThemeSettingsObjectValidator do
)
end
+ it "should not return any error messages when the value is not present and it's not required in the schema" 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 not present and it's required" do
+ schema = {
+ name: "section",
+ properties: {
+ integer_property: {
+ type: "integer",
+ required: true,
+ },
+ },
+ }
+ errors = described_class.new(schema: schema, object: {}).validate
+
+ expect(errors.keys).to eq(["/integer_property"])
+ expect(errors["/integer_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 of type integer" do
errors =
described_class.new(schema: schema, object: { integer_property: "string" }).validate
@@ -351,6 +391,26 @@ RSpec.describe ThemeSettingsObjectValidator do
).to eq({})
end
+ it "should not return any error messages when the value is not present and it's not required in the schema" 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 not present and it's required" do
+ schema = {
+ name: "section",
+ properties: {
+ string_property: {
+ type: "string",
+ required: true,
+ },
+ },
+ }
+ errors = described_class.new(schema: schema, object: {}).validate
+
+ expect(errors.keys).to eq(["/string_property"])
+ expect(errors["/string_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 of type string" do
schema = { name: "section", properties: { string_property: { type: "string" } } }
errors = described_class.new(schema: schema, object: { string_property: 1 }).validate
@@ -474,6 +534,27 @@ RSpec.describe ThemeSettingsObjectValidator do
).to eq({})
end
+ it "should not return any error messages when the value is not present and it's not required in the schema" do
+ schema = { name: "section", properties: { topic_property: { type: "topic" } } }
+ 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 not present and it's required" do
+ schema = {
+ name: "section",
+ properties: {
+ topic_property: {
+ type: "topic",
+ required: true,
+ },
+ },
+ }
+ errors = described_class.new(schema: schema, object: {}).validate
+
+ expect(errors.keys).to eq(["/topic_property"])
+ expect(errors["/topic_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 an integer" do
schema = { name: "section", properties: { topic_property: { type: "topic" } } }
@@ -545,6 +626,27 @@ RSpec.describe ThemeSettingsObjectValidator do
).to eq({})
end
+ it "should not return any error messages when the value is not present and it's not required in the schema" do
+ schema = { name: "section", properties: { upload_property: { type: "upload" } } }
+ 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 not present and it's required" do
+ schema = {
+ name: "section",
+ properties: {
+ upload_property: {
+ type: "upload",
+ required: true,
+ },
+ },
+ }
+ errors = described_class.new(schema: schema, object: {}).validate
+
+ expect(errors.keys).to eq(["/upload_property"])
+ expect(errors["/upload_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 an integer" do
schema = { name: "section", properties: { upload_property: { type: "upload" } } }
@@ -616,6 +718,19 @@ RSpec.describe ThemeSettingsObjectValidator do
).to eq({})
end
+ it "should not return any error messages when the value is not present and it's not required in the schema" do
+ schema = { name: "section", properties: { tag_property: { type: "tag" } } }
+ 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 not present and it's required" do
+ schema = { name: "section", properties: { tag_property: { type: "tag", required: true } } }
+ errors = described_class.new(schema: schema, object: {}).validate
+
+ expect(errors.keys).to eq(["/tag_property"])
+ expect(errors["/tag_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 an integer" do
schema = { name: "section", properties: { tag_property: { type: "tag" } } }
@@ -685,6 +800,27 @@ RSpec.describe ThemeSettingsObjectValidator do
).to eq({})
end
+ it "should not return any error messages when the value is not present and it's not required in the schema" do
+ schema = { name: "section", properties: { group_property: { type: "group" } } }
+ 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 not present and it's required" do
+ schema = {
+ name: "section",
+ properties: {
+ group_property: {
+ type: "group",
+ required: true,
+ },
+ },
+ }
+ errors = described_class.new(schema: schema, object: {}).validate
+
+ expect(errors.keys).to eq(["/group_property"])
+ expect(errors["/group_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 an integer" do
schema = { name: "section", properties: { group_property: { type: "group" } } }
@@ -756,6 +892,27 @@ RSpec.describe ThemeSettingsObjectValidator do
).to eq({})
end
+ it "should not return any error messages when the value is not present and it's not required in the schema" do
+ schema = { name: "section", properties: { post_property: { type: "post" } } }
+ 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 not present and it's required" do
+ schema = {
+ name: "section",
+ properties: {
+ post_property: {
+ type: "post",
+ required: true,
+ },
+ },
+ }
+ errors = described_class.new(schema: schema, object: {}).validate
+
+ expect(errors.keys).to eq(["/post_property"])
+ expect(errors["/post_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 an integer" do
schema = { name: "section", properties: { post_property: { type: "post" } } }
@@ -825,6 +982,27 @@ RSpec.describe ThemeSettingsObjectValidator do
).to eq({})
end
+ it "should not return any error messages when the value is not present and it's not required in the schema" do
+ schema = { name: "section", properties: { category_property: { type: "category" } } }
+ 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 not present and it's required" do
+ schema = {
+ name: "section",
+ properties: {
+ category_property: {
+ type: "category",
+ required: true,
+ },
+ },
+ }
+ errors = described_class.new(schema: schema, object: {}).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 an integer" do
schema = { name: "section", properties: { category_property: { type: "category" } } }