Skip to content

Commit 3d30489

Browse files
committed
[x/ts] - fixed issues with deep merges in analog read tasks
1 parent 309a233 commit 3d30489

8 files changed

Lines changed: 78 additions & 7 deletions

File tree

client/ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@synnaxlabs/client",
3-
"version": "0.42.0",
3+
"version": "0.42.1",
44
"description": "The Synnax Client Library",
55
"keywords": [
66
"synnax",

console/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@synnaxlabs/console",
33
"private": true,
4-
"version": "0.42.0",
4+
"version": "0.42.1",
55
"type": "module",
66
"scripts": {
77
"dev": "tauri dev",

docs/site/src/pages/releases/0-42-0.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ not been user friendly, especially when it comes to communicating errors. To add
3636
this, we've redesigned the `Writer` class in all three languages to be more simple and
3737
intuitive.
3838

39-
Instead of returning a boolean from calls to `write`, writers will not directly raise
39+
Instead of returning a boolean from calls to `write`, writers will now directly raise
4040
any errors encountered during a write operation. This removes the need for calls to the
4141
`error` method, so we've completely removed it.
4242

drift/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@synnaxlabs/drift",
3-
"version": "0.42.0",
3+
"version": "0.42.1",
44
"description": "State synchronization and Redux state synchronization for Tauri Apps",
55
"repository": "https://github.com/synnaxlabs/synnax/tree/main/drift",
66
"type": "module",

freighter/ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@synnaxlabs/freighter",
3-
"version": "0.42.0",
3+
"version": "0.42.1",
44
"type": "module",
55
"description": "a modular transport abstraction",
66
"repository": "https://github.com/synnaxlabs/synnax/tree/main/freighter/ts",

x/ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@synnaxlabs/x",
3-
"version": "0.42.0",
3+
"version": "0.42.1",
44
"type": "module",
55
"description": "Common Utilities for Synnax Labs",
66
"repository": "https://github.com/synnaxlabs/synnax/tree/main/x/go",

x/ts/src/deep/merge.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,64 @@ describe("deepMerge", () => {
202202
b: 2,
203203
});
204204
});
205+
206+
it("should work with zod unions", () => {
207+
const schema1 = z.object({
208+
a: z.number(),
209+
});
210+
const schema2 = z.object({
211+
b: z.number(),
212+
});
213+
const schema = z.union([schema1, schema2]);
214+
const base = {
215+
a: 1,
216+
};
217+
const override = {
218+
b: 2,
219+
};
220+
expect(deep.overrideValidItems(base, override, schema)).toEqual({
221+
a: 1,
222+
b: 2,
223+
});
224+
});
225+
226+
it("should work with extensions", () => {
227+
const schema = z.object({
228+
a: z.number(),
229+
});
230+
const extension = z.object({
231+
b: z.string(),
232+
});
233+
const extendedSchema = schema.extend(extension);
234+
const base = {
235+
a: 1,
236+
};
237+
const override = {
238+
b: "2",
239+
};
240+
expect(deep.overrideValidItems(base, override, extendedSchema)).toEqual({
241+
a: 1,
242+
b: "2",
243+
});
244+
});
245+
246+
it("should work with intersection", () => {
247+
const schema1 = z.object({
248+
a: z.number(),
249+
});
250+
const schema2 = z.object({
251+
b: z.string(),
252+
c: z.number(),
253+
});
254+
const schema = schema1.and(schema2);
255+
const base = {};
256+
const override = {
257+
b: "2",
258+
c: 3,
259+
};
260+
expect(deep.overrideValidItems(base, override, schema)).toEqual({
261+
b: "2",
262+
c: 3,
263+
});
264+
});
205265
});

x/ts/src/deep/merge.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,21 @@ export const overrideValidItems = <A, B>(
4747
overrideObj: any,
4848
currentSchema: any,
4949
): any => {
50+
if (currentSchema.def?.type === "union")
51+
return currentSchema.def.options.reduce(
52+
(acc: any, option: any) => mergeValidFields(acc, overrideObj, option),
53+
baseObj,
54+
);
55+
if (currentSchema.def?.type === "intersection") {
56+
const out = mergeValidFields(baseObj, overrideObj, currentSchema.def.left);
57+
const right = mergeValidFields(out, overrideObj, currentSchema.def.right);
58+
return right;
59+
}
60+
5061
// Iterate over each property in the override object
5162
for (const key in overrideObj) {
5263
const overrideValue = overrideObj[key];
53-
const shape = currentSchema.shape;
64+
const shape = currentSchema?.shape;
5465
if (shape?.[key]) {
5566
const result = shape[key].safeParse(overrideValue);
5667
// Check if parsing succeeded

0 commit comments

Comments
 (0)