leaky-ships/leaky-ships/lib/zodSchemas.ts

66 lines
1.6 KiB
TypeScript

import { GameState, MoveType, Orientation } from "@prisma/client"
import { z } from "zod"
export const PlayerSchema = z
.object({
id: z.string(),
name: z.string().nullable(),
index: z.number(),
chats: z
.object({
id: z.string(),
event: z.string().nullable(),
message: z.string().nullable(),
createdAt: z.coerce.date(),
})
.array(),
moves: z
.object({
id: z.string(),
index: z.number(),
action: z.nativeEnum(MoveType),
x: z.number(),
y: z.number(),
})
.array(),
ships: z
.object({
size: z.number(),
variant: z.number(),
x: z.number(),
y: z.number(),
orientation: z.nativeEnum(Orientation),
})
.array(),
})
.nullable()
export type PlayerSchema = z.infer<typeof PlayerSchema>
export const CreateSchema = z.object({
game: z
.object({
id: z.string(),
state: z.nativeEnum(GameState),
allowSpectators: z.boolean(),
allowSpecials: z.boolean(),
allowChat: z.boolean(),
allowMarkDraw: z.boolean(),
})
.nullable(),
gamePin: z.string().nullable(),
users: PlayerSchema.array(),
activeIndex: z.number().optional(),
})
export const GamePropsSchema = z.object({
payload: CreateSchema,
hash: z.string(),
})
export const optionalGamePropsSchema = z.object({
payload: CreateSchema.nullable(),
hash: z.string().nullable(),
})
export type GamePropsSchema = z.infer<typeof GamePropsSchema>
export type optionalGamePropsSchema = z.infer<typeof optionalGamePropsSchema>