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

4

u/[deleted] Apr 07 '26

[removed] — view removed comment

19

u/pthierry Apr 07 '26

But if you have the guarantees only at the boundary, you don't know if you actually got them anywhere inside the system. That's the point.

If the guarantees aren't everywhere, they are nowhere…

6

u/AxelLuktarGott Apr 07 '26

I agree. At that point you're just doing dynamicly typed programming with extra steps

3

u/[deleted] Apr 07 '26

[removed] — view removed comment

8

u/pthierry Apr 07 '26

How do I know that some function argument has been spec-checked?

Can you describe one thing spec checks can do that could not be done with parsing and types?

1

u/Steve_the_Stevedore Apr 08 '26

How do I know that some function argument has been spec-checked?

By making it impossible to instantiate a value of that type without the spec-checks running.

For example you can just not export the type constructor and only export functions that spec-check.

type PlaneTicket= MkPlaneTicket {departureTime::DateTime, arrivalTime::DateTime, from::Airport, to::Airport}
createTicket :: {departureTime::DateTime, arrivalTime::DateTime, from::Airport, to::Airport} -> Maybe PlaneTicket
modifyDepartureTime:: PlaneTicket -> Airport -> Maybe PlaneTicket
readPlaneTicket :: Text -> Either PlaneTicket ParseError

And now you just export everything but MkPlaneTicket. People can see the type and work with it. But only readPlaneTicket can create it and only modifyDepartureTime can modify it. If those functions are correct and check the spec all other functions can depend on the spec being correct and don't need to check.

Can you describe one thing spec checks can do that could not be done with parsing and types?

Types cannot ensure coherence. For example departure should happen before arrival. There is no type that ensures that. In that case you could replace it with departure and duration, but what about checksums or signatures. If the server sends you a value that contains a checksum or a digital signature the value is valid if the checksum/signature matches the data. How would you create a type that only contains values that contain a matching signature? I don't know of a type system that offers this.

2

u/pthierry Apr 08 '26

By making it impossible to instantiate a value of that type without the spec-checks running.
(…)
And now you just export everything but MkPlaneTicket. People can see the type and work with it. But only readPlaneTicket can create it and only modifyDepartureTime can modify it. If those functions are correct and check the spec all other functions can depend on the spec being correct and don't need to check.

Do you realize that you are literally describing the core idea of "Parse, don't validate"‽

Of course the type system cannot encode the logic of departure before arrival or checksum match, but the type system will encode that those logics were upheld.

2

u/Steve_the_Stevedore Apr 08 '26

Do you realize that you are literally describing the core idea of "Parse, don't validate"‽

Yes, that was my point. Why does this surprise you?

Of course the type system cannot encode the logic of departure before arrival

This is not true.

type FlightInfo = {departureTime::DateTime, duration::TimeSpan}
arrivalTime::FlightInfo -> DateTime
arrivalTime fi = (departureTime fi) + (duration fi)

There. Creating a FlightInfo with a departure time after the arrival time is impossible to instantiate on a type level. You could do the same with uint or similar types in all kinds of languages. So of course the type system can encode this. The question is if you should do it, that was the entire point above. When /u/beders wrote:

There’s a much simpler alternative to all this craziness when you realize that you don’t need all these type guarantees everywhere.

You don't need to try to get the type system to protect against everything. For example you don't need to replace

interface User {
    id: number;
    email: string;
    age: number;
}

with

interface User {
    id: Id;
    email: UserEmail;
    age: UserAge;
}

Just use the first one and make sure it cannot be instantiated or modified without checks.

Of course the type system cannot encode the logic of departure before arrival or checksum match, but the type system will encode that those logics were upheld.

What do you mean by that? To me that's a contradiction. You are saying of course the type system cannot do that but the type system can do that. The whole point is that even if the type system can do it, it might still be better to ensure coherence in a different way.

2

u/pthierry Apr 08 '26

There. Creating a FlightInfo with a departure time after the arrival time is impossible to instantiate on a type level.

Fair point, if TimeSpan cannot be negative. This example was not a good one.

Yes, that was my point. Why does this surprise you?

Because it seems surprising to give an example when I asked for a counterexample…

Just use the first one and make sure it cannot be instantiated or modified without checks.

Except that if you have a User where email is just a string, then when you have a function that takes an email, not a user, either it gets a string without safety guarantee or it gets a ValidEmail and you need it anyway and then, why not use it in the first place?

3

u/Steve_the_Stevedore Apr 08 '26

Except that if you have a User where email is just a string, then when you have a function that takes an email, not a user, either it gets a string without safety guarantee or it gets a ValidEmail and you need it anyway and then, why not use it in the first place?

That is true. I mean you could get tricky with rows but that would probably be more complicated then just typing it out. I guess I didn't think that through. I change my mind. I'm with you on this: I think you should type it to the smallest type you are going to use as a function parameter.

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?

→ More replies (0)

1

u/[deleted] Apr 07 '26

[removed] — view removed comment

7

u/pthierry Apr 07 '26

Runtime validation or parsing are perfectly compatible with compile-time static types.

We all know that we cannot always encode business rules in types, and that "make invalid states unrepresentable" is not a rule, that sometimes the type system is not practical for that. I'm not sure what you mean by "weasel out" in this context.

3

u/binaryfireball Apr 08 '26

"but its no saaaaaaaafe" as they dull their knives