A platform for community discussion. Free, open, simple.
FIX: Validate each value in an array custom field separately (#24659)
Daniel Waterworth611acaa6bcc5
diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rbindex 0429e70f206..b5b38da71fd 100644--- a/app/models/concerns/has_custom_fields.rb+++ b/app/models/concerns/has_custom_fields.rb@@ -17,6 +17,10 @@ module HasCustomFieldsendend+ def array_type?+ Array === type+ end+def validate(obj, name, value)return if value.nil?@@ -29,7 +33,7 @@ module HasCustomFieldsenddef serialize(value)- if value.is_a?(Hash) || type == :json || (Array === type && type[0] == :json)+ if value.is_a?(Hash) || type == :json || (array_type? && type[0] == :json)value.to_jsonelsif TrueClass === value"t"@@ -45,7 +49,7 @@ module HasCustomFieldsarray = nil- if Array === type+ if array_type?type = type[0]array = true if return_arrayend@@ -110,14 +114,15 @@ module HasCustomFieldsdef register_custom_field_type(name, type, max_length: nil)max_length ||= DEFAULT_FIELD_DESCRIPTOR.max_length- if Array === type+ descriptor = FieldDescriptor.new(type, max_length)+ custom_field_meta_data[name] = descriptor++ if descriptor.array_type?Discourse.deprecate("Array types for custom fields are deprecated, use type :json instead",drop_from: "3.3.0",)end-- custom_field_meta_data[name] = FieldDescriptor.new(type, max_length)enddef get_custom_field_descriptor(name)@@ -273,7 +278,7 @@ module HasCustomFieldsdescriptor = self.class.get_custom_field_descriptor(key)field_type = descriptor.type- if Array === field_type || (field_type != :json && Array === value)+ if descriptor.array_type? || (field_type != :json && Array === value)value = Array(value || [])value.compact!sub_type = field_type[0]@@ -344,7 +349,13 @@ module HasCustomFieldsdef custom_fields_value_lengthcustom_fields.each do |name, value|- self.class.get_custom_field_descriptor(name).validate(self, name, value)+ descriptor = self.class.get_custom_field_descriptor(name)++ if descriptor.array_type?+ Array(value).each { |v| descriptor.validate(self, name, v) }+ else+ descriptor.validate(self, name, value)+ endendendend