Giter Club home page Giter Club logo

types's People

Contributors

kvz avatar nickrttn avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

types's Issues

Discriminated unions in Zod

We would like to use Zod to define schemas for our Robots, Assembly and Template constructs, and more to come. The schemas can then be used by our customers and us to validate and type these types of data and later on by us to build editor completions and linting in our user dashboard.

In order to do that correctly, we need two features in Zod that are currently missing:

  • the usage of intersections in discriminated unions
  • the nesting of discriminated unions in discriminated unions

Both have been reported/requested by other users in the Zod repo in colinhacks/zod#1075 and colinhacks/zod#1618

In this repo, the schema that is currently breaking in particular is the robotSchema, caused by the intersections here:

types/src/robots/index.ts

Lines 180 to 183 in cecd12c

videoConcatRobotSchema,
videoEncodeRobotSchema,
videoMergeRobotSchema,
videoSubtitleRobotSchema,

These four schemas are (should be) intersections of z.object and z.discriminatedUnion. They themselves are used in the robotSchema mentioned above, which is also a z.discriminatedUnion.

A simplified example of what we want to achieve type-wise is this:

type Robots =
  | { robot: "/upload/handle" }
  | {
      robot: "/s3/store"
      use: string
      key: string
      bucket: string
    }
  | ({ robot: "/video/encode"; use: string; preset: string; result?: boolean } & (
      | { ffmpeg_stack: "v3.3.3"; preset: "foo" }
      | { ffmpeg_stack: "v4.3.1"; preset: "foo" | "bar" }
    ))

type Assembly = {
  steps: Record<string, Robots & { result?: boolean }>
}

The discriminators here are robot and ffmpeg_stack (and we might have other discriminators nested in individual Robots as well). The /video/encode robot is an intersection of an object and a discriminated union.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.