Ultra-minimal JSON schemas with TypeScript inference

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • sweet.js

    Sweeten your JavaScript.

  • Sweetjs is unfortunately dead [0] for like 5 years now.

    It also doesn't have any typescript awareness which is required to build this kind of functionality - you want to have static type introspection available in macros so you can generate code based on provided types.

    [0] https://github.com/sweet-js/sweet-core/graphs/contributors

  • spartan-schema

    Ultra-minimal JSON schemas with Typescript inference

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • as-typed

    Static TypeScript types from a literal JSONSchema type

  • swagger-ui

    Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.

  • Compared to a struct in C, Rust, or Golang it takes more typing on behalf of the programmer and only works in a narrowly specific set of circumstances.

    Convenient in some ways (e.g. if you only are comfortable in JS), and does nothing for other contexts.

    https://swagger.io/

    Is this more than a subset of Wordnik's Swagger?

  • Paste JSON as Code • quicktype

    Xcode extension to paste JSON as Swift, Objective-C, and more

  • This looks really nice as an alternative to JSON schema, which is horribly verbose. Like using RELAX NG compact syntax for XML schemas back in the day, versus horrible verbose DTDs or XSDs.

    But I think the most natural way to express JSON schemas would be TypeScript type declarations. Is there a project that can take a TS type and generate a runtime parser/validator for it?

    I thought maybe Quicktype (https://quicktype.io/) was that, but it looks like it only takes JSON as input, not TS types.

  • ts-to-zod

    Generate zod schemas from typescript types/interfaces

  • YES! I've introduced ts-to-zod into my codebase only yesterday, and it's served my needs exactly. It doesn't handle generics or mapped types, but that's a fair price to pay.

    It also works the other way, you can define a zod schema and you can infer types for your data from it.

    https://github.com/fabien0102/ts-to-zod

  • ts-simple-type

    Relationship type checker functions for Typescript types.

  • After some frustration with the TypeScript schema library ecosystem, I've decided that I'd prefer to declare my types using TypeScript's excellent type syntax, so I can take advantage of generics, mapped types, etc. Then, I'll take those TypeScript types and compile them to whatever alternative schema format I need.

    There are many libraries that claim to convert your Typescript types to other formats, such as ts-json-schema-generator, ts-to-zod, or typeconv/core-types-ts. These libraries work by interpreting the Typescript AST, essentially re-implementing a bare-bones type system from scratch. Most do not support advanced Typescript features like generic application, mapped types, or string literal types. So, what's the point? To avoid those limitations, I use Typescript's first-party ts.TypeChecker API to analyze types, and an existing library called ts-simple-type (which I've forked) to convert from ts.Type to a more usable intermediate format. Then, I recurse over the intermediate format and emit "AST nodes". It's pretty simple, but seems promising.

    So far, I have compilers from TypeScript type to Python 3 and Thrift. But I plan to add OpenAPI/JSONSchema, Protobuf (Proto3), Kotlin, Swift, and maybe Zod and Avro. Each target language is around ~300 LoC so they're pretty easy to put together.

    Repo: https://github.com/justjake/ts-simple-type

    Compiler input and output: https://github.com/justjake/ts-simple-type/blob/jake--compil...

    Thrift compiler: https://github.com/justjake/ts-simple-type/blob/jake--compil...

    Python compiler: https://github.com/justjake/ts-simple-type/blob/jake--compil...

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • ajv

    The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)

  • typebox

    Json Schema Type Builder with Static Type Resolution for TypeScript

  • ```

    The main thing we're using it for now, is defining an OpenAPI schema and using the types in the front-end and backend, and using the schema to validate requests and responses, both client-side as well as server-side. Validation is done using avj [1].

    We used Joi before and want to replace that code with JSON Schema, but that is quite the hassle. I like that TypeBox uses JSON Schema underneath, so migrating to another library should be easy.

    [0] https://github.com/sinclairzx81/typebox

  • marshal.ts

    A new full-featured and high-performance TypeScript framework

  • Deepkit is quite interesting in this area: https://deepkit.io/

    It reimplements the type system as… a stack based VM that expands the TS typedefs if needed at runtime?

    I would use it if the risk factor wasn’t so high. I’m too scared of needing to maintain their crazy cool stuff to use it.

    I would love Microsoft to build those APIs first party and officially support them.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts