Can I zod this gamestate?

export interface GameState {
id: string;
status: "initializing" | "waitingOpponent" | "started" | "finished";
field: Field;
entities: Entity[];
initiative: number[];
round: number;
turn: number;
hands: Card[][];
decks: Deck[];
}
export interface GameState {
id: string;
status: "initializing" | "waitingOpponent" | "started" | "finished";
field: Field;
entities: Entity[];
initiative: number[];
round: number;
turn: number;
hands: Card[][];
decks: Deck[];
}
It's a game. There is more nested interfaces (Field, Entity, Card, Deck). Should I just give up and use z.any() or is this still reasonable with zod? 😄
4 Replies
CaptainStarz
CaptainStarz•2y ago
If you can write interfaces, you surely can write as many zod schemas poohheh
cyremur
cyremurOP•2y ago
fine. meme question, I'll get around to it eventually was just hoping some zod wizards would show me the way to do this easier
CaptainStarz
CaptainStarz•2y ago
You can write schemas and infer types from them
Sean Cassiere
Sean Cassiere•2y ago
const GameSchema = z.object({
id: z.string(),
status: z.enum("initializing","waitingOpponent","started","finished"),
field: // your zod schema for this,
entities: z.array(/** you zod schema for this */),
initiative: z.array(z.number()),
round: z.number(),
turn: z.number(),
hands: z.array(z.array(/** you zod schema for this */)),
decks: z.array(/** you zod schema for this */)
});

export type TGameSchema = z.infer<typeof GameSchema>
const GameSchema = z.object({
id: z.string(),
status: z.enum("initializing","waitingOpponent","started","finished"),
field: // your zod schema for this,
entities: z.array(/** you zod schema for this */),
initiative: z.array(z.number()),
round: z.number(),
turn: z.number(),
hands: z.array(z.array(/** you zod schema for this */)),
decks: z.array(/** you zod schema for this */)
});

export type TGameSchema = z.infer<typeof GameSchema>
Want results from more Discord servers?
Add your server