mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
fix: Add idPredicate (#11109)
* fix: `idPredicate` * fix: add test * test: add negative test
This commit is contained in:
@@ -32,4 +32,11 @@ describe('Separator', () => {
|
||||
expect(separator.toJSON()).toEqual({ type: ComponentType.Separator });
|
||||
});
|
||||
});
|
||||
|
||||
describe('Invalid id', () => {
|
||||
test('GIVEN a separator with a set spacing and an invalid set id THEN throws error', () => {
|
||||
const separator = new SeparatorBuilder().setSpacing(SeparatorSpacingSize.Large).setId(-1);
|
||||
expect(() => separator.toJSON()).toThrowError();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,11 @@ describe('TextDisplay', () => {
|
||||
expect(textDisplay.toJSON()).toEqual({ type: ComponentType.TextDisplay, content: 'foo' });
|
||||
});
|
||||
|
||||
test('GIVEN a text display with a set content with an invalid id THEN throws error', () => {
|
||||
const textDisplay = new TextDisplayBuilder().setContent('foo').setId(5.5);
|
||||
expect(() => textDisplay.toJSON()).toThrowError();
|
||||
});
|
||||
|
||||
test('GIVEN a text display with a pre-defined content THEN overwritten content THEN return valid toJSON data', () => {
|
||||
const textDisplay = new TextDisplayBuilder({ content: 'foo' });
|
||||
textDisplay.setContent('bar');
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Locale } from 'discord-api-types/v10';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const idPredicate = z.int().min(0).max(2_147_483_647).optional();
|
||||
export const customIdPredicate = z.string().min(1).max(100);
|
||||
|
||||
export const memberPermissionsPredicate = z.coerce.bigint();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ButtonStyle, ChannelType, ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10';
|
||||
import { z } from 'zod';
|
||||
import { customIdPredicate } from '../Assertions.js';
|
||||
import { idPredicate, customIdPredicate } from '../Assertions.js';
|
||||
|
||||
const labelPredicate = z.string().min(1).max(80);
|
||||
|
||||
@@ -52,6 +52,7 @@ export const buttonPredicate = z.discriminatedUnion('style', [
|
||||
]);
|
||||
|
||||
const selectMenuBasePredicate = z.object({
|
||||
id: idPredicate,
|
||||
placeholder: z.string().max(150).optional(),
|
||||
min_values: z.number().min(0).max(25).optional(),
|
||||
max_values: z.number().min(0).max(25).optional(),
|
||||
@@ -135,6 +136,7 @@ export const selectMenuUserPredicate = selectMenuBasePredicate.extend({
|
||||
});
|
||||
|
||||
export const actionRowPredicate = z.object({
|
||||
id: idPredicate,
|
||||
type: z.literal(ComponentType.ActionRow),
|
||||
components: z.union([
|
||||
z
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ComponentType } from 'discord-api-types/v10';
|
||||
import { z } from 'zod';
|
||||
import { idPredicate } from '../../Assertions';
|
||||
import {
|
||||
selectMenuChannelPredicate,
|
||||
selectMenuMentionablePredicate,
|
||||
@@ -10,6 +11,7 @@ import {
|
||||
import { textInputPredicate } from '../textInput/Assertions';
|
||||
|
||||
export const labelPredicate = z.object({
|
||||
id: idPredicate,
|
||||
type: z.literal(ComponentType.Label),
|
||||
label: z.string().min(1).max(45),
|
||||
description: z.string().min(1).max(100).optional(),
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { ComponentType, TextInputStyle } from 'discord-api-types/v10';
|
||||
import { z } from 'zod';
|
||||
import { customIdPredicate } from '../../Assertions.js';
|
||||
import { customIdPredicate, idPredicate } from '../../Assertions.js';
|
||||
|
||||
export const textInputPredicate = z.object({
|
||||
id: idPredicate,
|
||||
type: z.literal(ComponentType.TextInput),
|
||||
custom_id: customIdPredicate,
|
||||
style: z.enum(TextInputStyle),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ComponentType, SeparatorSpacingSize } from 'discord-api-types/v10';
|
||||
import { z } from 'zod';
|
||||
import { idPredicate } from '../../Assertions.js';
|
||||
import { actionRowPredicate } from '../Assertions.js';
|
||||
|
||||
const unfurledMediaItemPredicate = z.object({
|
||||
@@ -8,6 +9,7 @@ const unfurledMediaItemPredicate = z.object({
|
||||
|
||||
export const thumbnailPredicate = z.object({
|
||||
type: z.literal(ComponentType.Thumbnail),
|
||||
id: idPredicate,
|
||||
media: unfurledMediaItemPredicate,
|
||||
description: z.string().min(1).max(1_024).nullish(),
|
||||
spoiler: z.boolean().optional(),
|
||||
@@ -19,22 +21,26 @@ const unfurledMediaItemAttachmentOnlyPredicate = z.object({
|
||||
|
||||
export const filePredicate = z.object({
|
||||
type: z.literal(ComponentType.File),
|
||||
id: idPredicate,
|
||||
file: unfurledMediaItemAttachmentOnlyPredicate,
|
||||
spoiler: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export const separatorPredicate = z.object({
|
||||
type: z.literal(ComponentType.Separator),
|
||||
id: idPredicate,
|
||||
divider: z.boolean().optional(),
|
||||
spacing: z.enum(SeparatorSpacingSize).optional(),
|
||||
});
|
||||
|
||||
export const textDisplayPredicate = z.object({
|
||||
type: z.literal(ComponentType.TextDisplay),
|
||||
id: idPredicate,
|
||||
content: z.string().min(1).max(4_000),
|
||||
});
|
||||
|
||||
export const mediaGalleryItemPredicate = z.object({
|
||||
id: idPredicate,
|
||||
media: unfurledMediaItemPredicate,
|
||||
description: z.string().min(1).max(1_024).nullish(),
|
||||
spoiler: z.boolean().optional(),
|
||||
@@ -42,11 +48,13 @@ export const mediaGalleryItemPredicate = z.object({
|
||||
|
||||
export const mediaGalleryPredicate = z.object({
|
||||
type: z.literal(ComponentType.MediaGallery),
|
||||
id: idPredicate,
|
||||
items: z.array(mediaGalleryItemPredicate).min(1).max(10),
|
||||
});
|
||||
|
||||
export const sectionPredicate = z.object({
|
||||
type: z.literal(ComponentType.Section),
|
||||
id: idPredicate,
|
||||
components: z.array(textDisplayPredicate).min(1).max(3),
|
||||
accessory: z.union([
|
||||
z.object({ type: z.literal(ComponentType.Button) }),
|
||||
@@ -56,6 +64,7 @@ export const sectionPredicate = z.object({
|
||||
|
||||
export const containerPredicate = z.object({
|
||||
type: z.literal(ComponentType.Container),
|
||||
id: idPredicate,
|
||||
components: z
|
||||
.array(
|
||||
z.union([
|
||||
|
||||
Reference in New Issue
Block a user