TheEdoRan / next-safe-action

Type safe and validated Server Actions in your Next.js project.
https://next-safe-action.dev
MIT License
2.06k stars 32 forks source link

[BUG] TypeError in useAction when using a yup schema #183

Closed valentinludu closed 1 month ago

valentinludu commented 2 months ago

Are you using the latest version of this library?

Is there an existing issue for this?

Describe the bug

I tryied a zod schema and works fine.

But with a yup schema:

Reproduction steps

Using "typescript": "^5.4.2", "@typeschema/yup": "^0.13.3", "next": "14.1.1", "react": "^18.2.0", "next-safe-action": "^7.1.0"

My client:

import type { IronSession } from 'iron-session';
import { getIronSession } from 'iron-session';
import { cookies } from 'next/headers';
import { createSafeActionClient } from 'next-safe-action/typeschema';
import * as yup from 'yup';

export const actionClient = createSafeActionClient({
  handleReturnedServerError: handleServerError,
  defineMetadataSchema: () => {
    return yup.object({
      actionName: yup.string()
    });
  }
})
.use(async ({ next }) => {
    const result = await next({ ctx: null });
    // log.info('LOGGING MIDDLEWARE FOR ACTION');
    return result;
})
.use(async ({ next }) => {
    const session = await getIronSession<SessionData>(cookies(), {});
    return next({
      ctx: { session }
    });
  });

defineMetadataSchema throws:

Screenshot 2024-06-25 at 13 42 57

Using useAction in the FE throws a TypeError:

const { execute, executeAsync, result, status, reset, isIdle, isExecuting, hasSucceeded, hasErrored } = useAction(actionOnboarding);

Screenshot 2024-06-25 at 13 41 45

useAction expects Zod:

Screenshot 2024-06-25 at 13 55 38

Expected behavior

useAction should expect the yup schema and not the zod one. defineMetadataSchema should accept yup.

Minimal reproduction example

https://github.com

Operating System

macOS

Library version

7.1.0

Next.js version

14.1.1

Additional context

No response

TheEdoRan commented 2 months ago

Cannot reproduce the issue:

image

My guess is you just need to uninstall zod and @typeschema/zod, maybe delete node_modules and lockfile as well, then reinstall dependencies and reload the editor. TypeSchema works with dynamic imports and TypeScript magic, so it causes errors sometimes. If the issue persists I'll need a reproduction link to investigate this.

valentinludu commented 2 months ago

I tested it inside a CodeSandbox and the errors are not present. So it must be something on my end. I don't have "zod" and "@typeschema/zod" installed so I will try to delete node_modules, lock file and see if I get it to work.

Thank you for the quick response. I will try it out and come back if I have any issues.

TheEdoRan commented 2 months ago

Any updates on this issue?

valentinludu commented 2 months ago

After deleting package-lock and node_modules, now useAction types are working. On the other hand defineMetadataSchema is still throwing that type error. I just added @ts-ignore for now so I can continue my work.

TheEdoRan commented 2 months ago

Kinda weird, have you tried reloading the editor?

valentinludu commented 2 months ago

Yes. Still there.

valentinludu commented 2 months ago

Most certainly it's because I am in a monorepo. Unfortunately I can't pin where the actual problem is.

TheEdoRan commented 2 months ago

Does v7.2.0 solve this issue? next-safe-action now ships with built-in support for multiple validation libraries. You can find more information about that in this discussion and on this page of the website.

valentinludu commented 1 month ago

After I updated the lib to the latest version 7.4.1, all the TypeScript errors are gone. I tested with TypeScript 5.4 and 5.5 because I had an issue with v5.5. as well and that is fixed too.

Thanks a million. Issue can be closed!

TheEdoRan commented 1 month ago

That's great, thanks for the feedback!