@@ -5,6 +5,8 @@ import * as v from 'valibot';
55import { zodToJSONSchema } from '$lib/adapters/zod.js' ;
66import { SchemaError } from '$lib/index.js' ;
77import { valibot } from '$lib/adapters/valibot.js' ;
8+ import { type } from 'arktype' ;
9+ import { arktype } from '$lib/adapters/arktype.js' ;
810
911enum Foo {
1012 A = 2 ,
@@ -27,11 +29,14 @@ const schema = z.object({
2729
2830const bigJsonSchema = zodToJSONSchema ( schema ) ;
2931
30- function dataToFormData ( data : Record < string , string | number | string [ ] | number [ ] > ) {
32+ function dataToFormData (
33+ data : Record < string , string | number | File | string [ ] | number [ ] | File [ ] >
34+ ) {
3135 const output = new FormData ( ) ;
3236 for ( const [ key , value ] of Object . entries ( data ) ) {
33- if ( Array . isArray ( value ) ) value . forEach ( ( v ) => output . append ( key , String ( v ) ) ) ;
34- else output . set ( key , String ( value ) ) ;
37+ if ( Array . isArray ( value ) )
38+ value . forEach ( ( v ) => output . append ( key , v instanceof File ? v : String ( v ) ) ) ;
39+ else output . set ( key , value instanceof File ? value : String ( value ) ) ;
3540 }
3641 return output ;
3742}
@@ -78,4 +83,22 @@ describe('FormData parsing', () => {
7883
7984 expect ( valibot ( schema ) . defaults . urltest ) . toBe ( '' ) ;
8085 } ) ;
86+
87+ it ( 'should handle empty arrays with simple adapters as "any"' , ( ) => {
88+ const uploadSchema = type ( {
89+ files : type . instanceOf ( File ) . array ( )
90+ } ) ;
91+
92+ const defaults = { defaults : { files : [ ] as File [ ] } } ;
93+ const adapter = arktype ( uploadSchema , defaults ) ;
94+
95+ const formData = dataToFormData ( {
96+ files : [ new File ( [ '123123' ] , 'test.png' ) ]
97+ } ) ;
98+ const parsed = parseFormData ( formData , adapter . jsonSchema , { allowFiles : true } ) ;
99+ const file = parsed . data ?. files as File [ ] ;
100+
101+ expect ( file [ 0 ] . size ) . toBe ( 6 ) ;
102+ expect ( file [ 0 ] . name ) . toBe ( 'test.png' ) ;
103+ } ) ;
81104} ) ;
0 commit comments