@@ -615,6 +615,60 @@ describe('File handling with the allowFiles option', () => {
615615 } ) ;
616616 } ) ;
617617
618+ describe ( 'Handling non-existing files' , ( ) => {
619+ const schema = z . object ( {
620+ image : z
621+ . custom < File > ( ( f ) => f instanceof File , 'Please upload a file.' )
622+ . refine ( ( f ) => {
623+ return f instanceof File && f . size <= 1000 ;
624+ } , 'Max 1Kb upload size.' )
625+ } ) ;
626+
627+ describe ( 'Non-nullable schema' , ( ) => {
628+ it ( 'should not accept a non-existing file' , async ( ) => {
629+ const formData = new FormData ( ) ;
630+ const adapter = zod ( schema ) ;
631+ expect ( adapter . defaults . image ) . toBeUndefined ( ) ;
632+
633+ const form = await superValidate ( formData , adapter , { allowFiles : true } ) ;
634+ assert ( ! form . valid ) ;
635+ expect ( form . errors . image ) . toEqual ( [ 'Please upload a file.' ] ) ;
636+ } ) ;
637+
638+ it ( 'should not accept an empty posted value' , async ( ) => {
639+ const formData = new FormData ( ) ;
640+ formData . set ( 'image' , '' ) ;
641+ const form = await superValidate ( formData , zod ( schema ) , { allowFiles : true } ) ;
642+
643+ assert ( ! form . valid ) ;
644+ expect ( form . errors . image ) . toEqual ( [ 'Please upload a file.' ] ) ;
645+ } ) ;
646+ } ) ;
647+
648+ describe ( 'Nullable schema' , ( ) => {
649+ const schemaNullable = schema . extend ( {
650+ image : schema . shape . image . nullable ( )
651+ } ) ;
652+
653+ it ( 'should accept a non-existing file' , async ( ) => {
654+ const formData = new FormData ( ) ;
655+ const form = await superValidate ( formData , zod ( schemaNullable ) , { allowFiles : true } ) ;
656+
657+ assert ( form . valid ) ;
658+ expect ( form . data . image ) . toBeNull ( ) ;
659+ } ) ;
660+
661+ it ( 'should accept an empty posted value' , async ( ) => {
662+ const formData = new FormData ( ) ;
663+ formData . set ( 'image' , '' ) ;
664+ const form = await superValidate ( formData , zod ( schemaNullable ) , { allowFiles : true } ) ;
665+
666+ assert ( form . valid ) ;
667+ expect ( form . data . image ) . toBeNull ( ) ;
668+ } ) ;
669+ } ) ;
670+ } ) ;
671+
618672 it ( 'should allow files if specified as an option' , async ( ) => {
619673 const formData = new FormData ( ) ;
620674 formData . set ( 'avatar' , new Blob ( [ 'A' . repeat ( 100 ) ] ) ) ;
0 commit comments