Guibi1 / SvelteKit-TypeSafe-API

Adding type safety to SvelteKit's server endpoints.
https://npmjs.com/sveltekit-typesafe-api
MIT License
7 stars 0 forks source link

Directly use zod schema in validate #2

Open apeman76 opened 6 months ago

apeman76 commented 6 months ago

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.

    const { data } = await validate(request, {
        mySchema
    });

gives Error: "Invalid data: at 'mySchema': 'Required'."

    const { data } = await validate(request, {
        ...mySchema.shape
    });

gives error when starting dev server: TypeError: Cannot read properties of undefined (reading 'getBaseTypes')

const { data } = await validate(request, mySchema); gives Argument of type 'ZodObject<{ mystuff }, "strip", ZodTypeAny, { ...; }, { ...; }>'.

Is there any way to make this work?

Guibi1 commented 6 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?

apeman76 commented 6 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?

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",
Guibi1 commented 6 months ago

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"
  },
apeman76 commented 6 months ago

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"
  },

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) 👍

apeman76 commented 6 months ago

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