Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/builders/__tests__/components/selectMenu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});

Expand Down
3 changes: 3 additions & 0 deletions packages/builders/src/components/Assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export const labelValueDescriptionValidator = s
.lengthLessThanOrEqual(100)
.setValidationEnabled(isValidationEnabled);

/**
* @deprecated Replaced with selectMenuStringOptionPredicate.
*/
export const jsonOptionValidator = s
.object({
label: labelValueDescriptionValidator,
Expand Down
3 changes: 1 addition & 2 deletions packages/builders/src/components/selectMenu/Assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -63,7 +64,7 @@ export class StringSelectMenuBuilder extends BaseSelectMenuBuilder<APIStringSele
...normalizedOptions.map((normalizedOption) =>
normalizedOption instanceof StringSelectMenuOptionBuilder
? normalizedOption
: new StringSelectMenuOptionBuilder(jsonOptionValidator.parse(normalizedOption)),
: new StringSelectMenuOptionBuilder(selectMenuStringOptionPredicate.parse(normalizedOption)),
),
);
return this;
Expand Down Expand Up @@ -120,7 +121,7 @@ export class StringSelectMenuBuilder extends BaseSelectMenuBuilder<APIStringSele
...normalizedOptions.map((normalizedOption) =>
normalizedOption instanceof StringSelectMenuOptionBuilder
? normalizedOption
: new StringSelectMenuOptionBuilder(jsonOptionValidator.parse(normalizedOption)),
: new StringSelectMenuOptionBuilder(selectMenuStringOptionPredicate.parse(normalizedOption)),
),
);

Expand Down
1 change: 1 addition & 0 deletions packages/builders/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down