mirror of
https://github.com/discourse/discourse.git
synced 2024-11-30 08:34:39 +08:00
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:
parent
7f3edcbdc6
commit
7fbbefe363
|
@ -20,14 +20,7 @@ module HasCustomFields
|
||||||
def validate(obj, name, value)
|
def validate(obj, name, value)
|
||||||
return if value.nil?
|
return if value.nil?
|
||||||
|
|
||||||
size =
|
if serialize(value).bytesize > max_length
|
||||||
if Array === type || (type != :json && Array === value)
|
|
||||||
value.map { |v| serialize(v).bytesize }.max || 0
|
|
||||||
else
|
|
||||||
serialize(value).bytesize
|
|
||||||
end
|
|
||||||
|
|
||||||
if size > max_length
|
|
||||||
obj.errors.add(
|
obj.errors.add(
|
||||||
:base,
|
:base,
|
||||||
I18n.t("custom_fields.validations.max_value_length", max_value_length: max_length),
|
I18n.t("custom_fields.validations.max_value_length", max_value_length: max_length),
|
||||||
|
@ -281,7 +274,7 @@ module HasCustomFields
|
||||||
field_type = descriptor.type
|
field_type = descriptor.type
|
||||||
|
|
||||||
if Array === field_type || (field_type != :json && Array === value)
|
if Array === field_type || (field_type != :json && Array === value)
|
||||||
value = value || []
|
value = Array(value || [])
|
||||||
value.compact!
|
value.compact!
|
||||||
sub_type = field_type[0]
|
sub_type = field_type[0]
|
||||||
|
|
||||||
|
|
|
@ -134,6 +134,13 @@ RSpec.describe HasCustomFields do
|
||||||
expect(db_item.custom_fields).to eq("a" => "b", "c" => "d")
|
expect(db_item.custom_fields).to eq("a" => "b", "c" => "d")
|
||||||
end
|
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
|
it "handles arrays properly" do
|
||||||
CustomFieldsTestItem.register_custom_field_type "array", [:integer]
|
CustomFieldsTestItem.register_custom_field_type "array", [:integer]
|
||||||
test_item = CustomFieldsTestItem.new
|
test_item = CustomFieldsTestItem.new
|
||||||
|
|
Loading…
Reference in New Issue
Block a user