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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of RawCreateParams for preprocess context #1100

Closed
ian-schu opened this issue Apr 25, 2022 · 2 comments
Closed

Use of RawCreateParams for preprocess context #1100

ian-schu opened this issue Apr 25, 2022 · 2 comments
Labels
wontfix This will not be worked on

Comments

@ian-schu
Copy link

ian-schu commented Apr 25, 2022

Hey there 馃憢馃徎 I'm coming in from io-ts and very happy to have found this project.

TL;DR: assuming I've set up some of my own-built schemas correctly, I can't seem to get custom errors out of a preprocess-based schema.


What I have is an own-rolled schema like the following, based on the Dates example from your docs:

export const dateString = (
  params?: RawCreateParams // <-- added params
) =>
  z.preprocess(
    (input: unknown) => {
      if (!(typeof input == 'string')) {
        return;
      }

      const date = new Date(input);

      if (isNaN(date.getTime())) {
        return;
      }
      return date;
    },
    z.date(),
    params
  );

Pretty basic stuff. The idea is to use it like so:

const someSchema = z.object({
  updated_at: dateString({
    invalid_type_error: "Field `updated_at` had wrong type"
  })
})

Problem is whenever I do this, the custom error message doesn't seem to survive. I've tried all the fields under RawCreateParams, but still just get the default error object when parsing fails.

By contrast, doing this with a built-in schema works just fine:

const someSchema = z.object({
  someString: z.string({
    invalid_type_error: 'Custom error message here!',
  }),
});

So, what am I doing wrong? Or is this a bug with preprocess?

@ian-schu
Copy link
Author

Possibly related to #696

@stale
Copy link

stale bot commented Jun 25, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Jun 25, 2022
@stale stale bot closed this as completed Jul 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

1 participant