codeweaver 2.3.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,66 +0,0 @@
1
- import { z } from "zod";
2
- import { ResponseError } from "./error-handling";
3
-
4
- /**
5
- * Strictly assign obj (type T1) to T2 using a Zod schema.
6
- *
7
- * - Extras in source are ignored (no throw).
8
- * - Validates fields with the schema; on failure, throws with a descriptive message.
9
- * - Returns an object typed as T2 (inferred from the schema).
10
- *
11
- * @param source - Source object of type T1
12
- * @param destination - Destination object to be populated (typed as T2)
13
- * @param schema - Zod schema describing the target type T2
14
- * @returns T2 representing the destination after assignment
15
- */
16
- export default function assign<T1 extends object, T2 extends object>(
17
- source: T1,
18
- destination: T2,
19
- schema: z.ZodObject<any>,
20
- ignoreNullAndUndefined: false
21
- ): T2 {
22
- // 1) Validate the subset of keys defined in the schema
23
- // Build an object that contains only the keys present in source but are part of the schema
24
- const subsetForSchema: any = {};
25
-
26
- // Iterate schema keys
27
- for (const key of Object.keys(schema.shape)) {
28
- if (Object.prototype.hasOwnProperty.call(source, key)) {
29
- if (ignoreNullAndUndefined) {
30
- subsetForSchema[key] = (source as any)[key] ?? subsetForSchema[key];
31
- } else {
32
- subsetForSchema[key] = (source as any)[key];
33
- }
34
- }
35
- }
36
-
37
- // Validate using the schema on the subset (this will also coerce if the schema has transforms)
38
- const parseResult = schema.safeParse(subsetForSchema);
39
- if (!parseResult.success) {
40
- // Build a descriptive error message from the first issue
41
- const issue = parseResult.error.issues?.[0];
42
- const path = issue?.path?.length ? issue.path.join(".") : "value";
43
- const message = issue?.message ?? "Schema validation failed";
44
- throw new ResponseError(
45
- `assignStrictlyFromSchema: Validation failed for "${path}": ${message}`,
46
- 500
47
- );
48
- }
49
-
50
- // 2) Assign validated values to destination strictly
51
- // Use the parsed result to ensure types align with the schema
52
- const validated = parseResult.data as any;
53
-
54
- // Copy only keys that are in the schema
55
- for (const key of Object.keys(schema.shape)) {
56
- if (Object.prototype.hasOwnProperty.call(validated, key)) {
57
- if (ignoreNullAndUndefined) {
58
- subsetForSchema[key] = (source as any)[key] ?? subsetForSchema[key];
59
- } else {
60
- subsetForSchema[key] = (source as any)[key];
61
- }
62
- }
63
- }
64
-
65
- return destination;
66
- }