Created
April 11, 2025 22:06
-
-
Save vpontis/0b342980d327598954fb2198fd08da28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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