mirror of
https://github.com/discourse/discourse.git
synced 2025-08-17 18:04:11 +08:00
FIX: Check tag group names in form template validator (#33850)
The previous form template validator did not detect non-existent tag group names, which would result in the page being saved normally when an incorrect name was entered, but causing the interface to crash during preview and a 500 error to be thrown when the user used it. This commit adds detection for non-existent tag group names and fixes this issue
This commit is contained in:
parent
ecfd35f3ca
commit
602efbdd45
3 changed files with 54 additions and 0 deletions
|
@ -5867,3 +5867,4 @@ en:
|
|||
duplicate_ids: "has duplicate ids"
|
||||
reserved_id: "has a reserved keyword as id: %{id}"
|
||||
unsafe_description: "has an unsafe HTML description"
|
||||
invalid_tag_group: "has invalid tag group: %{tag_group_name}"
|
||||
|
|
|
@ -31,6 +31,8 @@ class FormTemplateYamlValidator < ActiveModel::Validator
|
|||
check_ids(record, field, existing_ids)
|
||||
check_descriptions_html(record, field)
|
||||
end
|
||||
|
||||
check_tag_groups(record, yaml.map { |f| f["tag_group"] })
|
||||
rescue Psych::SyntaxError
|
||||
record.errors.add(:template, I18n.t("form_templates.errors.invalid_yaml"))
|
||||
end
|
||||
|
@ -81,4 +83,17 @@ class FormTemplateYamlValidator < ActiveModel::Validator
|
|||
|
||||
existing_ids << field["id"] if field["id"].present?
|
||||
end
|
||||
|
||||
def check_tag_groups(record, tag_group_names)
|
||||
tag_group_names = tag_group_names.compact.map(&:downcase).uniq
|
||||
valid_tag_group_names =
|
||||
TagGroup.where("lower(name) IN (?)", tag_group_names).pluck(:name).map(&:downcase)
|
||||
invalid_tag_groups = tag_group_names - valid_tag_group_names
|
||||
invalid_tag_groups.each do |tag_group_name|
|
||||
record.errors.add(
|
||||
:template,
|
||||
I18n.t("form_templates.errors.invalid_tag_group", tag_group_name: tag_group_name),
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -186,4 +186,42 @@ RSpec.describe FormTemplateYamlValidator, type: :validator do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#check_tag_groups" do
|
||||
fab!(:tag_group)
|
||||
|
||||
context "when tag group names are valid" do
|
||||
let(:yaml_content) { <<~YAML }
|
||||
- type: tag-chooser
|
||||
id: name
|
||||
tag_group: "#{tag_group.name}"
|
||||
YAML
|
||||
|
||||
it "does not add an error" do
|
||||
validator.validate(form_template)
|
||||
expect(form_template.errors[:template]).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
context "when tag group names contains invalid name" do
|
||||
let(:yaml_content) { <<~YAML }
|
||||
- type: tag-chooser
|
||||
id: name1
|
||||
tag_group: "#{tag_group.name}"
|
||||
- type: tag-chooser
|
||||
id: name2
|
||||
tag_group: "invalid tag group name"
|
||||
YAML
|
||||
|
||||
it "adds an error for invalid tag groups" do
|
||||
validator.validate(form_template)
|
||||
expect(form_template.errors[:template]).to include(
|
||||
I18n.t(
|
||||
"form_templates.errors.invalid_tag_group",
|
||||
tag_group_name: "invalid tag group name",
|
||||
),
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue