import { z } from "zod/mini" import { WidgetKeySchema } from "../common/widgetKey" import { StaticSliceModelSchema, DynamicSliceModelSchema } from "./slice" export const LegacySlicesFieldType = "Choice" export const SlicesFieldType = "Slices" // Label configuration for slices const SlicesLabelSchema = z.object({ name: z.string(), display: z.optional(z.string()), }) export const SlicesLabelsSchema = z.union([ z.record(z.string(), z.array(SlicesLabelSchema)), z.null(), ]) // Config const getSlicesConfigSchema = < TSliceSchema extends typeof StaticSliceModelSchema | typeof DynamicSliceModelSchema, >( sliceSchema: TSliceSchema, ) => z.object({ label: z.nullish(z.string()), labels: z.optional(SlicesLabelsSchema), choices: z.optional(z.record(WidgetKeySchema, sliceSchema)), }) const StaticSlicesConfigSchema = getSlicesConfigSchema(StaticSliceModelSchema) const DynamicSlicesConfigSchema = getSlicesConfigSchema(DynamicSliceModelSchema) // Model const getSlicesSchema = < TConfig extends typeof StaticSlicesConfigSchema | typeof DynamicSlicesConfigSchema, >( configSchema: TConfig, ) => z.object({ type: z.union([z.literal(SlicesFieldType), z.literal(LegacySlicesFieldType)]), fieldset: z.nullish(z.string()), config: z.optional(configSchema), }) export const StaticSlicesModelSchema = getSlicesSchema(StaticSlicesConfigSchema) export type StaticSlicesModel = z.infer export const DynamicSlicesModelSchema = getSlicesSchema(DynamicSlicesConfigSchema) export type DynamicSlicesModel = z.infer