import { z } from "zod/mini" import { WidgetKeySchema } from "../common/widgetKey" import type { GroupModel, NestedGroupModel } from "./group" import { GroupModelSchema, NestedGroupModelSchema } from "./group" import type { NestableModel } from "./nestable" import { NestableModelSchema } from "./nestable" // Simple (before 2020) export const LegacySliceModelSchema = z.discriminatedUnion("type", [ NestableModelSchema, NestedGroupModelSchema, ]) export type LegacySliceModel = NestableModel | NestedGroupModel // Composite (2020 to 2021) export const CompositeSliceType = "Slice" as const const CompositeSliceConfigSchema = z.object({ label: z.nullish(z.string()), }) export const CompositeSliceModelSchema = z.object({ type: z.literal(CompositeSliceType), fieldset: z.nullish(z.string()), description: z.optional(z.string()), icon: z.optional(z.string()), display: z.optional(z.string()), "non-repeat": z.optional(z.record(WidgetKeySchema, NestableModelSchema)), repeat: z.optional(z.record(WidgetKeySchema, NestableModelSchema)), config: z.optional(CompositeSliceConfigSchema), }) export type CompositeSliceModel = z.infer // Shared (2021 to present) export const SharedSliceType = "SharedSlice" as const const IMAGE_PLACEHOLDER_URL = "https://images.prismic.io/slice-machine/621a5ec4-0387-4bc5-9860-2dd46cbc07cd_default_ss.png?auto=compress,format" export const SharedSliceModelVariationSchema = z.object({ id: z.string(), name: z.string(), description: z.string(), imageUrl: z._default(z.string(), IMAGE_PLACEHOLDER_URL), docURL: z.string(), version: z.string(), display: z.optional(z.string()), primary: z.optional( z.record( z.string(), z.discriminatedUnion("type", [NestableModelSchema, GroupModelSchema]) as z.ZodMiniType< NestableModel | GroupModel >, ), ), items: z.optional(z.record(z.string(), NestableModelSchema)), }) export type SharedSliceModelVariation = z.infer export const SharedSliceModelSchema = z.object({ id: z.string(), type: z.literal(SharedSliceType), name: z.string(), variations: z.array(SharedSliceModelVariationSchema), description: z.optional(z.string()), legacyPaths: z.optional(z.record(z.string(), z.string())), }) export type SharedSliceModel = z.infer export const SharedSliceRefModelSchema = z.object({ type: z.literal(SharedSliceType), }) export type SharedSliceRefModel = z.infer // Used to represent a shared slice content which can only be // of a given shared slice variation type. export const SharedSliceVariationContentModelSchema = z.object({ type: z.literal(SharedSliceType), sliceName: z.string(), variationID: z.string(), fields: z.pick(SharedSliceModelVariationSchema, { primary: true, items: true }), }) export type SharedSliceVariationContentModel = z.infer< typeof SharedSliceVariationContentModelSchema > // All // Used when slices reference SharedSlice by ref (not full definition) export const DynamicSliceModelSchema: z.ZodMiniType = z.discriminatedUnion( "type", [CompositeSliceModelSchema, LegacySliceModelSchema, SharedSliceRefModelSchema], ) export type DynamicSliceModel = CompositeSliceModel | LegacySliceModel | SharedSliceRefModel // Used when slices include full SharedSlice definition export const StaticSliceModelSchema: z.ZodMiniType = z.discriminatedUnion( "type", [CompositeSliceModelSchema, LegacySliceModelSchema, SharedSliceModelSchema], ) export type StaticSliceModel = CompositeSliceModel | LegacySliceModel | SharedSliceModel // Used to represent a slice content model as shared slice variations // can only be of a given shared slice variation type. export const SliceContentModelSchema: z.ZodMiniType = z.discriminatedUnion( "type", [CompositeSliceModelSchema, LegacySliceModelSchema, SharedSliceVariationContentModelSchema], ) export type SliceContentModel = | CompositeSliceModel | LegacySliceModel | SharedSliceVariationContentModel // Model types export const SliceModelTypeSchema = z.union([ z.literal(CompositeSliceType), z.literal(SharedSliceType), ]) export type SliceModelType = z.infer