@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/api-services",
3
- "version": "26.12.9",
3
+ "version": "26.12.11",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -57,11 +57,21 @@ export const getDefaultsFromZodSchema = (
57
57
  schema: z.ZodType,
58
58
  value?: unknown,
59
59
  ): unknown => {
60
- return (
61
- schema
62
- /* zod validates passed value and throws err before returning defaults,
63
- so we should skip error throwing and return value instead */
64
- .catch(({ value: validatedValue }: { value: unknown }) => validatedValue)
65
- .parse(value)
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
  };