Skip to content

LazyFile not usable with zod z.file() validator in remote "form" function data #15018

@mattismoel

Description

@mattismoel

Describe the bug

I am testing out remote funcions 'form' function with an input zod schema for validating the form data like this:

//auth.ts
export const registerForm = z
	.object({
		email: z.email(),
		firstName: z.string().nonempty(),
		lastName: z.string().nonempty(),
		password: z.string().min(MINIMUM_PASSWORD_LENGTH).max(MAXIMUM_PASSWORD_LENGTH),
		passwordConfirm: z.string(),
		avatar: z.file().max(5_000_000).mime(["image/png", "image/jpeg"])
	})
	.refine((data) => data.password === data.passwordConfirm, "Passwords do not match");

Which is used like this:

//auth.remote.ts
import { form, getRequestEvent, query } from "$app/server";
import { registerForm, userSchema } from "./auth";
import { id } from "$lib/model";

export const register = form(registerForm, async (form) => {
	const { locals } = getRequestEvent();
	await locals.pb.collection("users").create(form);
});
<!-- page.svelte -->
<script lang="ts">
	import FormField from "$lib/components/FormField.svelte";
	import Button from "$lib/components/ui/Button.svelte";
	import Input from "$lib/components/ui/Input.svelte";
	import { register } from "$lib/features/auth.remote";

	const { avatar, ...rest } = register.fields;
</script>

<main class="flex min-h-svh items-center justify-center">
	<form {...register} enctype="multipart/form-data">
    	        <FormField issues={avatar.issues()}>
    		        <Input {...avatar.as("file")} />
    	        </FormField>
                <Button>Submit</Button>
	</form>
</main>

However this results in the following zod error: "Invalid input: expected file, received LazyFile"

I can see in the SvelteKit source code that this class LazyFile is not exported either, so i would not even be able to do something like avatar: z.instanceof(LazyFile).

If I instead type it as avatar: z.any() (which I don't like), the resulting form data property in the form function, is still LazyFile, which stuff like PocketBase unfortunatly also errors on, giving me errors like:

{
  "data": {
    "avatar": "Invalid new files: [[object Object]]."
  },
  "raw": "avatar: Invalid new files: [[object Object]].."
}

Would love to have this fixed, or someone guide me in another direction, cause these remote functions are actually awesome!

Any temporary workarounds?

Reproduction

  • Create a zod schema with a z.file() field
  • Use that schema in a remote form function as the form data schema
  • Success - validation errors for the input file...

Logs

System Info

System:
    OS: Linux 6.17 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (12) x64 AMD Ryzen 5 2600 Six-Core Processor
    Memory: 4.00 GB / 15.56 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    bun: 1.2.21 - /usr/local/bin/bun
  npmPackages:
    @sveltejs/adapter-auto: ^7.0.0 => 7.0.0 
    @sveltejs/kit: ^2.49.1 => 2.49.1 
    @sveltejs/vite-plugin-svelte: ^6.2.1 => 6.2.1 
    svelte: ^5.45.4 => 5.45.4 
    vite: ^7.2.6 => 7.2.6

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions