11import { ButtonStyle , ChannelType , ComponentType , SelectMenuDefaultValueType } from 'discord-api-types/v10' ;
2- import { z } from 'zod' ;
3- import { customIdPredicate , refineURLPredicate } from '../Assertions.js' ;
2+ import { z } from 'zod/v4 ' ;
3+ import { customIdPredicate } from '../Assertions.js' ;
44
55const labelPredicate = z . string ( ) . min ( 1 ) . max ( 80 ) ;
66
77export const emojiPredicate = z
8- . object ( {
8+ . strictObject ( {
99 id : z . string ( ) . optional ( ) ,
1010 name : z . string ( ) . min ( 2 ) . max ( 32 ) . optional ( ) ,
1111 animated : z . boolean ( ) . optional ( ) ,
1212 } )
13- . strict ( )
1413 . refine ( ( data ) => data . id !== undefined || data . name !== undefined , {
15- message : "Either 'id' or 'name' must be provided" ,
14+ error : "Either 'id' or 'name' must be provided" ,
1615 } ) ;
1716
18- const buttonPredicateBase = z . object ( {
17+ const buttonPredicateBase = z . strictObject ( {
1918 type : z . literal ( ComponentType . Button ) ,
2019 disabled : z . boolean ( ) . optional ( ) ,
2120} ) ;
@@ -26,31 +25,22 @@ const buttonCustomIdPredicateBase = buttonPredicateBase.extend({
2625 label : labelPredicate ,
2726} ) ;
2827
29- const buttonPrimaryPredicate = buttonCustomIdPredicateBase . extend ( { style : z . literal ( ButtonStyle . Primary ) } ) . strict ( ) ;
30- const buttonSecondaryPredicate = buttonCustomIdPredicateBase
31- . extend ( { style : z . literal ( ButtonStyle . Secondary ) } )
32- . strict ( ) ;
33- const buttonSuccessPredicate = buttonCustomIdPredicateBase . extend ( { style : z . literal ( ButtonStyle . Success ) } ) . strict ( ) ;
34- const buttonDangerPredicate = buttonCustomIdPredicateBase . extend ( { style : z . literal ( ButtonStyle . Danger ) } ) . strict ( ) ;
28+ const buttonPrimaryPredicate = buttonCustomIdPredicateBase . extend ( { style : z . literal ( ButtonStyle . Primary ) } ) ;
29+ const buttonSecondaryPredicate = buttonCustomIdPredicateBase . extend ( { style : z . literal ( ButtonStyle . Secondary ) } ) ;
30+ const buttonSuccessPredicate = buttonCustomIdPredicateBase . extend ( { style : z . literal ( ButtonStyle . Success ) } ) ;
31+ const buttonDangerPredicate = buttonCustomIdPredicateBase . extend ( { style : z . literal ( ButtonStyle . Danger ) } ) ;
3532
36- const buttonLinkPredicate = buttonPredicateBase
37- . extend ( {
38- style : z . literal ( ButtonStyle . Link ) ,
39- url : z
40- . string ( )
41- . url ( )
42- . refine ( refineURLPredicate ( [ 'http:' , 'https:' , 'discord:' ] ) ) ,
43- emoji : emojiPredicate . optional ( ) ,
44- label : labelPredicate ,
45- } )
46- . strict ( ) ;
33+ const buttonLinkPredicate = buttonPredicateBase . extend ( {
34+ style : z . literal ( ButtonStyle . Link ) ,
35+ url : z . url ( { protocol : / ^ (?: h t t p s ? | d i s c o r d ) $ / } ) ,
36+ emoji : emojiPredicate . optional ( ) ,
37+ label : labelPredicate ,
38+ } ) ;
4739
48- const buttonPremiumPredicate = buttonPredicateBase
49- . extend ( {
50- style : z . literal ( ButtonStyle . Premium ) ,
51- sku_id : z . string ( ) ,
52- } )
53- . strict ( ) ;
40+ const buttonPremiumPredicate = buttonPredicateBase . extend ( {
41+ style : z . literal ( ButtonStyle . Premium ) ,
42+ sku_id : z . string ( ) ,
43+ } ) ;
5444
5545export const buttonPredicate = z . discriminatedUnion ( 'style' , [
5646 buttonLinkPredicate ,
@@ -71,7 +61,7 @@ const selectMenuBasePredicate = z.object({
7161
7262export const selectMenuChannelPredicate = selectMenuBasePredicate . extend ( {
7363 type : z . literal ( ComponentType . ChannelSelect ) ,
74- channel_types : z . nativeEnum ( ChannelType ) . array ( ) . optional ( ) ,
64+ channel_types : z . enum ( ChannelType ) . array ( ) . optional ( ) ,
7565 default_values : z
7666 . object ( { id : z . string ( ) , type : z . literal ( SelectMenuDefaultValueType . Channel ) } )
7767 . array ( )
@@ -84,7 +74,7 @@ export const selectMenuMentionablePredicate = selectMenuBasePredicate.extend({
8474 default_values : z
8575 . object ( {
8676 id : z . string ( ) ,
87- type : z . union ( [ z . literal ( SelectMenuDefaultValueType . Role ) , z . literal ( SelectMenuDefaultValueType . User ) ] ) ,
77+ type : z . literal ( [ SelectMenuDefaultValueType . Role , SelectMenuDefaultValueType . User ] ) ,
8878 } )
8979 . array ( )
9080 . max ( 25 )
@@ -113,23 +103,25 @@ export const selectMenuStringPredicate = selectMenuBasePredicate
113103 type : z . literal ( ComponentType . StringSelect ) ,
114104 options : selectMenuStringOptionPredicate . array ( ) . min ( 1 ) . max ( 25 ) ,
115105 } )
116- . superRefine ( ( menu , ctx ) => {
106+ . check ( ( ctx ) => {
117107 const addIssue = ( name : string , minimum : number ) =>
118- ctx . addIssue ( {
108+ ctx . issues . push ( {
119109 code : 'too_small' ,
120110 message : `The number of options must be greater than or equal to ${ name } ` ,
121111 inclusive : true ,
122112 minimum,
123113 type : 'number' ,
124114 path : [ 'options' ] ,
115+ origin : 'number' ,
116+ input : minimum ,
125117 } ) ;
126118
127- if ( menu . max_values !== undefined && menu . options . length < menu . max_values ) {
128- addIssue ( 'max_values' , menu . max_values ) ;
119+ if ( ctx . value . max_values !== undefined && ctx . value . options . length < ctx . value . max_values ) {
120+ addIssue ( 'max_values' , ctx . value . max_values ) ;
129121 }
130122
131- if ( menu . min_values !== undefined && menu . options . length < menu . min_values ) {
132- addIssue ( 'min_values' , menu . min_values ) ;
123+ if ( ctx . value . min_values !== undefined && ctx . value . options . length < ctx . value . min_values ) {
124+ addIssue ( 'min_values' , ctx . value . min_values ) ;
133125 }
134126 } ) ;
135127
@@ -152,14 +144,13 @@ export const actionRowPredicate = z.object({
152144 . max ( 5 ) ,
153145 z
154146 . object ( {
155- type : z . union ( [
156- z . literal ( ComponentType . ChannelSelect ) ,
157- z . literal ( ComponentType . MentionableSelect ) ,
158- z . literal ( ComponentType . RoleSelect ) ,
159- z . literal ( ComponentType . StringSelect ) ,
160- z . literal ( ComponentType . UserSelect ) ,
161- // And this!
162- z . literal ( ComponentType . TextInput ) ,
147+ type : z . literal ( [
148+ ComponentType . ChannelSelect ,
149+ ComponentType . MentionableSelect ,
150+ ComponentType . StringSelect ,
151+ ComponentType . RoleSelect ,
152+ ComponentType . TextInput ,
153+ ComponentType . UserSelect ,
163154 ] ) ,
164155 } )
165156 . array ( )
0 commit comments