import { z } from "zod/mini" import { WidgetKeySchema } from "../common/widgetKey" import type { NestableModel } from "./nestable" import { NestableModelSchema } from "./nestable" export const GroupFieldType = "Group" // Factory to create group config schema with configurable widget types const createGroupConfigSchema = (widgets: TWidgets) => z.object({ label: z.nullish(z.string()), repeat: z.optional(z.boolean()), fields: z.optional(z.record(WidgetKeySchema, widgets)), }) // Factory to create group schema with configurable widget types const createGroupSchema = (widgets: TWidgets) => z.object({ type: z.literal(GroupFieldType), fieldset: z.nullish(z.string()), icon: z.optional(z.string()), description: z.optional(z.string()), config: z.optional(createGroupConfigSchema(widgets)), }) // NestedGroup - only allows Nestable in fields (no further nesting) export const NestedGroupModelSchema = createGroupSchema(NestableModelSchema) export type NestedGroupModel = z.infer // Group - allows Nestable | NestedGroup in fields (max 2 levels of nesting) export const GroupModelSchema = createGroupSchema( z.discriminatedUnion("type", [NestableModelSchema, NestedGroupModelSchema]) as z.ZodMiniType< NestableModel | NestedGroupModel >, ) export type GroupModel = z.infer