Skip to content

Commit 9560908

Browse files
committed
fix: update tests & deepMerge
1 parent a0d1c98 commit 9560908

File tree

5 files changed

+25
-39
lines changed

5 files changed

+25
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Zagora
22

3-
[![ci](https://github.com/tunnckoCore/zagora/actions/workflows/ci.yml/badge.svg)](https://github.com/tunnckoCore/zagora/actions/workflows/ci.yml) <!-- COV_BADGE:START -->![coverage](https://badgen.net/badge/coverage/99.49%25/99CC09)<!-- COV_BADGE:END -->
3+
[![ci](https://github.com/tunnckoCore/zagora/actions/workflows/ci.yml/badge.svg)](https://github.com/tunnckoCore/zagora/actions/workflows/ci.yml) <!-- COV_BADGE:START -->![coverage](https://badgen.net/badge/coverage/98.32%25/99CC09)<!-- COV_BADGE:END -->
44

55
Elevate your TypeScript workflow with Zagora: a sleek, bulletproof toolkit for forging type-safe, error-proof functions and libraries that never throw. Powered by StandardSchema-compliant validators like Zod, Valibot, and Arktype, it delivers rock-solid input/output validation and richly typed errors. No routers, no network baggage — just pure, exportable functions ready to supercharge your code. The ultimate streamlined alternative to oRPC and tRPC, stripping away the network layer for unmatched type-safety, simplicity and robustness.
66

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export class Zagora<
323323
const envVarsMapSchema = zagora.envVarsMapSchema;
324324
const baseEnvVars = zagora.envVars;
325325

326-
const mergedEnvVars = baseEnvVars ? deepMerge(baseEnvVars, env) : env;
326+
const mergedEnvVars = deepMerge(baseEnvVars || {}, env || {});
327327

328328
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: -- ok
329329
const forwardProcedure = (...args: unknown[]) => {

src/utils.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,19 +268,30 @@ export function handleTupleDefaults(
268268

269269
return rawArgs;
270270
}
271+
function isPlainObject(obj: any) {
272+
return obj && typeof obj === "object";
273+
}
274+
275+
export function deepMerge(aaa: any, bbb: any) {
276+
if (!isPlainObject(aaa)) {
277+
throw new Error("Expects an object args");
278+
}
279+
280+
const result = { ...aaa };
271281

272-
export function deepMerge(target: any, source: any): any {
273-
if (source == null || typeof source !== "object") return source;
274-
if (target == null || typeof target !== "object") return source;
275-
const result = Array.isArray(target) ? [...target] : { ...target };
276-
for (const key in source) {
277-
if (Object.hasOwn(source, key)) {
278-
if (typeof source[key] === "object" && source[key] !== null) {
279-
result[key] = deepMerge(target[key], source[key]);
282+
if (!isPlainObject(bbb)) {
283+
return result;
284+
}
285+
286+
for (const key in bbb) {
287+
if (Object.hasOwn(bbb, key)) {
288+
if (isPlainObject(result[key]) && isPlainObject(bbb[key])) {
289+
result[key] = deepMerge(result[key], bbb[key]);
280290
} else {
281-
result[key] = source[key];
291+
result[key] = bbb[key];
282292
}
283293
}
284294
}
295+
285296
return result;
286297
}

test/types-testing.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ test("should have ~zagora property typed correctly on buiulder and on callable",
703703
const _inputSchema = meta.inputSchema;
704704
const _outputSchema = meta.outputSchema;
705705
const _errorsMap = meta.errorsMap;
706+
const _envMap = meta.envVarsMapSchema;
706707

707708
const builder = zagora()
708709
.input(z.string())
@@ -715,6 +716,7 @@ test("should have ~zagora property typed correctly on buiulder and on callable",
715716
const _in = metaFromBuilder.inputSchema;
716717
const _out = metaFromBuilder.outputSchema;
717718
const _err = metaFromBuilder.errorsMap;
719+
const _env = metaFromBuilder.envVarsMapSchema;
718720
});
719721

720722
test("SpreadTuple works for 4-argument tuple", () => {

test/utils.test.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ test("validateInputOutput - async validation success path (line 82)", async () =
3030
.output(z.string())
3131
.handler((input) => input.toUpperCase());
3232

33-
// TODO / DOCUMENT: Having an ASYNC validation at any place,
34-
// always require the procedure to be awaited
33+
// NOTE: Having an ASYNC validation at any place, always require the procedure to be awaited
3534
const res = await fn("valid");
3635

3736
expect(res.ok).toBe(true);
@@ -132,29 +131,3 @@ test("deepMerge - merge nested objects", () => {
132131
expect(result.b.e).toBe(5);
133132
expect(result.f).toBe(6);
134133
});
135-
136-
test("deepMerge - merge arrays", () => {
137-
const target = [1, 2, 3];
138-
const source = [4, 5];
139-
const result = deepMerge(target, source);
140-
141-
expect(result).toEqual([4, 5, 3]);
142-
});
143-
144-
test("deepMerge - source is null", () => {
145-
const target = { a: 1 };
146-
const result = deepMerge(target, null);
147-
expect(result).toBe(null);
148-
});
149-
150-
test("deepMerge - target is null", () => {
151-
const source = { a: 1 };
152-
const result = deepMerge(null, source);
153-
expect(result).toEqual({ a: 1 });
154-
});
155-
156-
test("deepMerge - source is primitive", () => {
157-
const target = { a: 1 };
158-
const result = deepMerge(target, "string");
159-
expect(result).toBe("string");
160-
});

0 commit comments

Comments
 (0)