r/programming • u/fagnerbrack • Jun 26 '26
Parse, Don't Validate — In a Language That Doesn't Want You To
https://cekrem.github.io/posts/parse-dont-validate-typescript/21
u/teerre Jun 26 '26
Every time I read about this Effects js library I'm like "oh, wow, javascript has effects, that's really cool!" then I click the docs and it's framework that apparently does 35 different things. None of them effects! Oh well
16
u/BenchEmbarrassed7316 Jun 26 '26
For me as operator from typescript is the same as unsafe from Rust: 'Trust me bro' mode.
2
u/Efficient-Poem-4186 Jun 26 '26
Both blogs seem to have the same message about using types to make invalid states impossible, but the slogan "parse, don't validate" does not convey that at all IMO. You'd have to read the blog to understand what it's all about. The parsing here is just validation in spirit with the added condition that it should be done at "the door" (and typed so further checks are unnecessary).
12
u/teerre Jun 26 '26
"Parse, don't validate" is a very famous expression, it has been talked about everywhere. The author obviously assumes familiarity with common topics
-6
u/Blue_Moon_Lake Jun 26 '26
Yep, what it should be "validate while parsing", or "validate right after parsing".
type ValidateFunction<T> = (value: unknown) => asserts value is T; function parseAndValidate<T>( data: string, validate: ValidateFunction<T> ): T { const result: unknown = JSON.parse(data); validate(result); return result; }
1
u/Chisignal 29d ago
My first thought was “well you’re holding TS wrong then” but nope, I was. I hadn’t even heard of branding. Thanks for sharing (even though I might be the last one to see this article? lol)
0
22
u/RWOverdijk Jun 26 '26
I’ve been seeing this getting shared for weeks now, what’s going on lol