Skip to content

Commit

Permalink
fix(schema): when it's not an input content and config are not having…
Browse files Browse the repository at this point in the history
… a key
  • Loading branch information
Plopix committed Jan 17, 2025
1 parent 78cf472 commit 3fe8aac
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 122 deletions.
2 changes: 1 addition & 1 deletion components/schema/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crystallize/schema",
"version": "3.0.3",
"version": "3.1.0",
"license": "MIT",
"exports": {
"./pim": {
Expand Down
155 changes: 96 additions & 59 deletions components/schema/src/pim/components/component-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NestableComponentConfig> = 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
Expand All @@ -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<ComponentConfig> = 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<typeof ChoiceConfigSchema>;

Expand Down
153 changes: 91 additions & 62 deletions components/schema/src/pim/components/component-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,85 +14,114 @@ 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<NestableComponentContent> = 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
* as we have recursive imports here
*
* 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<ComponentContent> = 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({
Expand Down

0 comments on commit 3fe8aac

Please sign in to comment.