Skip to content

A platform for community discussion. Free, open, simple.

DEV: Correctly validate multiple uploads in object site setting (#36639)

Isaac Janzen4223288017db
diff --git a/lib/schema_settings_object_validator.rb b/lib/schema_settings_object_validator.rb
index 92ce110f043..fbbc4f0a061 100644
--- a/lib/schema_settings_object_validator.rb
+++ b/lib/schema_settings_object_validator.rb
@@ -80,6 +80,13 @@ class SchemaSettingsObjectValidator
end
def validate
+ # Convert all upload URLs to IDs before validation
+ @properties.each do |property_name, property_attributes|
+ next if property_attributes[:type] != "upload"
+ normalize_upload_value(property_name, property_attributes)
+ end
+
+ # Validate all properties
@properties.each do |property_name, property_attributes|
if property_attributes[:type] == "objects"
validate_child_objects(
@@ -101,6 +108,15 @@ class SchemaSettingsObjectValidator
private
+ def normalize_upload_value(property_name, property_attributes)
+ value = @object[property_name]
+ return unless value.is_a?(String) && value.present?
+
+ if upload = Upload.get_from_url(value)
+ @object[property_name] = upload.id
+ end
+ end
+
def validate_child_objects(objects, property_name:, schema:)
return if objects.blank?
@@ -137,18 +153,8 @@ class SchemaSettingsObjectValidator
when "integer", "topic", "post"
value.is_a?(Integer)
when "upload"
- # Convert upload URLs to IDs like core does
- if value.is_a?(String)
- upload = Upload.get_from_url(value)
- if upload
- @object[property_name] = upload.id
- true
- else
- false
- end
- else
- value.is_a?(Integer)
- end
+ # Upload URLs are converted to IDs in normalize_upload_value
+ value.is_a?(Integer)
when "float"
value.is_a?(Float) || value.is_a?(Integer)
when "boolean"
diff --git a/spec/lib/schema_settings_object_validator_spec.rb b/spec/lib/schema_settings_object_validator_spec.rb
index 48d7d7b86c6..9e6e7cbc806 100644
--- a/spec/lib/schema_settings_object_validator_spec.rb
+++ b/spec/lib/schema_settings_object_validator_spec.rb
@@ -800,6 +800,30 @@ RSpec.describe SchemaSettingsObjectValidator do
# only 1 SQL query should be executed to check if upload ids are valid
expect(queries.length).to eq(1)
end
+
+ it "should convert multiple upload URLs to IDs and validate successfully" do
+ upload1 = Fabricate(:upload)
+ upload2 = Fabricate(:upload)
+ schema = {
+ name: "section",
+ properties: {
+ upload_property_1: {
+ type: "upload",
+ },
+ upload_property_2: {
+ type: "upload",
+ },
+ },
+ }
+
+ object = { upload_property_1: upload1.url, upload_property_2: upload2.url }
+ validator = described_class.new(schema: schema, object: object)
+ errors = validator.validate
+
+ expect(errors).to eq({})
+ expect(validator.instance_variable_get(:@object)[:upload_property_1]).to eq(upload1.id)
+ expect(validator.instance_variable_get(:@object)[:upload_property_2]).to eq(upload2.id)
+ end
end
context "for tag properties" do