8000 v4: `refine` no longer accepts `params` as a function · Issue #4129 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

v4: refine no longer accepts params as a function #4129

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

Closed
arg0NNY opened this issue Apr 12, 2025 · 3 comments
Closed

v4: refine no longer accepts params as a function #4129

arg0NNY opened this issue Apr 12, 2025 · 3 comments

Comments

@arg0NNY
Copy link
arg0NNY commented Apr 12, 2025

This ability is mentioned in the v3 docs:

For advanced cases, the second argument can also be a function that returns RefineParams.

const longString = z.string().refine(
 (val) => val.length > 10,
 (val) => ({ message: `${val} is not more than 10 characters` })
);

I couldn't find any mentions of this change in the migration guide so I assume it was unintentional.

@williamgoulois
Copy link

@arg0NNY You should check https://v4.zod.dev/error-customization, message is now error and the second argument can be a function with an object argument containing input

const longStringZod = z.string().refine((val) => val.length > 10, {
  error: (iss) => `${iss.input} is not more than 10 characters`,
});

But in your case I would use :

const longStringZod = z.string().min(10, {
  error: (iss) => `${iss.input} is not more than 10 characters`,
});

or with @zod/mini :

import * as zMini from "@zod/mini";

const longStringZodMini = zMini.string().check(
  zMini.minLength(10, {
    error: (iss) => `${iss.input} is not more than 10 characters`,
  }),
);

@arg0NNY
Copy link
Author
arg0NNY commented Apr 12, 2025

Oh, missed that in the docs, thanks. I believe this should be added to the migration guide though.

However, is there a way for a path to be computed in refine as it can be in the v3 using function as params:

.refine(
  value => /* ... */,
  value => ({
    message: '...',
    path: [].concat(/* some conditions based on `value` */)
  })
)

Or should such a refinement just be transformed into custom check?

@colinhacks
Copy link
Owner

This is an intentional change, as noted. I added a note to the changelog as well. Thanks for pointing this out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0