Skip to content

Commit c5a6e5e

Browse files
committed
fix(builders): add proper snowflake validation
close #11289
1 parent 0b12263 commit c5a6e5e

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

packages/builders/src/Assertions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { z } from 'zod';
33

44
export const idPredicate = z.int().min(0).max(2_147_483_647).optional();
55
export const customIdPredicate = z.string().min(1).max(100);
6+
export const snowflakePredicate = z.string().regex(/^\d{17,20}$/);
67

78
export const memberPermissionsPredicate = z.coerce.bigint();
89

packages/builders/src/components/Assertions.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ButtonStyle, ChannelType, ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10';
22
import { z } from 'zod';
3-
import { idPredicate, customIdPredicate } from '../Assertions.js';
3+
import { idPredicate, customIdPredicate, snowflakePredicate } from '../Assertions.js';
44

55
const labelPredicate = z.string().min(1).max(80);
66

77
export const emojiPredicate = z
88
.strictObject({
9-
id: z.string().optional(),
9+
id: snowflakePredicate.optional(),
1010
name: z.string().min(2).max(32).optional(),
1111
animated: z.boolean().optional(),
1212
})
@@ -39,7 +39,7 @@ const buttonLinkPredicate = buttonPredicateBase.extend({
3939

4040
const buttonPremiumPredicate = buttonPredicateBase.extend({
4141
style: z.literal(ButtonStyle.Premium),
42-
sku_id: z.string(),
42+
sku_id: snowflakePredicate,
4343
});
4444

4545
export const buttonPredicate = z.discriminatedUnion('style', [
@@ -64,7 +64,7 @@ export const selectMenuChannelPredicate = selectMenuBasePredicate.extend({
6464
type: z.literal(ComponentType.ChannelSelect),
6565
channel_types: z.enum(ChannelType).array().optional(),
6666
default_values: z
67-
.object({ id: z.string(), type: z.literal(SelectMenuDefaultValueType.Channel) })
67+
.object({ id: snowflakePredicate, type: z.literal(SelectMenuDefaultValueType.Channel) })
6868
.array()
6969
.max(25)
7070
.optional(),
@@ -74,7 +74,7 @@ export const selectMenuMentionablePredicate = selectMenuBasePredicate.extend({
7474
type: z.literal(ComponentType.MentionableSelect),
7575
default_values: z
7676
.object({
77-
id: z.string(),
77+
id: snowflakePredicate,
7878
type: z.literal([SelectMenuDefaultValueType.Role, SelectMenuDefaultValueType.User]),
7979
})
8080
.array()
@@ -85,7 +85,7 @@ export const selectMenuMentionablePredicate = selectMenuBasePredicate.extend({
8585
export const selectMenuRolePredicate = selectMenuBasePredicate.extend({
8686
type: z.literal(ComponentType.RoleSelect),
8787
default_values: z
88-
.object({ id: z.string(), type: z.literal(SelectMenuDefaultValueType.Role) })
88+
.object({ id: snowflakePredicate, type: z.literal(SelectMenuDefaultValueType.Role) })
8989
.array()
9090
.max(25)
9191
.optional(),
@@ -142,7 +142,7 @@ export const selectMenuStringPredicate = selectMenuBasePredicate
142142
export const selectMenuUserPredicate = selectMenuBasePredicate.extend({
143143
type: z.literal(ComponentType.UserSelect),
144144
default_values: z
145-
.object({ id: z.string(), type: z.literal(SelectMenuDefaultValueType.User) })
145+
.object({ id: snowflakePredicate, type: z.literal(SelectMenuDefaultValueType.User) })
146146
.array()
147147
.max(25)
148148
.optional(),

0 commit comments

Comments
 (0)