discourse/lib/theme_settings_object_validator.rb
Alan Guo Xiang Tan 64b4e0d08d
DEV: First pass of ThemeSettingsObjectValidator (#25624)
Why this change?

This is a first pass at adding an objects validator which main's job is
to validate an object against a defined schema which we will support. In
this pass, we are simply validating that properties that has been marked
as required are present in the object.
2024-02-16 09:35:16 +08:00

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