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.
This commit is contained in:
Alan Guo Xiang Tan 2024-04-01 10:11:40 +08:00 committed by GitHub
parent 6cfeb62c29
commit a84757fd91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 2 deletions

View File

@ -208,7 +208,7 @@ class ThemeSettingsObjectValidator
end end
def is_property_present?(property_name) def is_property_present?(property_name)
if @object[property_name].nil? if @object[property_name].blank?
add_error(property_name, :required) add_error(property_name, :required)
false false
else else

View File

@ -1068,7 +1068,7 @@ RSpec.describe ThemeSettingsObjectValidator do
end end
end end
context "for category properties" do context "for categories properties" do
fab!(:category_1) { Fabricate(:category) } fab!(:category_1) { Fabricate(:category) }
fab!(:category_2) { 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({}) expect(described_class.new(schema: schema, object: {}).validate).to eq({})
end 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 it "should return the right hash of error messages when value of property is not present and it's required" do
schema = { schema = {
name: "section", name: "section",