feat: Enforce limit of URLs for link buttons (#11011)

feat: enforce limit of URLs for link buttons
This commit is contained in:
Jiralite
2025-07-23 15:20:56 +01:00
committed by GitHub
parent 90b3692941
commit f2fec9177f
2 changed files with 9 additions and 2 deletions

View File

@@ -30,7 +30,7 @@ describe('Button Components', () => {
button.toJSON(); button.toJSON();
}).not.toThrowError(); }).not.toThrowError();
expect(() => new LinkButtonBuilder().setURL('https://google.com')).not.toThrowError(); expect(() => new LinkButtonBuilder().setLabel('label').setURL('https://google.com').toJSON()).not.toThrowError();
}); });
test('GIVEN invalid fields THEN build does throw', () => { test('GIVEN invalid fields THEN build does throw', () => {
@@ -61,6 +61,13 @@ describe('Button Components', () => {
expect(() => new PrimaryButtonBuilder().setCustomId('hi').setDisabled(0).toJSON()).toThrowError(); expect(() => new PrimaryButtonBuilder().setCustomId('hi').setDisabled(0).toJSON()).toThrowError();
// @ts-expect-error: Invalid emoji // @ts-expect-error: Invalid emoji
expect(() => new PrimaryButtonBuilder().setCustomId('hi').setEmoji('foo').toJSON()).toThrowError(); expect(() => new PrimaryButtonBuilder().setCustomId('hi').setEmoji('foo').toJSON()).toThrowError();
expect(() =>
new LinkButtonBuilder()
.setLabel('label')
.setURL(`https://google.com/${'a'.repeat(512)}`)
.toJSON(),
).toThrowError();
}); });
test('GiVEN valid input THEN valid JSON outputs are given', () => { test('GiVEN valid input THEN valid JSON outputs are given', () => {

View File

@@ -32,7 +32,7 @@ const buttonDangerPredicate = buttonCustomIdPredicateBase.extend({ style: z.lite
const buttonLinkPredicate = buttonPredicateBase.extend({ const buttonLinkPredicate = buttonPredicateBase.extend({
style: z.literal(ButtonStyle.Link), style: z.literal(ButtonStyle.Link),
url: z.url({ protocol: /^(?:https?|discord)$/ }), url: z.url({ protocol: /^(?:https?|discord)$/ }).max(512),
emoji: emojiPredicate.optional(), emoji: emojiPredicate.optional(),
label: labelPredicate, label: labelPredicate,
}); });