@@ -676,6 +676,47 @@ test("z.check", () => {
676676 } ) ;
677677} ) ;
678678
679+ test ( "z.with (alias for z.check)" , ( ) => {
680+ // .with() should work exactly the same as .check()
681+ const a = z . any ( ) . with (
682+ z . check < string > ( ( ctx ) => {
683+ if ( typeof ctx . value === "string" ) return ;
684+ ctx . issues . push ( {
685+ code : "custom" ,
686+ origin : "custom" ,
687+ message : "Expected a string" ,
688+ input : ctx . value ,
689+ } ) ;
690+ } )
691+ ) ;
692+ expect ( z . safeParse ( a , "hello" ) ) . toMatchObject ( {
693+ success : true ,
694+ data : "hello" ,
695+ } ) ;
696+ expect ( z . safeParse ( a , 123 ) ) . toMatchObject ( {
697+ success : false ,
698+ error : { issues : [ { code : "custom" , message : "Expected a string" } ] } ,
699+ } ) ;
700+
701+ // Test with refine
702+ const b = z . string ( ) . with ( z . refine ( ( val ) => val . length > 3 , "Must be longer than 3" ) ) ;
703+ expect ( z . safeParse ( b , "hello" ) . success ) . toBe ( true ) ;
704+ expect ( z . safeParse ( b , "hi" ) . success ) . toBe ( false ) ;
705+
706+ // Test with function
707+ const c = z . string ( ) . with ( ( { value, issues } ) => {
708+ if ( value . length <= 3 ) {
709+ issues . push ( {
710+ code : "custom" ,
711+ input : value ,
712+ message : "Must be longer than 3" ,
713+ } ) ;
714+ }
715+ } ) ;
716+ expect ( z . safeParse ( c , "hello" ) . success ) . toBe ( true ) ;
717+ expect ( z . safeParse ( c , "hi" ) . success ) . toBe ( false ) ;
718+ } ) ;
719+
679720test ( "z.instanceof" , ( ) => {
680721 class A { }
681722
0 commit comments