DEV: Change group type to groups type for theme object schema (#26417)

Why this change?

This is a follow-up to 86b2e3a.

Basically, we want to allow people to select more than 1 group as well.

What does this change do?

1. Change `type: group` to `type: groups` and support `min` and `max`
   validations for `type: groups`.

2. Fix the `<SchemaThemeSetting::Types::Groups>` component to support the
   `min` and `max` validations and switch it to use the `<GroupChooser>` component
   instead of the `<ComboBoxComponent>` component which previously only supported
   selecting a single group.
This commit is contained in:
Alan Guo Xiang Tan
2024-03-28 22:05:48 +08:00
committed by GitHub
parent 186d6e4996
commit a670d6d4af
9 changed files with 218 additions and 105 deletions

View File

@ -845,19 +845,19 @@ RSpec.describe ThemeSettingsObjectValidator do
end
end
context "for group properties" do
it "should not return any error message when the value of the property is a valid id of a group record" do
context "for groups properties" do
it "should not return any error message when the value of the property is an array of valid group record ids" do
group = Fabricate(:group)
schema = { name: "section", properties: { group_property: { type: "group" } } }
schema = { name: "section", properties: { groups_property: { type: "groups" } } }
expect(
described_class.new(schema: schema, object: { group_property: group.id }).validate,
described_class.new(schema: schema, object: { groups_property: [group.id] }).validate,
).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" } } }
schema = { name: "section", properties: { groups_property: { type: "groups" } } }
expect(described_class.new(schema: schema, object: {}).validate).to eq({})
end
@ -865,44 +865,85 @@ RSpec.describe ThemeSettingsObjectValidator do
schema = {
name: "section",
properties: {
group_property: {
type: "group",
groups_property: {
type: "groups",
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")
expect(errors.keys).to eq(["/groups_property"])
expect(errors["/groups_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" } } }
it "should return the right hash of error messages when value of property is not an array of valid group ids" do
schema = { name: "section", properties: { groups_property: { type: "groups" } } }
errors = described_class.new(schema: schema, object: { group_property: "string" }).validate
errors = described_class.new(schema: schema, object: { groups_property: "string" }).validate
expect(errors.keys).to eq(["/group_property"])
expect(errors.keys).to eq(["/groups_property"])
expect(errors["/group_property"].full_messages).to contain_exactly(
"must be a valid group id",
expect(errors["/groups_property"].full_messages).to contain_exactly(
"must be an array of valid group ids",
)
end
it "should return the right hash of error messages when value of property is not a valid id of a group record" do
it "should return the right hash of error messages when number of groups ids does not satisfy min or max validations" do
group_1 = Fabricate(:group)
group_2 = Fabricate(:group)
group_3 = Fabricate(:group)
schema = {
name: "section",
properties: {
group_property: {
type: "group",
type: "groups",
validations: {
min: 1,
max: 2,
},
},
},
}
errors = described_class.new(schema: schema, object: { group_property: [] }).validate
expect(errors.keys).to eq(["/group_property"])
expect(errors["/group_property"].full_messages).to contain_exactly(
"must have at least 1 group ids",
)
errors =
described_class.new(
schema: schema,
object: {
group_property: [group_1.id, group_2.id, group_3.id],
},
).validate
expect(errors.keys).to eq(["/group_property"])
expect(errors["/group_property"].full_messages).to contain_exactly(
"must have at most 2 group ids",
)
end
it "should return the right hash of error messages when value of property is an array containing invalid group ids" do
schema = {
name: "section",
properties: {
groups_property: {
type: "groups",
},
child_groups: {
type: "objects",
schema: {
name: "child_group",
properties: {
group_property_2: {
type: "group",
groups_property_2: {
type: "groups",
},
},
},
@ -916,19 +957,19 @@ RSpec.describe ThemeSettingsObjectValidator do
described_class.new(
schema:,
object: {
group_property: 99_999_999,
child_groups: [{ group_property_2: 99_999_999 }],
groups_property: [99_999_999],
child_groups: [{ groups_property_2: [99_999_999] }],
},
).validate
expect(errors.keys).to eq(%w[/group_property /child_groups/0/group_property_2])
expect(errors.keys).to eq(%w[/groups_property /child_groups/0/groups_property_2])
expect(errors["/group_property"].full_messages).to contain_exactly(
"must be a valid group id",
expect(errors["/groups_property"].full_messages).to contain_exactly(
"must be an array of valid group ids",
)
expect(errors["/child_groups/0/group_property_2"].full_messages).to contain_exactly(
"must be a valid group id",
expect(errors["/child_groups/0/groups_property_2"].full_messages).to contain_exactly(
"must be an array of valid group ids",
)
end