2024-02-16 09:35:16 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ThemeSettingsObjectValidator
|
2024-02-27 09:16:37 +08:00
|
|
|
class << self
|
|
|
|
def validate_objects(schema:, objects:)
|
|
|
|
error_messages = []
|
|
|
|
|
|
|
|
objects.each_with_index do |object, index|
|
|
|
|
humanize_error_messages(
|
|
|
|
self.new(schema: schema, object: object).validate,
|
|
|
|
index:,
|
|
|
|
error_messages:,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
error_messages
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def humanize_error_messages(errors, index:, error_messages:)
|
|
|
|
errors.each do |property_json_pointer, error_details|
|
|
|
|
error_messages.push(*error_details.humanize_messages("/#{index}#{property_json_pointer}"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-21 15:27:42 +08:00
|
|
|
class ThemeSettingsObjectErrors
|
|
|
|
def initialize
|
|
|
|
@errors = []
|
|
|
|
end
|
|
|
|
|
2024-02-27 09:16:37 +08:00
|
|
|
def add_error(error, i18n_opts = {})
|
|
|
|
@errors << ThemeSettingsObjectError.new(error, i18n_opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def humanize_messages(property_json_pointer)
|
|
|
|
@errors.map { |error| error.humanize_messages(property_json_pointer) }
|
2024-02-21 15:27:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def full_messages
|
|
|
|
@errors.map(&:error_message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class ThemeSettingsObjectError
|
2024-02-27 09:16:37 +08:00
|
|
|
def initialize(error, i18n_opts = {})
|
|
|
|
@error = error
|
2024-02-21 15:27:42 +08:00
|
|
|
@i18n_opts = i18n_opts
|
|
|
|
end
|
|
|
|
|
2024-02-27 09:16:37 +08:00
|
|
|
def humanize_messages(property_json_pointer)
|
|
|
|
I18n.t(
|
|
|
|
"themes.settings_errors.objects.humanize_#{@error}",
|
|
|
|
@i18n_opts.merge(property_json_pointer:),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-02-21 15:27:42 +08:00
|
|
|
def error_message
|
2024-02-27 09:16:37 +08:00
|
|
|
I18n.t("themes.settings_errors.objects.#{@error}", @i18n_opts)
|
2024-02-21 15:27:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-27 14:27:10 +08:00
|
|
|
def initialize(schema:, object:, json_pointer_prefix: "", errors: {}, valid_ids_lookup: {})
|
2024-02-28 10:33:22 +08:00
|
|
|
@object = object.with_indifferent_access
|
2024-02-16 09:35:16 +08:00
|
|
|
@schema_name = schema[:name]
|
|
|
|
@properties = schema[:properties]
|
2024-02-27 09:16:37 +08:00
|
|
|
@errors = errors
|
|
|
|
@json_pointer_prefix = json_pointer_prefix
|
2024-02-27 14:27:10 +08:00
|
|
|
@valid_ids_lookup = valid_ids_lookup
|
2024-02-16 09:35:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
|
|
|
@properties.each do |property_name, property_attributes|
|
|
|
|
if property_attributes[:type] == "objects"
|
2024-02-28 10:44:46 +08:00
|
|
|
validate_child_objects(
|
|
|
|
@object[property_name],
|
|
|
|
property_name:,
|
|
|
|
schema: property_attributes[:schema],
|
|
|
|
)
|
|
|
|
else
|
|
|
|
validate_property(property_name, property_attributes)
|
2024-02-16 09:35:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@errors
|
|
|
|
end
|
|
|
|
|
2024-03-27 10:54:30 +08:00
|
|
|
def property_values_of_type(type)
|
|
|
|
fetch_property_values_of_type(@properties, @object, type)
|
|
|
|
end
|
|
|
|
|
2024-02-16 09:35:16 +08:00
|
|
|
private
|
|
|
|
|
2024-02-28 10:44:46 +08:00
|
|
|
def validate_child_objects(objects, property_name:, schema:)
|
|
|
|
return if objects.blank?
|
|
|
|
|
|
|
|
objects.each_with_index do |object, index|
|
|
|
|
self
|
|
|
|
.class
|
|
|
|
.new(
|
|
|
|
schema:,
|
|
|
|
object:,
|
|
|
|
valid_ids_lookup:,
|
|
|
|
json_pointer_prefix: "#{@json_pointer_prefix}#{property_name}/#{index}/",
|
|
|
|
errors: @errors,
|
|
|
|
)
|
|
|
|
.validate
|
2024-02-19 13:19:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-28 10:44:46 +08:00
|
|
|
def validate_property(property_name, property_attributes)
|
|
|
|
return if property_attributes[:required] && !is_property_present?(property_name)
|
|
|
|
return if !has_valid_property_value_type?(property_attributes, property_name)
|
|
|
|
!has_valid_property_value?(property_attributes, property_name)
|
|
|
|
end
|
|
|
|
|
2024-02-20 09:17:27 +08:00
|
|
|
def has_valid_property_value_type?(property_attributes, property_name)
|
2024-02-19 13:19:35 +08:00
|
|
|
value = @object[property_name]
|
|
|
|
type = property_attributes[:type]
|
|
|
|
|
2024-04-09 11:26:24 +08:00
|
|
|
return true if value.nil?
|
2024-02-19 13:19:35 +08:00
|
|
|
|
|
|
|
is_value_valid =
|
|
|
|
case type
|
2024-03-22 15:32:00 +08:00
|
|
|
when "string"
|
2024-02-19 13:19:35 +08:00
|
|
|
value.is_a?(String)
|
2024-03-28 22:05:48 +08:00
|
|
|
when "integer", "topic", "post", "upload"
|
2024-02-19 13:19:35 +08:00
|
|
|
value.is_a?(Integer)
|
|
|
|
when "float"
|
|
|
|
value.is_a?(Float) || value.is_a?(Integer)
|
|
|
|
when "boolean"
|
|
|
|
[true, false].include?(value)
|
|
|
|
when "enum"
|
|
|
|
property_attributes[:choices].include?(value)
|
2024-03-28 22:05:48 +08:00
|
|
|
when "categories", "groups"
|
2024-03-27 10:54:30 +08:00
|
|
|
value.is_a?(Array) && value.all? { |id| id.is_a?(Integer) }
|
2024-03-22 15:32:00 +08:00
|
|
|
when "tags"
|
|
|
|
value.is_a?(Array) && value.all? { |tag| tag.is_a?(String) }
|
2024-02-19 13:19:35 +08:00
|
|
|
else
|
2024-02-21 15:27:42 +08:00
|
|
|
add_error(property_name, :invalid_type, type:)
|
2024-02-20 09:17:27 +08:00
|
|
|
return false
|
2024-02-16 09:35:16 +08:00
|
|
|
end
|
2024-02-19 13:19:35 +08:00
|
|
|
|
2024-02-20 09:17:27 +08:00
|
|
|
if is_value_valid
|
|
|
|
true
|
|
|
|
else
|
2024-02-21 15:27:42 +08:00
|
|
|
add_error(property_name, "not_valid_#{type}_value", property_attributes)
|
2024-02-20 09:17:27 +08:00
|
|
|
false
|
2024-02-16 09:35:16 +08:00
|
|
|
end
|
|
|
|
end
|
2024-02-19 13:19:35 +08:00
|
|
|
|
2024-02-20 09:17:27 +08:00
|
|
|
def has_valid_property_value?(property_attributes, property_name)
|
|
|
|
validations = property_attributes[:validations]
|
|
|
|
type = property_attributes[:type]
|
|
|
|
value = @object[property_name]
|
|
|
|
|
2024-03-09 19:25:30 +08:00
|
|
|
return true if value.nil?
|
|
|
|
|
2024-02-20 09:17:27 +08:00
|
|
|
case type
|
2024-03-28 22:05:48 +08:00
|
|
|
when "topic", "upload", "post"
|
2024-02-27 14:27:10 +08:00
|
|
|
if !valid_ids(type).include?(value)
|
|
|
|
add_error(property_name, :"not_valid_#{type}_value")
|
2024-02-21 08:11:15 +08:00
|
|
|
return false
|
|
|
|
end
|
2024-03-28 22:05:48 +08:00
|
|
|
when "tags", "categories", "groups"
|
2024-03-22 15:32:00 +08:00
|
|
|
if !Array(value).to_set.subset?(valid_ids(type))
|
|
|
|
add_error(property_name, :"not_valid_#{type}_value")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if (min = validations&.dig(:min)) && value.length < min
|
2024-04-04 21:02:09 +08:00
|
|
|
add_error(property_name, :"#{type}_value_not_valid_min", count: min)
|
2024-03-22 15:32:00 +08:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if (max = validations&.dig(:max)) && value.length > max
|
2024-04-04 21:02:09 +08:00
|
|
|
add_error(property_name, :"#{type}_value_not_valid_max", count: max)
|
2024-03-22 15:32:00 +08:00
|
|
|
return false
|
|
|
|
end
|
2024-02-20 09:17:27 +08:00
|
|
|
when "string"
|
2024-02-21 08:11:15 +08:00
|
|
|
if (min = validations&.dig(:min_length)) && value.length < min
|
2024-04-04 21:02:09 +08:00
|
|
|
add_error(property_name, :string_value_not_valid_min, count: min)
|
2024-02-20 09:17:27 +08:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2024-02-21 08:11:15 +08:00
|
|
|
if (max = validations&.dig(:max_length)) && value.length > max
|
2024-04-04 21:02:09 +08:00
|
|
|
add_error(property_name, :string_value_not_valid_max, count: max)
|
2024-02-20 09:17:27 +08:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2024-03-04 13:22:14 +08:00
|
|
|
if validations&.dig(:url) && !UrlHelper.relaxed_parse(value)
|
2024-02-21 15:27:42 +08:00
|
|
|
add_error(property_name, :string_value_not_valid_url)
|
2024-02-20 09:17:27 +08:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
when "integer", "float"
|
2024-02-21 08:11:15 +08:00
|
|
|
if (min = validations&.dig(:min)) && value < min
|
2024-02-21 15:27:42 +08:00
|
|
|
add_error(property_name, :number_value_not_valid_min, min:)
|
2024-02-20 09:17:27 +08:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2024-02-21 08:11:15 +08:00
|
|
|
if (max = validations&.dig(:max)) && value > max
|
2024-02-21 15:27:42 +08:00
|
|
|
add_error(property_name, :number_value_not_valid_max, max:)
|
2024-02-20 09:17:27 +08:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_property_present?(property_name)
|
2024-04-01 10:11:40 +08:00
|
|
|
if @object[property_name].blank?
|
2024-02-21 15:27:42 +08:00
|
|
|
add_error(property_name, :required)
|
2024-02-19 13:19:35 +08:00
|
|
|
false
|
2024-02-20 09:17:27 +08:00
|
|
|
else
|
|
|
|
true
|
2024-02-19 13:19:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-21 15:27:42 +08:00
|
|
|
def add_error(property_name, key, i18n_opts = {})
|
2024-02-27 09:16:37 +08:00
|
|
|
pointer = json_pointer(property_name)
|
|
|
|
@errors[pointer] ||= ThemeSettingsObjectErrors.new
|
|
|
|
@errors[pointer].add_error(key, i18n_opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def json_pointer(property_name)
|
|
|
|
"/#{@json_pointer_prefix}#{property_name}"
|
2024-02-19 13:19:35 +08:00
|
|
|
end
|
2024-02-21 08:11:15 +08:00
|
|
|
|
2024-02-27 14:27:10 +08:00
|
|
|
def valid_ids_lookup
|
|
|
|
@valid_ids_lookup ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
TYPE_TO_MODEL_MAP = {
|
2024-03-27 10:54:30 +08:00
|
|
|
"categories" => {
|
2024-03-22 11:05:16 +08:00
|
|
|
klass: Category,
|
|
|
|
},
|
|
|
|
"topic" => {
|
|
|
|
klass: Topic,
|
|
|
|
},
|
|
|
|
"post" => {
|
|
|
|
klass: Post,
|
|
|
|
},
|
2024-03-28 22:05:48 +08:00
|
|
|
"groups" => {
|
2024-03-22 11:05:16 +08:00
|
|
|
klass: Group,
|
|
|
|
},
|
|
|
|
"upload" => {
|
|
|
|
klass: Upload,
|
|
|
|
},
|
2024-03-22 15:32:00 +08:00
|
|
|
"tags" => {
|
2024-03-22 11:05:16 +08:00
|
|
|
klass: Tag,
|
|
|
|
column: :name,
|
|
|
|
},
|
2024-02-27 14:27:10 +08:00
|
|
|
}
|
|
|
|
private_constant :TYPE_TO_MODEL_MAP
|
|
|
|
|
|
|
|
def valid_ids(type)
|
2024-03-22 11:05:16 +08:00
|
|
|
valid_ids_lookup[type] ||= begin
|
|
|
|
column = TYPE_TO_MODEL_MAP[type][:column] || :id
|
|
|
|
|
|
|
|
Set.new(
|
|
|
|
TYPE_TO_MODEL_MAP[type][:klass].where(
|
|
|
|
column => fetch_property_values_of_type(@properties, @object, type),
|
|
|
|
).pluck(column),
|
|
|
|
)
|
|
|
|
end
|
2024-02-21 08:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_property_values_of_type(properties, object, type)
|
|
|
|
values = Set.new
|
|
|
|
|
|
|
|
properties.each do |property_name, property_attributes|
|
|
|
|
if property_attributes[:type] == type
|
2024-03-22 15:32:00 +08:00
|
|
|
values.merge(Array(object[property_name]))
|
2024-02-21 08:11:15 +08:00
|
|
|
elsif property_attributes[:type] == "objects"
|
|
|
|
object[property_name]&.each do |child_object|
|
|
|
|
values.merge(
|
|
|
|
fetch_property_values_of_type(
|
|
|
|
property_attributes[:schema][:properties],
|
|
|
|
child_object,
|
|
|
|
type,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
values
|
|
|
|
end
|
2024-02-16 09:35:16 +08:00
|
|
|
end
|