DEV: active record validations for maxlength on text columns (#23499)

Introduce max length on text columns for description and slug fields within chat.

At a later date we will probably want to convert these text columns to string/varchar through a migration, but for now this change introduces a limit within the active record model.
This commit is contained in:
David Battersby 2023-09-11 17:05:02 +08:00 committed by GitHub
parent b07445ced8
commit e771382c1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 1 deletions

View File

@ -36,6 +36,8 @@ module Chat
}, },
presence: true, presence: true,
allow_nil: true allow_nil: true
validates :description, length: { maximum: 500 }
validates :slug, length: { maximum: 100 }
validate :ensure_slug_ok, if: :slug_changed? validate :ensure_slug_ok, if: :slug_changed?
before_validation :generate_auto_slug before_validation :generate_auto_slug

View File

@ -7,6 +7,8 @@ import { extractError } from "discourse/lib/ajax-error";
import { tracked } from "@glimmer/tracking"; import { tracked } from "@glimmer/tracking";
import { inject as service } from "@ember/service"; import { inject as service } from "@ember/service";
const SLUG_MAX_LENGTH = 100;
export default class ChatModalEditChannelName extends Component { export default class ChatModalEditChannelName extends Component {
@service chatApi; @service chatApi;
@service siteSettings; @service siteSettings;
@ -26,7 +28,8 @@ export default class ChatModalEditChannelName extends Component {
return ( return (
(this.channel.title === this.editedName && (this.channel.title === this.editedName &&
this.channel.slug === this.editedSlug) || this.channel.slug === this.editedSlug) ||
this.editedName?.length > this.siteSettings.max_topic_title_length this.editedName?.length > this.siteSettings.max_topic_title_length ||
this.editedSlug?.length > SLUG_MAX_LENGTH
); );
} }

View File

@ -7,6 +7,8 @@ RSpec.describe Chat::CategoryChannel do
it { is_expected.to delegate_method(:read_restricted?).to(:category) } it { is_expected.to delegate_method(:read_restricted?).to(:category) }
it { is_expected.to delegate_method(:url).to(:chatable).with_prefix } it { is_expected.to delegate_method(:url).to(:chatable).with_prefix }
it { is_expected.to validate_length_of(:description).is_at_most(500) }
it { is_expected.to validate_length_of(:slug).is_at_most(100) }
describe "#category_channel?" do describe "#category_channel?" do
it "always returns true" do it "always returns true" do

View File

@ -1,9 +1,14 @@
# frozen_string_literal: true # frozen_string_literal: true
RSpec.describe Chat::Channel do RSpec.describe Chat::Channel do
subject(:channel) { Fabricate(:chat_channel) }
fab!(:category_channel_1) { Fabricate(:category_channel) } fab!(:category_channel_1) { Fabricate(:category_channel) }
fab!(:dm_channel_1) { Fabricate(:direct_message_channel) } fab!(:dm_channel_1) { Fabricate(:direct_message_channel) }
it { is_expected.to validate_length_of(:description).is_at_most(500) }
it { is_expected.to validate_length_of(:slug).is_at_most(100) }
describe ".find_by_id_or_slug" do describe ".find_by_id_or_slug" do
subject(:find_channel) { described_class.find_by_id_or_slug(channel_id) } subject(:find_channel) { described_class.find_by_id_or_slug(channel_id) }

View File

@ -6,6 +6,7 @@ RSpec.describe Chat::DirectMessageChannel do
it_behaves_like "a chat channel model" it_behaves_like "a chat channel model"
it { is_expected.to delegate_method(:allowed_user_ids).to(:direct_message).as(:user_ids) } it { is_expected.to delegate_method(:allowed_user_ids).to(:direct_message).as(:user_ids) }
it { is_expected.to validate_length_of(:description).is_at_most(500) }
describe "#category_channel?" do describe "#category_channel?" do
it "always returns false" do it "always returns false" do