A daily game

Improve preferences validation

+12 -3
+12 -3
src/components/game/preferences-form.tsx
··· 11 } from "../ui/form"; 12 import { Input } from "../ui/input"; 13 14 type FormData = z.infer<typeof formSchema>; 15 const formSchema = z.object({ 16 - width: z.coerce.number<number>(), 17 - height: z.coerce.number<number>(), 18 - variants: z.array(z.string()).min(2).max(4), 19 }); 20 21 interface Props {
··· 11 } from "../ui/form"; 12 import { Input } from "../ui/input"; 13 14 + const oneEmoji = z 15 + .string() 16 + .refine((val) => val.length === 0 || [...val].length === 1, { 17 + error: "Please enter a single emoji", 18 + }); 19 + 20 + const gridDimension = (dir: "row" | "column") => 21 + z.coerce.number<number>().min(2, { error: `Minimum 2 ${dir}s` }); 22 + 23 type FormData = z.infer<typeof formSchema>; 24 const formSchema = z.object({ 25 + width: gridDimension("column"), 26 + height: gridDimension("row"), 27 + variants: z.array(oneEmoji).min(2).max(4), 28 }); 29 30 interface Props {