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

feat: allow ctx.addIssue from transform #1056

Merged
merged 3 commits into from
May 6, 2022
Merged

feat: allow ctx.addIssue from transform #1056

merged 3 commits into from
May 6, 2022

Conversation

morgs32
Copy link
Contributor

@morgs32 morgs32 commented Mar 29, 2022

Closes out part of #696 . Except only on transforms!

z
    .string()
    .transform((data, ctx) => {
      const i = strs.indexOf(data)
      if (i === -1) {
        ctx.addIssue({
          code: 'custom',
          message: `${data} is not one of our allowed strings`,
        })
      }
      return data.length
    })
    .parse("asdf");

Will throw. ctx works just like in superRefine()

return data.length
})
.parse("asdf");
}).toThrow();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if this test could check for the kind of error, ideally the custom code/message.

Otherwise, this test would also pass if ctx is undefined as it would throw a JS error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Checking the whole error message now.

@morgs32
Copy link
Contributor Author

morgs32 commented Apr 6, 2022

@FlorianWendelborn whaddya think? This a break from anyone's philosophy on zod? I'd love to wax zod philosophy

@FlorianWendelborn
Copy link
Contributor

Well, I'm in favor of this. I don't have the power to merge anything here though, just a zod user who likes to contribute a bit with suggestions

@morgs32
Copy link
Contributor Author

morgs32 commented Apr 14, 2022

insertgif.com

@colinhacks colinhacks merged commit d54ba35 into colinhacks:master May 6, 2022
@colinhacks
Copy link
Owner

Sorry for the delay on this. Fantastic PR. Landed in 3.15.0.

MrAwesome pushed a commit to MrAwesome/zod that referenced this pull request May 20, 2022
* feat: allow ctx.addIssue from transform

* chore: update transform test

* minor: lints
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

Successfully merging this pull request may close these issues.

3 participants