FIX: Allow setting an array custom field to a singleton value (#24636)

Also, validation happens per item in an array field.
This commit is contained in:
Daniel Waterworth 2023-11-29 14:18:47 -06:00
parent 7f3edcbdc6
commit 7fbbefe363
2 changed files with 9 additions and 9 deletions

View File

@ -20,14 +20,7 @@ module HasCustomFields
def validate(obj, name, value)
return if value.nil?
size =
if Array === type || (type != :json && Array === value)
value.map { |v| serialize(v).bytesize }.max || 0
else
serialize(value).bytesize
end
if size > max_length
if serialize(value).bytesize > max_length
obj.errors.add(
:base,
I18n.t("custom_fields.validations.max_value_length", max_value_length: max_length),
@ -281,7 +274,7 @@ module HasCustomFields
field_type = descriptor.type
if Array === field_type || (field_type != :json && Array === value)
value = value || []
value = Array(value || [])
value.compact!
sub_type = field_type[0]

View File

@ -134,6 +134,13 @@ RSpec.describe HasCustomFields do
expect(db_item.custom_fields).to eq("a" => "b", "c" => "d")
end
it "handles assigning singleton values to array fields" do
CustomFieldsTestItem.register_custom_field_type "array", [:integer]
test_item = CustomFieldsTestItem.new
test_item.custom_fields = { "array" => "1" }
test_item.save
end
it "handles arrays properly" do
CustomFieldsTestItem.register_custom_field_type "array", [:integer]
test_item = CustomFieldsTestItem.new