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

Properties of schema theme settings that are not marked `required: true` should accept nil as a value.
This commit is contained in:
Osama Sayegh 2024-03-09 14:25:30 +03:00 committed by GitHub
parent 9484174f32
commit f8964f8f8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 180 additions and 0 deletions

View File

@ -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)

View File

@ -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" } } }