Skip to content

Commit

Permalink
feat(schema): update component can get and sku but not both
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Nov 20, 2024
1 parent a6416b0 commit d7fec4c
Show file tree
Hide file tree
Showing 6 changed files with 1,442 additions and 125 deletions.
10 changes: 5 additions & 5 deletions components/schema/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crystallize/schema",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"type": "module",
"exports": {
Expand All @@ -22,10 +22,10 @@
},
"devDependencies": {
"@tsconfig/node20": "^20.1.4",
"@types/node": "^20.14.9",
"@types/node": "^22.9.1",
"json": "^11.0.0",
"tsup": "^8.1.0",
"typescript": "^5.5.2",
"vitest": "^1.6.0"
"tsup": "^8.3.5",
"typescript": "^5.6.3",
"vitest": "^2.1.5"
}
}
34 changes: 25 additions & 9 deletions components/schema/src/mass-operation/item.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { z } from 'zod';
import { ComponentInputSchema } from '../item/components/index.js';
import { IdSchema } from '../shared/index.js';

export const CreateItemOperationSchema = z.object({
concern: z.literal('item'),
action: z.literal('create'),
type: z.enum(['product', 'document', 'folder']),
shape: z.string().min(1),
language: z.string().min(1),
topics: z.array(z.string()).optional(),
topics: z.array(IdSchema).optional(),
components: z.array(ComponentInputSchema),
});

Expand All @@ -18,7 +19,7 @@ export const UpdateItemOperationSchema = CreateItemOperationSchema.omit({
}).merge(
z.object({
action: z.literal('update'),
itemId: z.string().min(1),
itemId: IdSchema,
}),
);

Expand All @@ -30,19 +31,34 @@ export const UpsertItemOperationSchema = UpdateItemOperationSchema.omit({ action

export const UpdateCompomentOperationSchema = UpdateItemOperationSchema.omit({
action: true,
itemId: true,
components: true,
topics: true,
}).merge(
z.object({
action: z.literal('updateComponent'),
component: ComponentInputSchema,
}),
);
})
.merge(
z.object({
action: z.literal('updateComponent'),
component: ComponentInputSchema,
}),
)
.and(
z
.object({
sku: z.never().optional(),
itemId: IdSchema,
})
.or(
z.object({
sku: z.string(),
itemId: z.never().optional(),
}),
),
);

export const PublishItemOperationSchema = z.object({
concern: z.literal('item'),
action: z.literal('publish'),
itemId: z.string().min(1),
itemId: IdSchema,
language: z.string().min(1),
includeDescendants: z.boolean().optional(),
});
Expand Down
3 changes: 2 additions & 1 deletion components/schema/src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod';

export const IdSchema = z.string().min(24).max(24);
export const IdSchema = z.string().regex(/^[0-9a-f]{24}$/);

export const KeyValuePairSchema = z.record(z.string());
export const DateTimeSchema = z.string().refine(
(str) => Number.isInteger(Date.parse(str)),
Expand Down
83 changes: 83 additions & 0 deletions components/schema/tests/mass-operation/update-component.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { never, ZodError } from 'zod';
import { UpdateCompomentOperation, UpdateCompomentOperationSchema } from '../../src/mass-operation/item';
import { describe, expect, it, test } from 'vitest';

describe('Mass Operations - Update Component Operation', {}, async () => {
it('should NOT fail if the component is valid with only itemId ', () => {
const updateComponentOperation: UpdateCompomentOperation = {
action: 'updateComponent',
concern: 'item',
itemId: '604f7655a16b91dea030895b',
language: 'en',
component: {
componentId: '456',
boolean: {
value: true,
},
},
};
expect(UpdateCompomentOperationSchema.safeParse(updateComponentOperation)).toEqual({
success: true,
data: updateComponentOperation,
});
});

it('should NOT fail if the component is valid with only sku ', () => {
const updateComponentOperation: UpdateCompomentOperation = {
action: 'updateComponent',
concern: 'item',
language: 'en',
sku: 'asdasd',
component: {
componentId: '456',
boolean: {
value: true,
},
},
};
expect(UpdateCompomentOperationSchema.safeParse(updateComponentOperation)).toEqual({
success: true,
data: updateComponentOperation,
});
});

it('should fail if the component contains sku and itemId ', () => {
// @ts-expect-error
const updateComponentOperation: UpdateCompomentOperation = {
action: 'updateComponent',
concern: 'item',
language: 'en',
sku: 'asdasd',
itemId: '604f7655a16b91dea030895b',
component: {
componentId: '456',
boolean: {
value: true,
},
},
};
expect(UpdateCompomentOperationSchema.safeParse(updateComponentOperation)).toMatchObject({
success: false,
});
});

it('should fail if the component is invalid ', () => {
// @ts-expect-error
const updateComponentOperation: UpdateCompomentOperation = {
action: 'updateComponent',
concern: 'item',
language: 'en',
sku: 'asdasd',
itemId: '604f655a16b91dea030895b', // mistake here
component: {
componentId: '456',
boolean: {
value: true,
},
},
};
expect(UpdateCompomentOperationSchema.safeParse(updateComponentOperation)).toMatchObject({
success: false,
});
});
});
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"devDependencies": {
"prettier": "3.3.2",
"turbo": "^2.0.6"
"turbo": "^2.3.0"
},
"scripts": {
"build": "turbo run build",
Expand All @@ -19,6 +19,5 @@
"packageManager": "[email protected]",
"volta": {
"node": "20.15.0"
},
"dependencies": {}
}
}
Loading

0 comments on commit d7fec4c

Please sign in to comment.