2023-02-08 14:21:39 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class FormTemplateYamlValidator < ActiveModel::Validator
|
|
|
|
def validate(record)
|
|
|
|
begin
|
|
|
|
yaml = Psych.safe_load(record.template)
|
2023-03-01 14:07:13 -05:00
|
|
|
check_missing_type(record, yaml)
|
|
|
|
check_allowed_types(record, yaml)
|
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
|
|
|
|
|
|
|
def check_allowed_types(record, yaml)
|
|
|
|
allowed_types = %w[checkbox dropdown input multi-select textarea upload]
|
|
|
|
yaml.each do |field|
|
|
|
|
if !allowed_types.include?(field["type"])
|
|
|
|
return(
|
|
|
|
record.errors.add(
|
|
|
|
:template,
|
|
|
|
I18n.t(
|
|
|
|
"form_templates.errors.invalid_type",
|
|
|
|
type: field["type"],
|
|
|
|
valid_types: allowed_types.join(", "),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_missing_type(record, yaml)
|
|
|
|
yaml.each do |field|
|
|
|
|
if field["type"].blank?
|
|
|
|
return record.errors.add(:template, I18n.t("form_templates.errors.missing_type"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-02-08 14:21:39 -05:00
|
|
|
end
|