From 3fe8aacfd6685c0d322e4b2799c18f7514be0903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20Se=CC=81bastien?= Date: Fri, 17 Jan 2025 13:58:55 -0800 Subject: [PATCH] fix(schema): when it's not an input content and config are not having a key --- components/schema/package.json | 2 +- .../src/pim/components/component-config.ts | 155 +++++++++++------- .../src/pim/components/component-content.ts | 153 ++++++++++------- 3 files changed, 188 insertions(+), 122 deletions(-) diff --git a/components/schema/package.json b/components/schema/package.json index c8d4bc22..e9753812 100644 --- a/components/schema/package.json +++ b/components/schema/package.json @@ -1,6 +1,6 @@ { "name": "@crystallize/schema", - "version": "3.0.3", + "version": "3.1.0", "license": "MIT", "exports": { "./pim": { diff --git a/components/schema/src/pim/components/component-config.ts b/components/schema/src/pim/components/component-config.ts index 90f28080..2eab14df 100644 --- a/components/schema/src/pim/components/component-config.ts +++ b/components/schema/src/pim/components/component-config.ts @@ -14,52 +14,70 @@ import { FilesConfig, FilesConfigSchema } from './types/files.js'; import { ItemRelationsConfig, ItemRelationsConfigSchema } from './types/item-relations.js'; import { PropertiesTableConfig, PropertiesTableConfigSchema } from './types/properties-table.js'; import { SelectionConfig, SelectionConfigSchema } from './types/selection.js'; -import { GenericComponentConfigSchema } from './shared.js'; -import { ComponentDefinitionInputSchema, ComponentDefinitionSchema } from './component-definition.js'; +import { GenericComponentConfig, GenericComponentConfigSchema } from './shared.js'; +import { ComponentDefinition, ComponentDefinitionSchema } from './component-definition.js'; -export type NestableComponentConfig = { - files?: FilesConfig; - images?: ImagesConfig; - videos?: VideosConfig; - boolean?: BooleanConfig; - datetime?: DatetimeConfig; - gridRelations?: GridRelationsConfig; - itemRelations?: ItemRelationsConfig; - location?: LocationConfig; - numeric?: NumericConfig; - paragraphCollection?: ParagraphCollectionConfig; - propertiesTable?: PropertiesTableConfig; - richText?: RichTextConfig; - selection?: SelectionConfig; - singleLine?: SingleLineConfig; - piece?: PieceConfig; -}; +export type NestableComponentConfig = + | FilesConfig + | ImagesConfig + | VideosConfig + | BooleanConfig + | DatetimeConfig + | GridRelationsConfig + | ItemRelationsConfig + | LocationConfig + | NumericConfig + | ParagraphCollectionConfig + | PropertiesTableConfig + | RichTextConfig + | SelectionConfig + | SingleLineConfig + + // PieceConfig | + | (GenericComponentConfig & { + identifier: string; + components: ComponentDefinition[]; + }); export const NestableComponentConfigSchema: z.ZodType = z.lazy(() => - z.object({ - files: FilesConfigSchema.optional(), - images: ImagesConfigSchema.optional(), - videos: VideosConfigSchema.optional(), - boolean: BooleanConfigSchema.optional(), - datetime: DatetimeConfigSchema.optional(), - gridRelations: GridRelationsConfigSchema.optional(), - itemRelations: ItemRelationsConfigSchema.optional(), - location: LocationConfigSchema.optional(), - numeric: NumericConfigSchema.optional(), - paragraphCollection: ParagraphCollectionConfigSchema.optional(), - propertiesTable: PropertiesTableConfigSchema.optional(), - richText: RichTextConfigSchema.optional(), - selection: SelectionConfigSchema.optional(), - singleLine: SingleLineConfigSchema.optional(), - piece: PieceConfigSchema.optional(), - }), + z.union([ + FilesConfigSchema, + ImagesConfigSchema, + VideosConfigSchema, + BooleanConfigSchema, + DatetimeConfigSchema, + GridRelationsConfigSchema, + ItemRelationsConfigSchema, + LocationConfigSchema, + NumericConfigSchema, + ParagraphCollectionConfigSchema, + PropertiesTableConfigSchema, + RichTextConfigSchema, + SelectionConfigSchema, + SingleLineConfigSchema, + GenericComponentConfigSchema.extend({ + identifier: z.string().min(1), + components: z.array(ComponentDefinitionSchema), + }), + ]), ); -export type ComponentConfig = NestableComponentConfig & { - componentChoice?: ChoiceConfig; - componentMultipleChoice?: MultipleChoicesConfig; - contentChunk?: ChunksConfig; -}; +export type ComponentConfig = + | NestableComponentConfig + // ChoiceConfig + | (GenericComponentConfig & { + choices: ComponentDefinition[]; + }) + // MultipleChoicesConfig + | (GenericComponentConfig & { + choices: ComponentDefinition[]; + allowDuplicates: boolean; + }) + // ChunksConfig + | (GenericComponentConfig & { + components: ComponentDefinition[]; + repeatable: boolean; + }); /* * Hoisting Nightmare debug prevention @@ -69,28 +87,47 @@ export type ComponentConfig = NestableComponentConfig & { * Also we have to duplicate the schema here because of lazy loading */ export const ComponentConfigSchema: z.ZodType = z.lazy(() => - NestableComponentConfigSchema.and( - z.object({ - // componentChoice: ChoiceConfigSchema.optional(), - componentChoice: GenericComponentConfigSchema.extend({ - choices: z.array(ComponentDefinitionInputSchema), - }).optional(), - // componentMultipleChoice: MultipleChoicesConfigSchema.optional(), - componentMultipleChoice: GenericComponentConfigSchema.extend({ - choices: z.array(ComponentDefinitionSchema), - allowDuplicates: z.boolean().optional(), - }).optional(), - // contentChunk: ChunksConfigSchema.optional(), - contentChunk: GenericComponentConfigSchema.extend({ - components: z.array(ComponentDefinitionSchema), - repeatable: z.boolean(), - }).optional(), + z.union([ + FilesConfigSchema, + ImagesConfigSchema, + VideosConfigSchema, + BooleanConfigSchema, + DatetimeConfigSchema, + GridRelationsConfigSchema, + ItemRelationsConfigSchema, + LocationConfigSchema, + NumericConfigSchema, + ParagraphCollectionConfigSchema, + PropertiesTableConfigSchema, + RichTextConfigSchema, + SelectionConfigSchema, + SingleLineConfigSchema, + // Piece + GenericComponentConfigSchema.extend({ + identifier: z.string().min(1), + components: z.array(ComponentDefinitionSchema), + }), + // Choice + GenericComponentConfigSchema.extend({ + choices: z.array(ComponentDefinitionSchema), + }), + + // Multiple Choices + GenericComponentConfigSchema.extend({ + choices: z.array(ComponentDefinitionSchema), + allowDuplicates: z.boolean().optional(), + }), + + //Chnunk + GenericComponentConfigSchema.extend({ + components: z.array(ComponentDefinitionSchema), + repeatable: z.boolean(), }), - ), + ]), ); export const ChoiceConfigSchema = GenericComponentConfigSchema.extend({ - choices: z.array(ComponentDefinitionInputSchema), + choices: z.array(ComponentDefinitionSchema), }); export type ChoiceConfig = z.infer; diff --git a/components/schema/src/pim/components/component-content.ts b/components/schema/src/pim/components/component-content.ts index cde53bf7..55fd40d5 100644 --- a/components/schema/src/pim/components/component-content.ts +++ b/components/schema/src/pim/components/component-content.ts @@ -14,54 +14,66 @@ import { FilesContent, FilesContentSchema } from './types/files.js'; import { ItemRelationsContent, ItemRelationsContentSchema } from './types/item-relations.js'; import { PropertiesTableContent, PropertiesTableContentSchema } from './types/properties-table.js'; import { SelectionContent, SelectionContentSchema } from './types/selection.js'; -import { ComponentSchema } from './component.js'; +import { Component, ComponentSchema } from './component.js'; +import { ComponentType, ComponentTypeEnum } from '../../shared/index.js'; -export type NestableComponentContent = { +type IComponent = { componentId: string; - files?: FilesContent; - images?: ImagesContent; - videos?: VideosContent; - boolean?: BooleanContent; - datetime?: DatetimeContent; - gridRelations?: GridRelationsContent; - itemRelations?: ItemRelationsContent; - location?: LocationContent; - numeric?: NumericContent; - paragraphCollection?: ParagraphCollectionContent; - propertiesTable?: PropertiesTableContent; - richText?: RichTextContent; - selection?: SelectionContent; - singleLine?: SingleLineContent; - piece?: PieceContent; + name: string; + type: ComponentType; + content: ComponentContent; }; +export type NestableComponentContent = + | FilesContent + | ImagesContent + | VideosContent + | BooleanContent + | DatetimeContent + | GridRelationsContent + | ItemRelationsContent + | LocationContent + | NumericContent + | ParagraphCollectionContent + | PropertiesTableContent + | RichTextContent + | SelectionContent + | SingleLineContent + // PieceContent | + | { + identifier: string; + components: IComponent[]; + }; export const NestableComponentContentSchema: z.ZodType = z.lazy(() => - z.object({ - componentId: z.string().min(1), - files: FilesContentSchema.optional(), - images: ImagesContentSchema.optional(), - videos: VideosContentSchema.optional(), - boolean: BooleanContentSchema.optional(), - datetime: DatetimeContentSchema.optional(), - gridRelations: GridRelationsContentSchema.optional(), - itemRelations: ItemRelationsContentSchema.optional(), - location: LocationContentSchema.optional(), - numeric: NumericContentSchema.optional(), - paragraphCollection: ParagraphCollectionContentSchema.optional(), - propertiesTable: PropertiesTableContentSchema.optional(), - richText: RichTextContentSchema.optional(), - selection: SelectionContentSchema.optional(), - singleLine: SingleLineContentSchema.optional(), - piece: PieceContentSchema.optional(), - }), + z.union([ + FilesContentSchema, + ImagesContentSchema, + VideosContentSchema, + BooleanContentSchema, + DatetimeContentSchema, + GridRelationsContentSchema, + ItemRelationsContentSchema, + LocationContentSchema, + NumericContentSchema, + ParagraphCollectionContentSchema, + PropertiesTableContentSchema, + RichTextContentSchema, + SelectionContentSchema, + SingleLineContentSchema, + z.object({ + identifier: z.string().min(1), + components: z.array( + z.object({ + componentId: z.string().min(1), + name: z.string().min(1), + type: ComponentTypeEnum, + content: ComponentContentSchema, + }), + ), + }), + ]), ); -export type ComponentContent = NestableComponentContent & { - componentChoice?: ChoiceContent; - componentMultipleChoice?: MultipleChoicesContent; - contentChunk?: ChunksContent; -}; - /* * Hoisting Nightmare debug prevention * Do not try to put those files elsewhere, because base on how you transpile the code, it may not work @@ -69,30 +81,47 @@ export type ComponentContent = NestableComponentContent & { * * Also we have to duplicate the schema here because of lazy loading */ - +export type ComponentContent = + | NestableComponentContent + | { + selectedComponent: IComponent; + } + | { + selectedComponents: IComponent[]; + } + | { + chunks: IComponent[][]; + }; export const ComponentContentSchema: z.ZodType = z.lazy(() => - NestableComponentContentSchema.and( + z.union([ + FilesContentSchema, + ImagesContentSchema, + VideosContentSchema, + BooleanContentSchema, + DatetimeContentSchema, + GridRelationsContentSchema, + ItemRelationsContentSchema, + LocationContentSchema, + NumericContentSchema, + ParagraphCollectionContentSchema, + PropertiesTableContentSchema, + RichTextContentSchema, + SelectionContentSchema, + SingleLineContentSchema, + z.object({ + identifier: z.string().min(1), + components: z.array(ComponentSchema), + }), + z.object({ + selectedComponent: ComponentSchema, + }), + z.object({ + selectedComponents: z.array(ComponentSchema), + }), z.object({ - // componentChoice: ChoiceContentSchema.optional(), - componentChoice: z - .object({ - selectedComponent: ComponentSchema, - }) - .optional(), - // componentMultipleChoice: MultipleChoicesContentSchema.optional(), - componentMultipleChoice: z - .object({ - selectedComponents: z.array(ComponentSchema), - }) - .optional(), - // contentChunk: ChunksContentSchema.optional(), - contentChunk: z - .object({ - chunks: z.array(z.array(ComponentSchema)), - }) - .optional(), + chunks: z.array(z.array(ComponentSchema)), }), - ), + ]), ); export const ChoiceContentSchema = z.object({