Skip to content

Commit

Permalink
feat(schema): preparation version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Dec 1, 2024
1 parent 732f277 commit 6a4751b
Show file tree
Hide file tree
Showing 15 changed files with 5,465 additions and 7,317 deletions.
1 change: 1 addition & 0 deletions components/import-export-sdk/src/shape/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ShapeComponentInput,
UpdateShapeInputSchema,
} from '@crystallize/schema';

import { ThinClient } from '../shared/thin-client.js';
import { createShapeMutation } from './mutations/create.js';
import { updateShapeMutation } from './mutations/update.js';
Expand Down
10 changes: 10 additions & 0 deletions components/import-export-sdk/tests/shape/createMutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const testCases: testCase[] = [
{
name: 'Returns the query and variables for a basic shape',
input: {
identifier: 'shape',
name: 'Some Shape',
type: 'product',
},
Expand All @@ -23,6 +24,7 @@ const testCases: testCase[] = [
input: {
name: 'Some Shape',
type: 'product',
identifier: 'shape',
components: [
{
id: 'someSingleLine',
Expand Down Expand Up @@ -59,6 +61,13 @@ const testCases: testCase[] = [
type: 'product',
} as NextPimCreateShapeInput,
error: new ZodError([
{
code: 'invalid_type',
expected: 'string',
received: 'undefined',
path: ['identifier'],
message: 'Required',
},
{
code: 'invalid_type',
expected: 'string',
Expand All @@ -73,6 +82,7 @@ const testCases: testCase[] = [
input: {
name: 'some invalid shape',
type: 'product',
identifier: 'shape',
components: [
{
id: 'someSingleLine',
Expand Down
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": "1.2.1",
"version": "2.0.0",
"license": "MIT",
"type": "module",
"main": "./dist/index.cjs",
Expand Down
1 change: 1 addition & 0 deletions components/schema/src/item/components/piece/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from 'zod';
import { ComponentInputSchema } from '../index.js';

export const PieceContentInputSchema = z.object({
identifier: z.string().min(1),
components: z.array(ComponentInputSchema).optional(),
});

Expand Down
5 changes: 0 additions & 5 deletions components/schema/src/item/enums.ts

This file was deleted.

11 changes: 6 additions & 5 deletions components/schema/src/item/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { z } from 'zod';
import { basicShapeSchema } from '../shape/index.js';
import { DateTimeSchema, Id, IdSchema } from '../shared/index.js';
import { BasicShapeSchema } from '../shape/index.js';
import { DateTimeSchema, Id, IdSchema, ItemType, ItemTypeEnum } from '../shared/index.js';
import { Topic, TopicSchema } from '../topic/index.js';
import { ComponentInputSchema } from './components/index.js';
import { ItemType, ItemTypeEnum } from './enums.js';
export * from './components/index.js';

export type Item = {
Expand All @@ -15,7 +14,7 @@ export type Item = {
type: ItemType;
name?: string;
externalReference?: string;
shape?: z.infer<typeof basicShapeSchema>;
shape?: z.infer<typeof BasicShapeSchema>;
components?: any[];
topics?: Topic[];
relatingItems?: Item[];
Expand Down Expand Up @@ -59,7 +58,7 @@ export const ItemSchema: z.ZodType<Item> = z.lazy(() =>
name: z.string().optional(),
externalReference: z.string().optional(),
components: z.array(z.any()).optional(),
shape: basicShapeSchema.optional(),
shape: BasicShapeSchema.optional(),
topics: z.array(TopicSchema).optional(),
relatingItems: z.array(ItemSchema).optional(),
tree: z
Expand Down Expand Up @@ -161,3 +160,5 @@ export type CreateProductInput = z.infer<typeof CreateProductInputSchema>;
export type UpdateDocumentInput = z.infer<typeof UpdateDocumentInputSchema>;
export type UpdateFolderInput = z.infer<typeof UpdateFolderInputSchema>;
export type UpdateProductInput = z.infer<typeof UpdateProductInputSchema>;

export type { ComponentInput } from './components/index.js';
17 changes: 15 additions & 2 deletions components/schema/src/mass-operation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import {
CreateItemOperationSchema,
PublishItemOperationSchema,
UnPublishItemOperationSchema,
UpdateCompomentOperationSchema,
UpdateComponentOperationSchema,
UpdateItemOperationSchema,
UpsertItemOperationSchema,
} from './item.js';
import { CreateShapeOperationSchema, UpdateShapeOperationSchema } from './shape.js';

export const OperationSchema = z.union([
CreateItemOperationSchema,
UpdateItemOperationSchema,
UpsertItemOperationSchema,
UpdateCompomentOperationSchema,
UpdateComponentOperationSchema,
PublishItemOperationSchema,
UnPublishItemOperationSchema,
CreateShapeOperationSchema,
UpdateShapeOperationSchema,
]);

export const OperationsSchema = z.object({
Expand All @@ -26,3 +29,13 @@ export const OperationsSchema = z.object({

export type Operation = z.infer<typeof OperationSchema>;
export type Operations = z.infer<typeof OperationsSchema>;
export type {
CreateItemOperation,
PublishItemOperation,
UnPublishItemOperation,
UpdateComponentOperation,
UpdateItemOperation,
UpsertItemOperation,
} from './item.js';

export type { CreateShapeOperation, UpdateShapeOperation } from './shape.js';
4 changes: 2 additions & 2 deletions components/schema/src/mass-operation/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const UpsertItemOperationSchema = UpdateItemOperationSchema.omit({ action
}),
);

export const UpdateCompomentOperationSchema = UpdateItemOperationSchema.omit({
export const UpdateComponentOperationSchema = UpdateItemOperationSchema.omit({
action: true,
itemId: true,
components: true,
Expand Down Expand Up @@ -72,6 +72,6 @@ export const UnPublishItemOperationSchema = PublishItemOperationSchema.omit({ ac
export type CreateItemOperation = z.infer<typeof CreateItemOperationSchema>;
export type UpdateItemOperation = z.infer<typeof UpdateItemOperationSchema>;
export type UpsertItemOperation = z.infer<typeof UpsertItemOperationSchema>;
export type UpdateCompomentOperation = z.infer<typeof UpdateCompomentOperationSchema>;
export type UpdateComponentOperation = z.infer<typeof UpdateComponentOperationSchema>;
export type PublishItemOperation = z.infer<typeof PublishItemOperationSchema>;
export type UnPublishItemOperation = z.infer<typeof UnPublishItemOperationSchema>;
19 changes: 19 additions & 0 deletions components/schema/src/mass-operation/shape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { z } from 'zod';
import { NextPimCreateShapeInputSchema, UpdateShapeInputSchema } from '../shape/index.js';

export const CreateShapeOperationSchema = NextPimCreateShapeInputSchema.and(
z.object({
concern: z.literal('shape'),
action: z.literal('create'),
}),
);
export type CreateShapeOperation = z.infer<typeof CreateShapeOperationSchema>;

export const UpdateShapeOperationSchema = UpdateShapeInputSchema.and(
z.object({
concern: z.literal('shape'),
action: z.literal('update'),
identifier: z.string().min(1),
}),
);
export type UpdateShapeOperation = z.infer<typeof UpdateShapeOperationSchema>;
42 changes: 31 additions & 11 deletions components/schema/src/shape/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import { z } from 'zod';
import { IdSchema } from '../shared/index.js';
import { ShapeComponentTypeEnum } from './enums.js';

const GenericComponentConfigSchema = z.object({
multilingual: z.boolean().optional(),
required: z.boolean().optional(),
discoverable: z.boolean().optional(),
});

type GenericComponentConfig = z.infer<typeof GenericComponentConfigSchema>;

const minValueSchema = z.number().min(0).nullable().optional();
const maxValueSchema = z.number().min(0).nullable().optional();
const minMaxSchema = z.object({ min: minValueSchema, max: maxValueSchema });
const minMaxSchema = GenericComponentConfigSchema.extend({ min: minValueSchema, max: maxValueSchema });
const minMaxItemRelationsSchema = z.object({
min: minValueSchema,
max: maxValueSchema,
Expand Down Expand Up @@ -104,17 +112,17 @@ export const MinMaxItemRelationsComponentConfigSchema = minMaxItemRelationsSchem
},
);

export type ComponentChoiceComponentConfig = {
export type ComponentChoiceComponentConfig = GenericComponentConfig & {
choices: any[];
};
export type ComponentMultipleChoiceComponentConfig = {
export type ComponentMultipleChoiceComponentConfig = GenericComponentConfig & {
choices: any[];
allowDuplicates: boolean;
};

export const ComponentChoiceComponentConfigInputSchema: z.ZodType<ComponentChoiceComponentConfig> = z
.lazy(() =>
z.object({
GenericComponentConfigSchema.extend({
choices: z.array(ShapeComponentInputSchema),
}),
)
Expand All @@ -124,20 +132,20 @@ export const ComponentChoiceComponentConfigInputSchema: z.ZodType<ComponentChoic

export const ComponentMultipleChoiceComponentConfigInputSchema: z.ZodType<ComponentMultipleChoiceComponentConfig> =
z.lazy(() =>
z.object({
GenericComponentConfigSchema.extend({
choices: z.array(ShapeComponentInputSchema),
allowDuplicates: z.coerce.boolean(),
}),
);

export type ContentChunkComponentConfig = {
export type ContentChunkComponentConfig = GenericComponentConfig & {
components: any[];
repeatable?: boolean;
};

export const ContentChunkComponentConfigInputSchema: z.ZodType<ContentChunkComponentConfig> = z
.lazy(() =>
z.object({
GenericComponentConfigSchema.extend({
components: z.array(ShapeComponentInputSchema),
repeatable: z.coerce.boolean().default(false),
}),
Expand Down Expand Up @@ -177,6 +185,7 @@ export const ItemRelationsComponentConfigSchema = MinMaxItemRelationsComponentCo
.optional()
.nullable(),
})

.optional()
.nullable(),
}),
Expand Down Expand Up @@ -210,24 +219,24 @@ export const ItemRelationsComponentConfigSchema = MinMaxItemRelationsComponentCo
}
});

export const NumericComponentConfigSchema = z.object({
export const NumericComponentConfigSchema = GenericComponentConfigSchema.extend({
decimalPlaces: z.number().min(0).optional(),
units: z.array(z.string()).optional(),
});

export type PieceComponentConfig = {
export type PieceComponentConfig = GenericComponentConfig & {
identifier: string;
components: any[];
};

export const PieceComponentConfigInputSchema: z.ZodType<PieceComponentConfig> = z.lazy(() =>
z.object({
GenericComponentConfigSchema.extend({
identifier: z.string().min(1),
components: z.array(ShapeComponentInputSchema),
}),
);

export const PropertiesTableComponentConfigSchema = z.object({
export const PropertiesTableComponentConfigSchema = GenericComponentConfigSchema.extend({
sections: z.array(
z.object({
title: z.string().optional().nullable(),
Expand All @@ -252,10 +261,19 @@ export const SelectionComponentConfigInputSchema = MinMaxComponentConfigSchema.a
);

export const ShapeComponentConfigInputSchema = z.object({
singleLine: MinMaxComponentConfigSchema.optional(),
boolean: GenericComponentConfigSchema.optional(),
datetime: GenericComponentConfigSchema.optional(),
gridRelations: GenericComponentConfigSchema.optional(),
location: GenericComponentConfigSchema.optional(),
paragraphCollection: GenericComponentConfigSchema.optional(),
richText: GenericComponentConfigSchema.optional(),
componentChoice: ComponentChoiceComponentConfigInputSchema.optional(),
componentMultipleChoice: ComponentMultipleChoiceComponentConfigInputSchema.optional(),
contentChunk: ContentChunkComponentConfigInputSchema.optional(),
files: FileComponentConfigSchema.optional(),
images: GenericComponentConfigSchema.optional(), //@todo: add ImageComponentConfigSchema
videos: GenericComponentConfigSchema.optional(), //@todo: add VideoComponentConfigSchema
itemRelations: ItemRelationsComponentConfigSchema.optional(),
numeric: NumericComponentConfigSchema.optional(),
piece: PieceComponentConfigInputSchema.optional(),
Expand All @@ -282,6 +300,7 @@ export const ShapeComponentInputSchema = z
description: z.string().optional().nullable(),
config: ShapeComponentConfigInputSchema.optional().nullable(),
})

.refine(
({ type, config }) => {
if (!config) {
Expand All @@ -305,6 +324,7 @@ export const ShapeComponentSchema = z
description: z.string().optional().nullable(),
config: ShapeComponentConfigSchema.optional().nullable(),
})

.refine(
({ type, config }) => {
if (!config) {
Expand Down
2 changes: 0 additions & 2 deletions components/schema/src/shape/enums.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { z } from 'zod';

export const ShapeTypeEnum = z.enum(['product', 'document', 'folder']);
export const ShapeComponentTypeEnum = z.enum([
'boolean',
'componentChoice',
Expand All @@ -22,5 +21,4 @@ export const ShapeComponentTypeEnum = z.enum([
'files',
]);

export type ShapeType = z.infer<typeof ShapeTypeEnum>;
export type ShapeComponentType = z.infer<typeof ShapeComponentTypeEnum>;
Loading

0 comments on commit 6a4751b

Please sign in to comment.