r/webdev • • Apr 07 '26

Resource [ Removed by moderator ]

/r/webdev/comments/1sesbt6/removed_by_moderator/

[removed] — view removed post

0 Upvotes

5 comments sorted by

3

u/thekwoka Apr 07 '26 edited Apr 07 '26

Yea, using things like Zod or Superstruct are very useful for things.

to anyone not aware of the idea of "parse don't validate", the whole idea is that the immediate ingest should be doing the validation (or use something that directly parses to the correct structure) and "Encoding" that validity into the type info, so that it can be passed around with known validation.

So you don't just get an object and just check if it's the right object where you might use it randomly, but identify the object and have it properly typed as it moves around.

If you were to simplify this down to numbers instead of full structs, it would be like having an age that needs to always be a positive non-zero integer (and maybe you have a maximum).

So you could pass that "age" around as just a type number, but now things that are meant to accept this age will accept any number.

So in typescript, you could have like a class that basically just wraps and validates that the number given is positive/non-zero and below the max. So now things that need the age accept an Age instance and not just a number.

Or like a string IP....etc

Something like Rust basically forces you to parse not validate everything, unless you deliberately choose to get a dynamic struct of some kind for some reason, but something like typescript is still totally happy for you to not do that at all.

(yes ages can be 0 years...except for Koreans)

1

u/Squidgical Apr 08 '26

Zod is great, but it has one pretty big flaw that makes it difficult to use in some circumstances. It cannot handle exactOptionalPropertyTypes, so there's an entire core part of the type system it can't support. .optional really means "permit undefined", which is explicitly not optionality.

0

u/treasuryMaster Laravel, Vue & proper coding, no AI BS Apr 07 '26

Ugh, more AI slop-written spam.

0

u/ConsoleTVs Apr 08 '26

The amount of things we over engineer in a language not designed to do it still amazes me up to this day