Fix: added validation for custom and additional attribute (#4260)
This commit is contained in:
@@ -50,6 +50,8 @@ class Conversation < ApplicationRecord
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
before_validation :validate_additional_attributes
|
||||
validates :additional_attributes, jsonb_attributes_length: true
|
||||
validates :custom_attributes, jsonb_attributes_length: true
|
||||
|
||||
enum status: { open: 0, resolved: 1, pending: 2, snoozed: 3 }
|
||||
|
||||
|
||||
21
app/models/jsonb_attributes_length_validator.rb
Normal file
21
app/models/jsonb_attributes_length_validator.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class JsonbAttributesLengthValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
return if value.empty?
|
||||
|
||||
@attribute = attribute
|
||||
@record = record
|
||||
|
||||
value.each do |key, attribute_value|
|
||||
validate_keys(key, attribute_value)
|
||||
end
|
||||
end
|
||||
|
||||
def validate_keys(key, attribute_value)
|
||||
case attribute_value.class.name
|
||||
when 'String'
|
||||
@record.errors.add @attribute, "#{key} length should be < 1500" if attribute_value.length > 1500
|
||||
when 'Integer'
|
||||
@record.errors.add @attribute, "#{key} value should be < 9999999999" if attribute_value > 9_999_999_999
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user