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 comment

4

u/[deleted] Apr 07 '26

[removed] — view removed comment

7

u/pthierry Apr 07 '26

I can write a function in the functional core that takes unvalidated input. I don't see how that would guarantee anything.

Can you show an actual example where spec does more than static typing?

4

u/[deleted] Apr 07 '26

[removed] — view removed comment

2

u/pthierry Apr 08 '26

There's a huge downside if it's a convention. When you parse instead of validating, the unvalidated data and type only exist at the boundary of the system. It's not a convention that some reckless developer might choose not to follow. Inside the boundary, only the result of parsing exists.

1

u/Alternative-Papaya57 Apr 08 '26

No type system will save you from a reckless developer.

4

u/pthierry Apr 08 '26

It has, several times. Some of them, I was the reckless one.

1

u/[deleted] Apr 08 '26

[removed] — view removed comment

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.

→ More replies