diff --git a/packages/conform-zod/coercion.ts b/packages/conform-zod/coercion.ts index 6f492625..9c46f335 100644 --- a/packages/conform-zod/coercion.ts +++ b/packages/conform-zod/coercion.ts @@ -55,7 +55,12 @@ export function coerceString( * Modify the value only if it's a file, otherwise return the value as-is */ export function coerceFile(file: unknown) { - if (file instanceof File && file.name === '' && file.size === 0) { + if ( + typeof File !== 'undefined' && + file instanceof File && + file.name === '' && + file.size === 0 + ) { return undefined; } @@ -68,6 +73,10 @@ export function coerceFile(file: unknown) { * Check the `instanceOfType` function on zod for more info */ export function isFileSchema(schema: ZodEffects): boolean { + if (typeof File === 'undefined') { + return false; + } + return ( schema._def.effect.type === 'refinement' && schema.innerType() instanceof ZodAny && diff --git a/tests/conform-zod.spec.ts b/tests/conform-zod.spec.ts index 60b59112..71a3a2b7 100644 --- a/tests/conform-zod.spec.ts +++ b/tests/conform-zod.spec.ts @@ -13,7 +13,7 @@ function createFormData(entries: Array<[string, string | File]>): FormData { return formData; } -test.beforeAll(() => { +test.beforeEach(() => { installGlobals(); }); @@ -660,6 +660,11 @@ test.describe('conform-zod', () => { }, error: {}, }); + + // @ts-expect-error To test if File is not defined in certain environment + delete global.File; + + expect(() => parse(createFormData([]), { schema })).not.toThrow(); }); test('z.lazy', () => {