@@ -22,3 +22,36 @@ test("The callback's return value becomes the apply's return value.", () => {
2222 expect ( result ) . toBe ( symbol ) ;
2323 expectTypeOf < typeof result > ( ) . toEqualTypeOf < symbol > ( ) ;
2424} ) ;
25+
26+ test ( "apply forwards extra args to the callback" , ( ) => {
27+ const calls : unknown [ ] = [ ] ;
28+ const capture = ( schema : z . ZodMiniString , ...args : unknown [ ] ) => {
29+ calls . push ( schema , ...args ) ;
30+ return schema ;
31+ } ;
32+
33+ const schema = z . string ( ) ;
34+ const result = schema . apply ( capture , "id" , 42 ) ;
35+
36+ expect ( calls [ 0 ] ) . toBe ( schema ) ;
37+ expect ( calls [ 1 ] ) . toBe ( "id" ) ;
38+ expect ( calls [ 2 ] ) . toBe ( 42 ) ;
39+ expect ( z . parse ( result , "value" ) ) . toBe ( "value" ) ;
40+ expectTypeOf < z . infer < typeof result > > ( ) . toEqualTypeOf < string > ( ) ;
41+ } ) ;
42+
43+ test ( "apply type-checks the extra args" , ( ) => {
44+ const withMin = ( schema : z . ZodMiniString , n : number ) => schema . check ( z . minLength ( n ) ) ;
45+
46+ expect ( z . parse ( z . string ( ) . apply ( withMin , 3 ) , "abcd" ) ) . toBe ( "abcd" ) ;
47+ expectTypeOf (
48+ z . number ( ) . apply < z . ZodMiniNumber > ( ( schema ) => schema . check ( z . minimum ( 0 ) ) )
49+ ) . toEqualTypeOf < z . ZodMiniNumber > ( ) ;
50+
51+ // @ts -expect-error wrong arg type
52+ z . string ( ) . apply ( withMin , "3" ) ;
53+ // @ts -expect-error missing arg
54+ z . string ( ) . apply ( withMin ) ;
55+ // @ts -expect-error extra arg for a single-parameter callback
56+ z . string ( ) . apply ( ( schema : z . ZodMiniString ) => schema , 3 ) ;
57+ } ) ;
0 commit comments