mirror of
https://github.com/discourse/discourse.git
synced 2025-02-06 03:18:23 +00:00
40 lines
998 B
Ruby
40 lines
998 B
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
class ThemeSettingsObjectValidator
|
||
|
def initialize(schema:, object:)
|
||
|
@object = object
|
||
|
@schema_name = schema[:name]
|
||
|
@properties = schema[:properties]
|
||
|
@errors = {}
|
||
|
end
|
||
|
|
||
|
def validate
|
||
|
validate_required_properties
|
||
|
|
||
|
@properties.each do |property_name, property_attributes|
|
||
|
if property_attributes[:type] == "objects"
|
||
|
@object[property_name]&.each do |child_object|
|
||
|
@errors[property_name] ||= []
|
||
|
|
||
|
@errors[property_name].push(
|
||
|
self.class.new(schema: property_attributes[:schema], object: child_object).validate,
|
||
|
)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
@errors
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def validate_required_properties
|
||
|
@properties.each do |property_name, property_attributes|
|
||
|
if property_attributes[:required] && @object[property_name].nil?
|
||
|
@errors[property_name] ||= []
|
||
|
@errors[property_name] << I18n.t("themes.settings_errors.objects.required")
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|