@webitel/api-services 26.12.9 → 26.12.11
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.
- package/package.json +1 -1
- package/src/utils/gen/index.ts +17 -7
- package/types/.tsbuildinfo +1 -1
package/package.json
CHANGED
package/src/utils/gen/index.ts
CHANGED
|
@@ -57,11 +57,21 @@ export const getDefaultsFromZodSchema = (
|
|
|
57
57
|
schema: z.ZodType,
|
|
58
58
|
value?: unknown,
|
|
59
59
|
): unknown => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
const result = schema.safeParse(value);
|
|
61
|
+
if (result.success) return result.data;
|
|
62
|
+
if (!(schema instanceof z.ZodObject)) return value;
|
|
63
|
+
|
|
64
|
+
// the value is invalid (e.g. a new card with empty required fields),
|
|
65
|
+
// so take each field's own default and keep what is already filled in
|
|
66
|
+
const defaults: Record<string, unknown> = {};
|
|
67
|
+
for (const [key, fieldSchema] of Object.entries(schema.shape)) {
|
|
68
|
+
const fieldResult = (fieldSchema as z.ZodType).safeParse(undefined);
|
|
69
|
+
if (fieldResult.success && fieldResult.data !== undefined) {
|
|
70
|
+
defaults[key] = fieldResult.data;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
...defaults,
|
|
75
|
+
...(value as object),
|
|
76
|
+
};
|
|
67
77
|
};
|