Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
valentinludu opened this issue Jun 25, 2024 · 7 comments
Open
2 tasks done

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

valentinludu opened this issue Jun 25, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@valentinludu
Copy link

Are you using the latest version of this library?

  • I verified that the issue exists in the latest next-safe-action release

Is there an existing issue for this?

  • I have searched the existing issues and found nothing that matches

Describe the bug

I tryied a zod schema and works fine.

But with a yup schema:

  • useAction hook throws a type error when used with an action that has an yup schema.
  • defineMetadataSchema throws a type error when defining 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

@valentinludu valentinludu added the bug Something isn't working label Jun 25, 2024
@TheEdoRan
Copy link
Owner

TheEdoRan commented Jun 25, 2024

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
Copy link
Author

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
Copy link
Owner

Any updates on this issue?

@valentinludu
Copy link
Author

valentinludu commented Jun 26, 2024

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
Copy link
Owner

Kinda weird, have you tried reloading the editor?

@valentinludu
Copy link
Author

Yes. Still there.

@valentinludu
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants