mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 06:29:30 +08:00
DEV: Refactor ThemeSettingsObjectValidator#validate
(#25904)
What does this change do? 1. Reduce an additional loop through all the properties 2. Extract the validation of child objects into a dedicate method
This commit is contained in:
parent
afb0adf48d
commit
54a1fea74e
|
@ -70,22 +70,15 @@ class ThemeSettingsObjectValidator
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate
|
def validate
|
||||||
validate_properties
|
|
||||||
|
|
||||||
@properties.each do |property_name, property_attributes|
|
@properties.each do |property_name, property_attributes|
|
||||||
if property_attributes[:type] == "objects"
|
if property_attributes[:type] == "objects"
|
||||||
@object[property_name]&.each_with_index do |child_object, index|
|
validate_child_objects(
|
||||||
self
|
@object[property_name],
|
||||||
.class
|
property_name:,
|
||||||
.new(
|
schema: property_attributes[:schema],
|
||||||
schema: property_attributes[:schema],
|
)
|
||||||
object: child_object,
|
else
|
||||||
json_pointer_prefix: "#{@json_pointer_prefix}#{property_name}/#{index}/",
|
validate_property(property_name, property_attributes)
|
||||||
valid_ids_lookup:,
|
|
||||||
errors: @errors,
|
|
||||||
)
|
|
||||||
.validate
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -94,15 +87,29 @@ class ThemeSettingsObjectValidator
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def validate_properties
|
def validate_child_objects(objects, property_name:, schema:)
|
||||||
@properties.each do |property_name, property_attributes|
|
return if objects.blank?
|
||||||
next if property_attributes[:type] == "objects"
|
|
||||||
next if property_attributes[:required] && !is_property_present?(property_name)
|
objects.each_with_index do |object, index|
|
||||||
next if !has_valid_property_value_type?(property_attributes, property_name)
|
self
|
||||||
next if !has_valid_property_value?(property_attributes, property_name)
|
.class
|
||||||
|
.new(
|
||||||
|
schema:,
|
||||||
|
object:,
|
||||||
|
valid_ids_lookup:,
|
||||||
|
json_pointer_prefix: "#{@json_pointer_prefix}#{property_name}/#{index}/",
|
||||||
|
errors: @errors,
|
||||||
|
)
|
||||||
|
.validate
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def has_valid_property_value_type?(property_attributes, property_name)
|
def has_valid_property_value_type?(property_attributes, property_name)
|
||||||
value = @object[property_name]
|
value = @object[property_name]
|
||||||
type = property_attributes[:type]
|
type = property_attributes[:type]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user