Open apeman76 opened 10 months ago
Thats weird... The shape
property works well on my test project.
// This can be imported
const postSchema = z.object({
id: z.string(),
});
export const POST = (async ({ request, locals }) => {
const { data } = await validate(request, { ...postSchema.shape });
return json({});
}) satisfies RequestHandler;
You also aren't required to spread the shape. (validate(request, postSchema.shape)
)
Can you maybe share more details about your project?
Thats weird... The
shape
property works well on my test project.// This can be imported const postSchema = z.object({ id: z.string(), }); export const POST = (async ({ request, locals }) => { const { data } = await validate(request, { ...postSchema.shape }); return json({}); }) satisfies RequestHandler;
You also aren't required to spread the shape. (
validate(request, postSchema.shape)
)Can you maybe share more details about your project?
Thanks for the fast reply! You're right that I dont need to spread the shape, I found out that it seems to be caused by z.enum, whenever I comment out that line it will give the
TypeError: Cannot read properties of undefined (reading 'getBaseTypes')
error
i.e.
export const testSchema = z.object({
test: z.string(),
someData: z.enum(["maybe", "orthis"]).optional()
});
"sveltekit-typesafe-api": "^0.1.0",
"zod": "^3.22.4",
"vite": "^4.5.0",
"typescript": "^5.3.3",
"@sveltejs/adapter-node": "^1.3.1",
I added a z.enum
in my schema and it worked normally...
Maybe its your vite
version that is causing this issue? (it would be suprising tho)
I would recommend updating your dependencies. Here is my package.json
:
"dependencies": {
"@sveltejs/kit": "^2.3.3",
"svelte": "^4.2.8",
"sveltekit-typesafe-api": "^0.1.2",
"vite": "^5.0.11",
"zod": "^3.22.4"
},
I added a
z.enum
in my schema and it worked normally... Maybe its yourvite
version that is causing this issue? (it would be suprising tho) I would recommend updating your dependencies. Here is mypackage.json
:"dependencies": { "@sveltejs/kit": "^2.3.3", "svelte": "^4.2.8", "sveltekit-typesafe-api": "^0.1.2", "vite": "^5.0.11", "zod": "^3.22.4" },
Ah yes I'm still on Svelte 1 but I did a dirty upgrade to Sveltekit 2 and it seems to work now.. Thank you! I'll try it out when I'm at a point that I can upgrade to Sveltekit 2 (soon) 👍
Hello, Im sorry to bother again but I have upgraded to Svelte 2 and somehow I'm still getting the error. I have set up a codesandbox and I'm also getting the error there. Not sure what I might be doing wrong here, it still seems to becaused by z.enum: https://codesandbox.io/p/devbox/friendly-meadow-xqhwp5
Hi, im trying to get the type safety working with my API but I already have a ton of zod schemas defined in another file, when I try to use these it wont work.
gives
Error: "Invalid data: at 'mySchema': 'Required'."
gives
error when starting dev server: TypeError: Cannot read properties of undefined (reading 'getBaseTypes')
const { data } = await validate(request, mySchema);
givesArgument of type 'ZodObject<{ mystuff }, "strip", ZodTypeAny, { ...; }, { ...; }>'.
Is there any way to make this work?