Live data from Hacker News

Can Types Replace Validation?

blog.ploeh.dk

1–10 of 35 posts

Re: Can Types Replace Validation?

#3
Another interesting idea I've read, I think in a Haskell context, is proofs attached to values.

You prove that a string originating from the user was HTML escaped - by calling the HTMLEscape function which attaches the proof - and then later in the code whenever you output it in the template it won't be escaped anymore because it's proven to be escaped.

Re: Can Types Replace Validation?

#4
post #3

Another interesting idea I've read, I think in a Haskell context, is proofs attached to values. You prove that a string originating from the user was HTML escaped - by calling the HTMLEscape function which attaches the proof - and then later in the code whenever you output it in the template it won't be escaped anymore because it's proven to be escaped.

I went down this rabbit hole 2 months ago and stumbled upon the Curry-Howard correspondence [1].

I don't understand why I find this stuff fascinating.

https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspon...

Re: Can Types Replace Validation?

#7
Incidently, there is a wave of popular python projects in dev python spheres that use types to define validation and serialization: fastapi, typer, etc.

Granted, the python typing system is very limited, and to get complete validation, you'll need more than types as python type hints can't express complex rules.

Also, as the articles mentions, the validation will occur at runtime, it can't in any way make sure the program is valid from mypy checks alone.

Still, it's really nice to define your input data types and contraints in one go.

Post reply on HN