mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 07:02:46 +08:00
64b4e0d08d
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.
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
|