Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some thoughts #124

Open
dvv opened this issue Mar 27, 2023 · 2 comments
Open

Some thoughts #124

dvv opened this issue Mar 27, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@dvv
Copy link

dvv commented Mar 27, 2023

Feature description

I would like to discuss whether the following features are feasible to have in the mainstream:

jsdoc tag to apply .coerce to the schema

Input

  /**
   * @coerce true
   */
  type MaybeInt = number

Output

  const maybeIntSchema = z.coerce.number()

NB: may be generalized as the following

jsdoc tag override schema definition

Input

  /**
   * @schema z.coerce.number().int().refine(x => x % 2)
   */
  type MaybeOddInt = number

Output

  const maybeOddIntSchema = z.coerce.number().int().refine(x => x % 2)

support z.enum() for simple text enums

Input

  type LoggerLevel = "debug" | "info" | "warning" | "error" | "critical"

Output

  const loggerLevelSchema = z.enum(["debug", "info", "warning", "error", "critical"] as const)

jsdoc tag for discriminatedUnion

Input

  export enum LoggerKind { console, file }
  interface ILogger {
    kind: LoggerKind
  }
  interface LoggerConsole extends ILogger {
    kind: LoggerKind.console
  }
  interface LoggerFile extends ILogger {
    kind: LoggerKind.file
    filename: string
  }
  /**
   * @tag kind
   */
  // NB: treat the following as discriminatedUnion
  export type Logger = LoggerConsole | LoggerFile

NB: may be better to directly mark ILogger field as discriminator?

Output

  const iLoggerSchema = z.object({
    kind: loggerKindSchema,
  });

  const loggerConsoleSchema = iLoggerSchema.extend({
    kind: z.literal(LoggerKind.console),
  });

  const loggerFileSchema = iLoggerSchema.extend({
    kind: z.literal(LoggerKind.file),
    filename: z.string(),
  });

  export const loggerSchema = z.discriminatedUnion("kind", [loggerConsole, loggerFile])

support Deno style imports (".ts")

Output

  // Generated by ts-to-zod
  import { z } from "zod.ts";
  import { LoggerLevel } from "./../proto/src.ts";

support output file header template

Say, via a --header file.ts command line option

Output

  // This is a custom header
  import { z } from "../deps.ts";
  // Generated by ts-to-zod
  ... here goes the compilation result

TIA

@dvv dvv mentioned this issue Mar 30, 2023
@t-animal
Copy link

support z.enum() for simple text enums

For these scenarios I personally prefer doing it like this:

export const LoggerLevels = [ "debug", "info", "warning", "error", "critical"] as const;
export type LoggerLevel = typeof LoggerLevels[number];

because this bridges the gap between runtime and static type system nicely. Unfortunately it's also not supported by ts-to-zod.

@fabien0102
Copy link
Owner

A lot of amazing ideas over here! I'm laking a bit of time right now to work on this project, but I'm keeping an eye on this 😃

@tvillaren tvillaren added the enhancement New feature or request label Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants