2023-02-08 14:21:39 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class FormTemplateYamlValidator < ActiveModel::Validator
|
2023-08-29 17:41:33 -04:00
|
|
|
RESERVED_KEYWORDS = %w[title body category category_id tags]
|
|
|
|
ALLOWED_TYPES = %w[checkbox dropdown input multi-select textarea upload]
|
2023-10-04 16:51:53 -04:00
|
|
|
HTML_SANITIZATION_OPTIONS = { elements: ["a"], attributes: { "a" => %w[href target] } }
|
2023-08-29 17:41:33 -04:00
|
|
|
|
2023-02-08 14:21:39 -05:00
|
|
|
def validate(record)
|
|
|
|
begin
|
|
|
|
yaml = Psych.safe_load(record.template)
|
2023-10-04 16:51:53 -04:00
|
|
|
|
|
|
|
unless yaml.is_a?(Array)
|
|
|
|
record.errors.add(:template, I18n.t("form_templates.errors.invalid_yaml"))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
existing_ids = []
|
|
|
|
yaml.each do |field|
|
|
|
|
check_missing_fields(record, field)
|
|
|
|
check_allowed_types(record, field)
|
|
|
|
check_ids(record, field, existing_ids)
|
|
|
|
check_descriptions_html(record, field)
|
|
|
|
end
|
2023-02-08 14:21:39 -05:00
|
|
|
rescue Psych::SyntaxError
|
|
|
|
record.errors.add(:template, I18n.t("form_templates.errors.invalid_yaml"))
|
|
|
|
end
|
|
|
|
end
|
2023-03-01 14:07:13 -05:00
|
|
|
|
2023-10-04 16:51:53 -04:00
|
|
|
def check_allowed_types(record, field)
|
|
|
|
if !ALLOWED_TYPES.include?(field["type"])
|
|
|
|
record.errors.add(
|
|
|
|
:template,
|
|
|
|
I18n.t(
|
|
|
|
"form_templates.errors.invalid_type",
|
|
|
|
type: field["type"],
|
|
|
|
valid_types: ALLOWED_TYPES.join(", "),
|
|
|
|
),
|
|
|
|
)
|
2023-03-01 14:07:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-04 16:51:53 -04:00
|
|
|
def check_missing_fields(record, field)
|
|
|
|
if field["type"].blank?
|
|
|
|
record.errors.add(:template, I18n.t("form_templates.errors.missing_type"))
|
2023-03-01 14:07:13 -05:00
|
|
|
end
|
2023-10-04 16:51:53 -04:00
|
|
|
record.errors.add(:template, I18n.t("form_templates.errors.missing_id")) if field["id"].blank?
|
2023-03-01 14:07:13 -05:00
|
|
|
end
|
2023-08-29 17:41:33 -04:00
|
|
|
|
2023-10-04 16:51:53 -04:00
|
|
|
def check_descriptions_html(record, field)
|
|
|
|
description = field.dig("attributes", "description")
|
2023-08-29 17:41:33 -04:00
|
|
|
|
2023-10-04 16:51:53 -04:00
|
|
|
return if description.blank?
|
2023-08-29 17:41:33 -04:00
|
|
|
|
2023-10-04 16:51:53 -04:00
|
|
|
sanitized_html = Sanitize.fragment(description, HTML_SANITIZATION_OPTIONS)
|
|
|
|
|
|
|
|
is_safe_html = sanitized_html == Loofah.html5_fragment(description).to_s
|
|
|
|
|
|
|
|
unless is_safe_html
|
|
|
|
record.errors.add(:template, I18n.t("form_templates.errors.unsafe_description"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_ids(record, field, existing_ids)
|
|
|
|
if RESERVED_KEYWORDS.include?(field["id"])
|
|
|
|
record.errors.add(:template, I18n.t("form_templates.errors.reserved_id", id: field["id"]))
|
|
|
|
end
|
2023-08-29 17:41:33 -04:00
|
|
|
|
2023-10-04 16:51:53 -04:00
|
|
|
if existing_ids.include?(field["id"])
|
|
|
|
record.errors.add(:template, I18n.t("form_templates.errors.duplicate_ids"))
|
2023-08-29 17:41:33 -04:00
|
|
|
end
|
2023-10-04 16:51:53 -04:00
|
|
|
|
2024-05-27 06:27:13 -04:00
|
|
|
existing_ids << field["id"] if field["id"].present?
|
2023-08-29 17:41:33 -04:00
|
|
|
end
|
2023-02-08 14:21:39 -05:00
|
|
|
end
|