Skip to content

Instantly share code, notes, and snippets.

@vpontis
Created April 11, 2025 22:06
Show Gist options
  • Save vpontis/0b342980d327598954fb2198fd08da28 to your computer and use it in GitHub Desktop.
Save vpontis/0b342980d327598954fb2198fd08da28 to your computer and use it in GitHub Desktop.
import { z } from "zod";
const ConfigBase = z.object({
name: z.string(),
});
const FreeConfig = ConfigBase.extend(
z.object({
type: z.literal("free"),
min_cents: z.null(),
}),
);
const PricedConfig = ConfigBase.extend(
z.object({
type: z.literal("fiat-price"),
min_cents: z.int().nullable(),
}),
);
const Config = z.discriminatedUnion("type", [FreeConfig, PricedConfig]);
const zodTest = async () => {
Config.parse({
min_cents: null,
type: "fiat-price",
name: "Standard",
});
console.log("Exiting");
process.exit(0);
};
zodTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment