There are libraries that do just this like https://effect.website/docs/guides/schema/introduction.
Appreciate the write up though! All too easy to rely on libraries without understanding what's under the hood.
Thanks for sharing, it was a good read.