From 61251cfcb842cc468aca7a581f54be4fd28c2721 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Sun, 30 Nov 2025 18:38:46 +0000 Subject: [PATCH] fix: Adjust label predicates (#11318) * fix: labels in 14 * test: add variable for at limit * docs(jsonOptionValidator): deprecate * fix: export `selectMenuStringOptionPredicate` * fix: export everything Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com> --------- Co-authored-by: Qjuh <76154676+Qjuh@users.noreply.github.com> --- .../__tests__/components/selectMenu.test.ts | 14 +++++++++----- packages/builders/src/components/Assertions.ts | 3 +++ .../src/components/selectMenu/Assertions.ts | 3 +-- .../src/components/selectMenu/StringSelectMenu.ts | 7 ++++--- packages/builders/src/index.ts | 1 + 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/builders/__tests__/components/selectMenu.test.ts b/packages/builders/__tests__/components/selectMenu.test.ts index 6e0c887dc..3c4d1997d 100644 --- a/packages/builders/__tests__/components/selectMenu.test.ts +++ b/packages/builders/__tests__/components/selectMenu.test.ts @@ -6,6 +6,10 @@ const selectMenu = () => new StringSelectMenuBuilder(); const selectMenuOption = () => new StringSelectMenuOptionBuilder(); const longStr = 'a'.repeat(256); +const selectMenuOptionLabelAtLimit = 'a'.repeat(100); +const selectMenuOptionLabelAboveLimit = 'a'.repeat(101); +const selectMenuOptionValueAboveLimit = 'a'.repeat(101); +const selectMenuOptionDescriptionAboveLimit = 'a'.repeat(101); const selectMenuOptionData: APISelectMenuOption = { label: 'test', @@ -53,12 +57,12 @@ describe('Select Menu Components', () => { expect(() => selectMenu().setDisabled()).not.toThrowError(); expect(() => selectMenu().setPlaceholder('description')).not.toThrowError(); const option = selectMenuOption() - .setLabel('test') + .setLabel(selectMenuOptionLabelAtLimit) .setValue('test') .setDefault(true) .setEmoji({ name: 'test' }) .setDescription('description'); - expect(() => selectMenu().addOptions(option)).not.toThrowError(); + expect(() => selectMenu().setCustomId('customId').addOptions(option).toJSON()).not.toThrowError(); expect(() => selectMenu().setOptions(option)).not.toThrowError(); expect(() => selectMenu().setOptions({ label: 'test', value: 'test' })).not.toThrowError(); expect(() => selectMenu().addOptions([option])).not.toThrowError(); @@ -156,13 +160,13 @@ describe('Select Menu Components', () => { expect(() => { selectMenuOption() - .setLabel(longStr) - .setValue(longStr) + .setLabel(selectMenuOptionLabelAboveLimit) + .setValue(selectMenuOptionValueAboveLimit) // @ts-expect-error: Invalid default value .setDefault(-1) // @ts-expect-error: Invalid emoji .setEmoji({ name: 1 }) - .setDescription(longStr); + .setDescription(selectMenuOptionDescriptionAboveLimit); }).toThrowError(); }); diff --git a/packages/builders/src/components/Assertions.ts b/packages/builders/src/components/Assertions.ts index 7165a5dd6..c7fb82883 100644 --- a/packages/builders/src/components/Assertions.ts +++ b/packages/builders/src/components/Assertions.ts @@ -50,6 +50,9 @@ export const labelValueDescriptionValidator = s .lengthLessThanOrEqual(100) .setValidationEnabled(isValidationEnabled); +/** + * @deprecated Replaced with selectMenuStringOptionPredicate. + */ export const jsonOptionValidator = s .object({ label: labelValueDescriptionValidator, diff --git a/packages/builders/src/components/selectMenu/Assertions.ts b/packages/builders/src/components/selectMenu/Assertions.ts index 8aa0c0f1d..e26b671fe 100644 --- a/packages/builders/src/components/selectMenu/Assertions.ts +++ b/packages/builders/src/components/selectMenu/Assertions.ts @@ -2,7 +2,6 @@ import { Result, s } from '@sapphire/shapeshift'; import { ChannelType, ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10'; import { isValidationEnabled } from '../../util/validation.js'; import { customIdValidator, emojiValidator, idValidator } from '../Assertions.js'; -import { labelValidator } from '../textInput/Assertions.js'; const selectMenuBasePredicate = s.object({ id: idValidator.optional(), @@ -63,7 +62,7 @@ export const selectMenuUserPredicate = selectMenuBasePredicate export const selectMenuStringOptionPredicate = s .object({ - label: labelValidator, + label: s.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(100), value: s.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(100), description: s.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(100).optional(), emoji: emojiValidator.optional(), diff --git a/packages/builders/src/components/selectMenu/StringSelectMenu.ts b/packages/builders/src/components/selectMenu/StringSelectMenu.ts index 9c6542387..f3883e3d4 100644 --- a/packages/builders/src/components/selectMenu/StringSelectMenu.ts +++ b/packages/builders/src/components/selectMenu/StringSelectMenu.ts @@ -1,7 +1,8 @@ import { ComponentType } from 'discord-api-types/v10'; import type { APIStringSelectComponent, APISelectMenuOption } from 'discord-api-types/v10'; import { normalizeArray, type RestOrArray } from '../../util/normalizeArray.js'; -import { jsonOptionValidator, optionsLengthValidator, validateRequiredSelectMenuParameters } from '../Assertions.js'; +import { optionsLengthValidator, validateRequiredSelectMenuParameters } from '../Assertions.js'; +import { selectMenuStringOptionPredicate } from './Assertions.js'; import { BaseSelectMenuBuilder } from './BaseSelectMenu.js'; import { StringSelectMenuOptionBuilder } from './StringSelectMenuOption.js'; @@ -63,7 +64,7 @@ export class StringSelectMenuBuilder extends BaseSelectMenuBuilder normalizedOption instanceof StringSelectMenuOptionBuilder ? normalizedOption - : new StringSelectMenuOptionBuilder(jsonOptionValidator.parse(normalizedOption)), + : new StringSelectMenuOptionBuilder(selectMenuStringOptionPredicate.parse(normalizedOption)), ), ); return this; @@ -120,7 +121,7 @@ export class StringSelectMenuBuilder extends BaseSelectMenuBuilder normalizedOption instanceof StringSelectMenuOptionBuilder ? normalizedOption - : new StringSelectMenuOptionBuilder(jsonOptionValidator.parse(normalizedOption)), + : new StringSelectMenuOptionBuilder(selectMenuStringOptionPredicate.parse(normalizedOption)), ), ); diff --git a/packages/builders/src/index.ts b/packages/builders/src/index.ts index 3fe81285e..77f8fc792 100644 --- a/packages/builders/src/index.ts +++ b/packages/builders/src/index.ts @@ -31,6 +31,7 @@ export { */ StringSelectMenuOptionBuilder as SelectMenuOptionBuilder, } from './components/selectMenu/StringSelectMenuOption.js'; +export * as SelectMenuAssertions from './components/selectMenu/Assertions.js'; export * from './components/selectMenu/StringSelectMenuOption.js'; export * from './components/selectMenu/UserSelectMenu.js';