question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Point-free `.parse` not possible since v3.9.0 – a feature or a bug?

See original GitHub issue

Hi!

Zod is an awesome project. And for a while I had code looking like this when parsing data from API endpoints:

import { z } from "zod";

type User = z.infer<typeof User>;
const User = z.object({
  name: z.string()
});

function getUser(): Promise<User> {
  return getUserFromApi().then(User.parse);
}

getUser()
  .then(
    // Use User
  )
  .catch(
    // Handle parsing error
  );

This is just a simplified example, but the idea is that User.parse is called by .then to parse whatever data is coming from the Promise. If User.parse throws an error, the whole thing results in a rejected Promise. Nice and simple and without the need to write .then(user => User.parse(user)).

However, since the release of v3.9.0, .parse cannot be used in a point-free way. An error is thrown inside Zod:

TypeError: Cannot read properties of undefined (reading 'safeParse')

Now, I want to ask if this behaviour is intentional or if it’s a bug?

Disclaimers:

  • I know that many people discourage point-free programming in JavaScript. It can be problematic, as the error above demonstrates, but I still happen to use the technique when I find that it simplifies my code.
  • It’s absolutely fine with me if this is not considered a bug. It’s a small thing, and I’ll keep using Zod even if I can’t use .parse in a point-free way. I just wanted to make sure that I reported my findings.

I created a CodeSandbox that shows running code in v3.8.2, but if you change the version to v3.9.0 under “Dependencies”, the example breaks: https://codesandbox.io/s/point-free-zod-parse-473q1?file=/src/index.ts

Screen Shot 2021-12-02 at 22 23 08

Finally, thank you for Zod. It’s truly awesome 🙏

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
colinhackscommented, Mar 1, 2022

Bug! Fixed in 3.12.1 - sorry about that.

0reactions
JacobWeisenburgercommented, Feb 27, 2022

Thanks I’ll keep this open

Read more comments on GitHub >

github_iconTop Results From Across the Web

Point-Free: A video series on functional programming and the ...
Point-Free is a video series exploring functional programming and Swift. ... as concisely as possible, to avoid a whole class of bugs, unlock...
Read more >
A blog exploring functional programming and Swift. - Point-Free
A companion blog to Point-Free, exploring functional programming and Swift.
Read more >
Parser Errors - Point-Free
A new release of swift-parsing that brings delightful and informative error messaging to parser failures.
Read more >
Episode #185: Tour of Parser-Printers: Introduction - Point-Free
Today we celebrate a huge release of swift-parsing, which includes the ability to build invertible parser-printers with ease.
Read more >
Introducing Parser Builders - Point-Free
Today we are releasing 0.5.0 of our swift-parsing library, which leverages result builders for creating complex parsers with a minimal ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found