json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

What are some use cases for this library?

Open tlahav opened this issue 3 years ago • 3 comments

Hi everyone, I have worked a bit with JSON schema to generate UI, validate models with AJV, and OpenAPI generators. I found this library while looking for projects to contribute to, but i cant wrap my head around its use-cases.

In the context of Node-world, why would I take a JSON schema, something I can deliver anywhere, then transpile it into typings that are only available during develop time, only for them to transpile back to type-less JavaScript? Is this purely for Deno environments?

Not trying to step on any toes, just trying to understand.
Thank you!

tlahav avatar Sep 16 '22 17:09 tlahav

I can document an API or object structure in a schema, which is authoritative. I can share that independent of platform, and anybody can use it. Then I can convert that schema to a type that I can work with in my actual code that implements or otherwise interacts with that API or object.

Perlkonig avatar Sep 24 '22 20:09 Perlkonig

@Perlkonig Thanks for the reply. Do you use this library during dev-time then? pull the JSON files using a script and and update local files? Or during build-time where they're pulled then unit tests verify that your local code still works with the new schemas?

tlahav avatar Oct 17 '22 13:10 tlahav

I use JSON schema as a platform-independent way of defining potentially complex APIs. This library lets me take a schema and use it in any Typescript tooling, whether a client or a server. Yes it is a development-time tool. At runtime I'm using things like Ajv to validate and work with actual JSON being handed around.

Examples:

  • Renderer: This is a library that uses JSON objects to define a game board. Game code, potentially written by anybody, has to produce board representations that adhere to this schema. The schema is authoritative and can guide anybody coding in any language. json-schema-to-typescript lets me use that schema both in the Typescript game code I write to generate well-formed representations and in the consumer library to generate the resulting SVG (playground here).

  • Full Thrust Ship Library: This library describes a schema for a ship in a tabletop space combat game called Full Thrust. The schema is authoritative and available for anybody to use. I use json-schema-to-typescript to generate the final d.ts file, which is part of the library dist package. I consume it in the ship builder, for example, but I will also be using it in other tools I'm developing. If somebody wanted to write their own compatible ship builder, they can use that schema as a platform-independent guide. They don't need json-schema-to-typescript. But if you do want to work in TypeScript, then this tool is invaluable.

I hope that helps.

Perlkonig avatar Oct 17 '22 14:10 Perlkonig

The main usecase is it allows shared interfaces between backend and frontend, which almost eliminates the "backend changed the output of an API endpoint which causes error on frontend in runtime" class of problems (almost because it's possible to change the schema in incompatible way while retaining the same type signature). It will just not build and the compiler will point at exact problematic files. Reduces a lot of headaches (which would otherwise require e2e tests to debug) when doing API refactors. As a bonus these interfaces can be freely extended/implemented within the code without spilling this logic on serializable surface.

GabenGar avatar Dec 14 '22 14:12 GabenGar