Earlier quoted context omitted.
You can generate JSON Schema from TS with this library: https://github.com/YousefED/typescript-json-schema It's certainly conceivable to build an equivalent for Zod but you'll eventually want to validate types that aren't expressible in TS (i.e. Integer).
This looks like it generates JSON Schema, but not runtime validators?
Designing the perfect TypeScript schema validation library
51–60 of 77 posts
Re: Designing the perfect TypeScript schema validation library
#52Having the JSON Schema intermediate is useful when using Fastify [2], so that request validation can happen with less boilerplate.
[1] https://github.com/sinclairzx81/typebox
[2] https://www.fastify.io/docs/latest/Validation-and-Serializat...
Re: Designing the perfect TypeScript schema validation library
#53Earlier quoted context omitted.
Yeah, that's really something that feels like it should be a part of the language, because it would be so useful!
It's explicitly one of the language's non-goals[1]. > Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata. [1] https://github.com/Microsoft/TypeScript/wiki/TypeScript-Desi...
Re: Designing the perfect TypeScript schema validation library
#54My library `@hediet/cli` [1] uses this technology to offer a browser based UI for CLI applications [2]. In the long term, I want to replace my use of `io-ts` with this technology in my JSON RPC implementation [3], so that I can implement something similar to Swagger for JSON RPC.
I always felt the possibilities of JSON Schema are not really explored, which I think might be due to bad design of JSON Schema (it's really hard to consider all these denormalizations, it's like HTML 20 years ago).
I'll have a more detailed look at your library once I find time!
[1] https://github.com/hediet/ts-cli/blob/master/cli/README.md [2] https://github.com/hediet/ts-cli/blob/master/cli/docs/gui.pn... [3] https://github.com/hediet/typed-json-rpc
Re: Designing the perfect TypeScript schema validation library
#55Earlier quoted context omitted.
I personally prefer to standardize everything as a function. It also leaves some breathing room in case I decide to include parameters as part of a future API augmentation.
I hope you'll give me the opportunity to try to convince you otherwise. I understand the context of this is that io-ts puts an onerous emphasis on FP concepts and data structures. But FP principles , when applied in an effective and usable way, are quite good. The make it easier to reason about and maintain code. One such principle is that a "function" with no parameters is a good sign that it will cause some kind of…
A zero-argument function is either a constant, or a side-effect. Given we don’t want a side-effect, exposing a constant instead of an effectful-looking function is preferable.
Re: Designing the perfect TypeScript schema validation library
#56You mention creating object types with optional keys is cumbersome in io-ts. How is that solved in zod, exactly? What allows you to map `foo: union([bar, undefined])` to `foo?: bar | undefined` (note the question mark on the left hand side)? There’s nothing in the declaration to give away why this wouldn’t yield `foo: bar | undefined` which is what I believe you’d get out of io-ts. Looks useful - I would have an easi…
Good question! It wasn't easy to get the question mark on the left-hand side, but it is possible. Here's the Zod equivalent: const C = z.object({ foo: z.string(), bar: z.number().optional(), }); type C = t.TypeOf ; /* { foo: string; bar?: number | undefined } */ And here's the code that pulls this off: type OptionalKeys = { [k in keyof T]: undefined extends T[k]['_type'] ? k : never; }[keyof T]; type RequiredKeys = E…
FWIW it’s made my life easier to say the keys will always be there, but the values are possibly undefined. Less room for ambiguous interpretation.
Re: Designing the perfect TypeScript schema validation library
#57TypeScript seems designed to be incrementally added to large existing projects. Why then isn't there a standard way to validate objects coming from the untyped parts of your project?
Re: Designing the perfect TypeScript schema validation library
#58Re: Designing the perfect TypeScript schema validation library
#59In these cases I'd far rather see an old fashioned reliable server-side application built in a language with a lot of built in safety. The less you ask the client to do, the more reliable the application.
Re: Designing the perfect TypeScript schema validation library
#60Also, and perhaps most importantly, this library has no respect for undefined, because particularly in the context of data modeling, undefined is complete nonsense.