@@ -3,7 +3,17 @@ import { describe, expect, expectTypeOf, it, test } from 'vitest';
33import { z as z3 } from 'zod/v3' ;
44import { z as z4 } from 'zod/v4' ;
55
6- import { $BooleanLike , $NumberLike , $Uint8ArrayLike , $UrlLike , isZodType , isZodTypeLike , safeParse } from '../zod.js' ;
6+ import {
7+ $$Function ,
8+ $AnyFunction ,
9+ $BooleanLike ,
10+ $NumberLike ,
11+ $Uint8ArrayLike ,
12+ $UrlLike ,
13+ isZodType ,
14+ isZodTypeLike ,
15+ safeParse
16+ } from '../zod.js' ;
717
818import type {
919 ZodErrorLike ,
@@ -216,6 +226,41 @@ describe('$Uint8ArrayLike', () => {
216226 } ) ;
217227} ) ;
218228
229+ describe ( '$AnyFunction' , ( ) => {
230+ it ( 'should be correctly typed' , ( ) => {
231+ expectTypeOf < z4 . infer < typeof $AnyFunction > > ( ) . toEqualTypeOf < ( ...args : any [ ] ) => any > ( ) ;
232+ } ) ;
233+ it ( 'should fail to validate a non-function' , ( ) => {
234+ expect ( $AnyFunction . safeParse ( '' ) . success ) . toBe ( false ) ;
235+ } ) ;
236+ it ( 'should validate a function' , ( ) => {
237+ expect ( $AnyFunction . safeParse ( safeParse ) . success ) . toBe ( true ) ;
238+ } ) ;
239+ } ) ;
240+
241+ describe ( '$$Function' , ( ) => {
242+ const $Schema = $$Function ( { input : [ z4 . number ( ) , z4 . number ( ) ] , output : z4 . number ( ) } ) ;
243+ it ( 'should be correctly typed' , ( ) => {
244+ expectTypeOf < z4 . infer < typeof $Schema > > ( ) . toEqualTypeOf < ( arg_0 : number , arg_1 : number ) => number > ( ) ;
245+ } ) ;
246+ it ( 'should fail to validate a non-function' , ( ) => {
247+ expect ( $Schema . safeParse ( '' ) . success ) . toBe ( false ) ;
248+ } ) ;
249+ it ( 'should return a function that throws when called with the incorrect number of arguments' , ( ) => {
250+ const fn = $Schema . parse ( ( ..._args : any [ ] ) => 0 ) as ( ...args : any [ ] ) => number ;
251+ expect ( ( ) => fn ( 1 ) ) . toThrow ( ) ;
252+ expect ( ( ) => fn ( 1 , 2 , 3 ) ) . toThrow ( ) ;
253+ } ) ;
254+ it ( 'should return a function that throws when it returns an invalid value' , ( ) => {
255+ const fn = $Schema . parse ( ( ..._args : any [ ] ) => 'hello' ) as ( ...args : any [ ] ) => any ;
256+ expect ( ( ) => fn ( 1 , 2 ) ) . toThrow ( ) ;
257+ } ) ;
258+ it ( 'should return a function that returns a valid value, when called with valid inputs' , ( ) => {
259+ const fn = $Schema . parse ( ( a : number , b : number ) => a + b ) ;
260+ expect ( fn ( 1 , 2 ) ) . toBe ( 3 ) ;
261+ } ) ;
262+ } ) ;
263+
219264describe ( 'safeParse' , ( ) => {
220265 const $Schema = z4 . object ( { foo : z4 . enum ( [ '1' , '2' ] ) . transform ( Number ) } ) ;
221266 it ( 'should return an Ok result with the parsed data if successful' , ( ) => {
0 commit comments