r/functionalprogramming • • Apr 07 '26

TypeScript Parse, Don't Validate — In a Language That Doesn't Want You To · cekrem.github.io

https://cekrem.github.io/posts/parse-dont-validate-typescript/
33 Upvotes

35 comments sorted by

View all comments

Show parent comments

1

u/pthierry Apr 10 '26

How would the linter warn you that a string is used as an email?

1

u/[deleted] Apr 10 '26

[removed] — view removed comment

1

u/pthierry Apr 10 '26

But what prevents me from writing a function in the core that doesn't spec-check anything?

1

u/[deleted] Apr 11 '26

[removed] — view removed comment

1

u/pthierry Apr 12 '26

I'm not being obtuse, it's a reality of professional codebases: the "parse, don't validate" didn't appear from nowhere, it is a concrete answer to a concrete problem.

If you write functions that pass around a string as email that can be spec checked, you can insert in that system a bunch of functions that won't spec check anything. And all might work well for some time, because there are enough spec checks all around that a bad input never makes it way somewhere dangerous (hence the "shotgun validation"). But part of the codebase is unsafe and if someone adds an entry point where some inputs are not spec checked, the unvalidated input can be passed to unsafe parts of the system. And that's how you get a serious outage.

When you follow "parse, don't validate", there are no points in your core or the interfaces to external adapters where you can pass a string as an email argument, so developers are forced to use a parser when they want to interact with that code. The right path is not the easy path, it's the only path immediately available.

When someone adds a new entry point, skipping input validation is not a shortcut, it's a huge nuisance, because it would mean reimplementing everything that can only work with parsed types.

1

u/[deleted] Apr 12 '26

[removed] — view removed comment

1

u/pthierry Apr 12 '26

Feel free to pretend it's not a real situation. As I said, "Parse, dont validate" was a very pragmatic response to a very real situation in existing codebases.

Also, in my experience, codes that follow that pattern are not a nightmare to maintain, quite the contrary. There's a reason this article gets around so much: people following it tend to like the result.

1

u/[deleted] Apr 12 '26

[removed] — view removed comment

1

u/pthierry Apr 12 '26

I don't get your position. If you're on board with parsing, then there's a different type before and after parsing, so you don't have less types. If you have less types and dynamic typing, I don't see how you get the level of safety that parsing+types get us.