Extract constants for column size length validation limits (#30045)

This commit is contained in:
Matt Jankowski 2024-04-24 04:56:28 -04:00 committed by GitHub
parent ebcf9840f4
commit f4a53f3fb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 21 additions and 7 deletions

View File

@ -13,10 +13,12 @@
# #
class AccountModerationNote < ApplicationRecord class AccountModerationNote < ApplicationRecord
CONTENT_SIZE_LIMIT = 500
belongs_to :account belongs_to :account
belongs_to :target_account, class_name: 'Account' belongs_to :target_account, class_name: 'Account'
scope :latest, -> { reorder('created_at DESC') } scope :latest, -> { reorder('created_at DESC') }
validates :content, presence: true, length: { maximum: 500 } validates :content, presence: true, length: { maximum: CONTENT_SIZE_LIMIT }
end end

View File

@ -14,9 +14,11 @@
class AccountNote < ApplicationRecord class AccountNote < ApplicationRecord
include RelationshipCacheable include RelationshipCacheable
COMMENT_SIZE_LIMIT = 2_000
belongs_to :account belongs_to :account
belongs_to :target_account, class_name: 'Account' belongs_to :target_account, class_name: 'Account'
validates :account_id, uniqueness: { scope: :target_account_id } validates :account_id, uniqueness: { scope: :target_account_id }
validates :comment, length: { maximum: 2_000 } validates :comment, length: { maximum: COMMENT_SIZE_LIMIT }
end end

View File

@ -19,12 +19,14 @@
class Invite < ApplicationRecord class Invite < ApplicationRecord
include Expireable include Expireable
COMMENT_SIZE_LIMIT = 420
belongs_to :user, inverse_of: :invites belongs_to :user, inverse_of: :invites
has_many :users, inverse_of: :invite, dependent: nil has_many :users, inverse_of: :invite, dependent: nil
scope :available, -> { where(expires_at: nil).or(where('expires_at >= ?', Time.now.utc)) } scope :available, -> { where(expires_at: nil).or(where('expires_at >= ?', Time.now.utc)) }
validates :comment, length: { maximum: 420 } validates :comment, length: { maximum: COMMENT_SIZE_LIMIT }
before_validation :set_code before_validation :set_code

View File

@ -26,6 +26,8 @@ class Report < ApplicationRecord
include Paginable include Paginable
include RateLimitable include RateLimitable
COMMENT_SIZE_LIMIT = 1_000
rate_limit by: :account, family: :reports rate_limit by: :account, family: :reports
belongs_to :account belongs_to :account
@ -46,7 +48,7 @@ class Report < ApplicationRecord
# A report is considered local if the reporter is local # A report is considered local if the reporter is local
delegate :local?, to: :account delegate :local?, to: :account
validates :comment, length: { maximum: 1_000 }, if: :local? validates :comment, length: { maximum: COMMENT_SIZE_LIMIT }, if: :local?
validates :rule_ids, absence: true, if: -> { (category_changed? || rule_ids_changed?) && !violation? } validates :rule_ids, absence: true, if: -> { (category_changed? || rule_ids_changed?) && !violation? }
validate :validate_rule_ids, if: -> { (category_changed? || rule_ids_changed?) && violation? } validate :validate_rule_ids, if: -> { (category_changed? || rule_ids_changed?) && violation? }

View File

@ -13,10 +13,12 @@
# #
class ReportNote < ApplicationRecord class ReportNote < ApplicationRecord
CONTENT_SIZE_LIMIT = 500
belongs_to :account belongs_to :account
belongs_to :report, inverse_of: :notes, touch: true belongs_to :report, inverse_of: :notes, touch: true
scope :latest, -> { reorder(created_at: :desc) } scope :latest, -> { reorder(created_at: :desc) }
validates :content, presence: true, length: { maximum: 500 } validates :content, presence: true, length: { maximum: CONTENT_SIZE_LIMIT }
end end

View File

@ -15,9 +15,11 @@
class Rule < ApplicationRecord class Rule < ApplicationRecord
include Discard::Model include Discard::Model
TEXT_SIZE_LIMIT = 300
self.discard_column = :deleted_at self.discard_column = :deleted_at
validates :text, presence: true, length: { maximum: 300 } validates :text, presence: true, length: { maximum: TEXT_SIZE_LIMIT }
scope :ordered, -> { kept.order(priority: :asc, id: :asc) } scope :ordered, -> { kept.order(priority: :asc, id: :asc) }
end end

View File

@ -12,6 +12,8 @@
# #
class UserInviteRequest < ApplicationRecord class UserInviteRequest < ApplicationRecord
TEXT_SIZE_LIMIT = 420
belongs_to :user, inverse_of: :invite_request belongs_to :user, inverse_of: :invite_request
validates :text, presence: true, length: { maximum: 420 } validates :text, presence: true, length: { maximum: TEXT_SIZE_LIMIT }
end end