rads-db 3.0.58 → 3.0.59
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/dist/index.cjs +21 -2
- package/dist/index.mjs +22 -3
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -82,7 +82,7 @@ function getFieldZodSchemaBase(zodSchemas, schema, field) {
|
|
|
82
82
|
const zodSchema = getZodSchema(zodSchemas, schema, field.type);
|
|
83
83
|
if (!zodSchema)
|
|
84
84
|
throw new Error(`Cannot find zod schema for type: ${field.type}`);
|
|
85
|
-
return
|
|
85
|
+
return deepPartial(zodSchema.omit({ id: true }));
|
|
86
86
|
}
|
|
87
87
|
if (field.type === "string")
|
|
88
88
|
return zod.z.string();
|
|
@@ -106,6 +106,25 @@ function getFieldZodSchemaBase(zodSchemas, schema, field) {
|
|
|
106
106
|
}
|
|
107
107
|
throw new Error(`Unknown type: ${field.type}`);
|
|
108
108
|
}
|
|
109
|
+
function deepPartial(zodSchema) {
|
|
110
|
+
const shape = zodSchema.shape;
|
|
111
|
+
const newShape = {};
|
|
112
|
+
for (const key in shape) {
|
|
113
|
+
let field = shape[key];
|
|
114
|
+
while (field instanceof zod.ZodNullable || field instanceof zod.ZodOptional) {
|
|
115
|
+
field = field.unwrap();
|
|
116
|
+
}
|
|
117
|
+
if (field instanceof zod.ZodLazy) {
|
|
118
|
+
field = field.schema;
|
|
119
|
+
}
|
|
120
|
+
if (field instanceof zod.ZodObject) {
|
|
121
|
+
newShape[key] = deepPartial(field).optional().nullable();
|
|
122
|
+
} else {
|
|
123
|
+
newShape[key] = field.optional().nullable();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return zod.z.object(newShape);
|
|
127
|
+
}
|
|
109
128
|
class ValidationError extends Error {
|
|
110
129
|
constructor(error) {
|
|
111
130
|
const firstError = error.errors[0];
|
|
@@ -576,7 +595,7 @@ function cleanUndefinedAndNull(obj, isChange = false) {
|
|
|
576
595
|
if (obj[key] == null && !isChange)
|
|
577
596
|
delete obj[key];
|
|
578
597
|
if (___default.isPlainObject(obj[key]))
|
|
579
|
-
cleanUndefinedAndNull(obj[key], key === "change");
|
|
598
|
+
cleanUndefinedAndNull(obj[key], isChange || key === "change");
|
|
580
599
|
}
|
|
581
600
|
}
|
|
582
601
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z, ZodError } from 'zod';
|
|
1
|
+
import { z, ZodNullable, ZodOptional, ZodLazy, ZodObject, ZodError } from 'zod';
|
|
2
2
|
import _ from 'lodash';
|
|
3
3
|
import { v4 } from 'uuid';
|
|
4
4
|
import createMerge from '@fastify/deepmerge';
|
|
@@ -74,7 +74,7 @@ function getFieldZodSchemaBase(zodSchemas, schema, field) {
|
|
|
74
74
|
const zodSchema = getZodSchema(zodSchemas, schema, field.type);
|
|
75
75
|
if (!zodSchema)
|
|
76
76
|
throw new Error(`Cannot find zod schema for type: ${field.type}`);
|
|
77
|
-
return
|
|
77
|
+
return deepPartial(zodSchema.omit({ id: true }));
|
|
78
78
|
}
|
|
79
79
|
if (field.type === "string")
|
|
80
80
|
return z.string();
|
|
@@ -98,6 +98,25 @@ function getFieldZodSchemaBase(zodSchemas, schema, field) {
|
|
|
98
98
|
}
|
|
99
99
|
throw new Error(`Unknown type: ${field.type}`);
|
|
100
100
|
}
|
|
101
|
+
function deepPartial(zodSchema) {
|
|
102
|
+
const shape = zodSchema.shape;
|
|
103
|
+
const newShape = {};
|
|
104
|
+
for (const key in shape) {
|
|
105
|
+
let field = shape[key];
|
|
106
|
+
while (field instanceof ZodNullable || field instanceof ZodOptional) {
|
|
107
|
+
field = field.unwrap();
|
|
108
|
+
}
|
|
109
|
+
if (field instanceof ZodLazy) {
|
|
110
|
+
field = field.schema;
|
|
111
|
+
}
|
|
112
|
+
if (field instanceof ZodObject) {
|
|
113
|
+
newShape[key] = deepPartial(field).optional().nullable();
|
|
114
|
+
} else {
|
|
115
|
+
newShape[key] = field.optional().nullable();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return z.object(newShape);
|
|
119
|
+
}
|
|
101
120
|
class ValidationError extends Error {
|
|
102
121
|
constructor(error) {
|
|
103
122
|
const firstError = error.errors[0];
|
|
@@ -568,7 +587,7 @@ function cleanUndefinedAndNull(obj, isChange = false) {
|
|
|
568
587
|
if (obj[key] == null && !isChange)
|
|
569
588
|
delete obj[key];
|
|
570
589
|
if (_.isPlainObject(obj[key]))
|
|
571
|
-
cleanUndefinedAndNull(obj[key], key === "change");
|
|
590
|
+
cleanUndefinedAndNull(obj[key], isChange || key === "change");
|
|
572
591
|
}
|
|
573
592
|
}
|
|
574
593
|
|