@liminalfunctions/framework 1.0.9 → 1.0.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.
|
@@ -31,13 +31,13 @@ export const z_mongodb_id_optional = z.custom((val) => {
|
|
|
31
31
|
}).meta({ framework_override_type: 'mongodb_id' });
|
|
32
32
|
export function mongoose_from_zod(schema_name, zod_definition) {
|
|
33
33
|
let mongoose_schema = schema_from_zod(zod_definition);
|
|
34
|
-
return mongoose.model(schema_name, mongoose_schema);
|
|
34
|
+
return mongoose.model(schema_name, new Schema(mongoose_schema, { typeKey: 'mongoose_type' }));
|
|
35
35
|
}
|
|
36
36
|
export function schema_from_zod(zod_definition) {
|
|
37
37
|
let mongoose_schema = schema_entry_from_zod(zod_definition);
|
|
38
|
-
delete mongoose_schema.
|
|
39
|
-
delete mongoose_schema.
|
|
40
|
-
return mongoose_schema.
|
|
38
|
+
delete mongoose_schema.mongoose_type.required;
|
|
39
|
+
delete mongoose_schema.mongoose_type._id;
|
|
40
|
+
return mongoose_schema.mongoose_type;
|
|
41
41
|
}
|
|
42
42
|
export function schema_entry_from_zod(zod_definition, loop_detector = new Set()) {
|
|
43
43
|
if (!zod_definition) {
|
|
@@ -84,7 +84,8 @@ export function schema_entry_from_zod(zod_definition, loop_detector = new Set())
|
|
|
84
84
|
result.required = !zod_definition.safeParse(undefined).success;
|
|
85
85
|
return result;
|
|
86
86
|
case "any":
|
|
87
|
-
result = {
|
|
87
|
+
result = { mongoose_type: Schema.Types.Mixed, required: false };
|
|
88
|
+
return result;
|
|
88
89
|
case "default":
|
|
89
90
|
result = parse_default(zod_definition._zod.def, loop_detector);
|
|
90
91
|
result.required = true;
|
|
@@ -118,27 +119,27 @@ export function schema_entry_from_zod(zod_definition, loop_detector = new Set())
|
|
|
118
119
|
}
|
|
119
120
|
function parse_object(def, loop_detector) {
|
|
120
121
|
if (loop_detector.has(def)) {
|
|
121
|
-
return {
|
|
122
|
+
return { mongoose_type: Schema.Types.Mixed, required: true };
|
|
122
123
|
}
|
|
123
124
|
loop_detector.add(def);
|
|
124
125
|
let retval = {};
|
|
125
126
|
for (let [key, value] of Object.entries(def.shape)) {
|
|
126
127
|
retval[key] = schema_entry_from_zod(value, loop_detector);
|
|
127
128
|
}
|
|
128
|
-
return {
|
|
129
|
+
return { mongoose_type: retval, required: true };
|
|
129
130
|
}
|
|
130
131
|
function parse_array(def, loop_detector) {
|
|
131
|
-
let retval = {
|
|
132
|
+
let retval = { mongoose_type: [schema_entry_from_zod(def.element, loop_detector)] };
|
|
132
133
|
retval.required = true;
|
|
133
134
|
return retval;
|
|
134
135
|
}
|
|
135
136
|
function parse_enum(def) {
|
|
136
|
-
let retval = {
|
|
137
|
+
let retval = { mongoose_type: String };
|
|
137
138
|
retval.required = true;
|
|
138
139
|
return retval;
|
|
139
140
|
}
|
|
140
141
|
function parse_union(def) {
|
|
141
|
-
let retval = {
|
|
142
|
+
let retval = { mongoose_type: Schema.Types.Mixed };
|
|
142
143
|
retval.required = true;
|
|
143
144
|
return retval;
|
|
144
145
|
}
|
|
@@ -146,24 +147,24 @@ function parse_record(def, loop_detector) {
|
|
|
146
147
|
if (def.keyType._zod.def.type !== 'string') {
|
|
147
148
|
throw new Error('mongoDB only supports maps where the key is a string.');
|
|
148
149
|
}
|
|
149
|
-
let retval = {
|
|
150
|
+
let retval = { mongoose_type: Schema.Types.Map, of: schema_entry_from_zod(def.valueType, loop_detector), required: true };
|
|
150
151
|
retval.required = true;
|
|
151
152
|
return retval;
|
|
152
153
|
}
|
|
153
154
|
function parse_string(def) {
|
|
154
|
-
let retval = {
|
|
155
|
+
let retval = { mongoose_type: String };
|
|
155
156
|
return retval;
|
|
156
157
|
}
|
|
157
158
|
function parse_number(def) {
|
|
158
|
-
let retval = {
|
|
159
|
+
let retval = { mongoose_type: Number };
|
|
159
160
|
return retval;
|
|
160
161
|
}
|
|
161
162
|
function parse_boolean(def) {
|
|
162
|
-
let retval = {
|
|
163
|
+
let retval = { mongoose_type: Boolean };
|
|
163
164
|
return retval;
|
|
164
165
|
}
|
|
165
166
|
function parse_date(def) {
|
|
166
|
-
let retval = {
|
|
167
|
+
let retval = { mongoose_type: Date };
|
|
167
168
|
return retval;
|
|
168
169
|
}
|
|
169
170
|
function parse_default(def, loop_detector) {
|
|
@@ -177,6 +178,6 @@ function parse_optional(def, loop_detector) {
|
|
|
177
178
|
return type_definition;
|
|
178
179
|
}
|
|
179
180
|
function parse_mongodb_id(def) {
|
|
180
|
-
return {
|
|
181
|
+
return { mongoose_type: Schema.Types.ObjectId };
|
|
181
182
|
}
|
|
182
183
|
//# sourceMappingURL=mongoose_from_zod.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongoose_from_zod.js","sourceRoot":"","sources":["../../src/utils/mongoose_from_zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAA;AAC1B,OAAO,QAAQ,EAAE,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAI5C,MAAM,+BAA+B,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAC9D,MAAM,wCAAwC,GAAG,+BAA+B,CAAC,QAAQ,EAAE,CAAC;AAE5F,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAS,CAAC,GAAG,EAAE,EAAE;IACjD,IAAG,CAAC,GAAG,EAAC,CAAC;QAAC,OAAO,KAAK,CAAC;IAAC,CAAC;IACzB,IAAI,MAAM,GAAG,+BAA+B,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAC5D,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAClB,OAAO,KAAK,CAAC;IACjB,CAAC;SAAM,CAAC;QACJ,OAAO,IAAI,CAAC;IAChB,CAAC;AACL,CAAC,CAAC,CAAC,IAAI,CAAC;IACJ,MAAM,EAAE,QAAQ;IAChB,QAAQ,EAAE,QAAQ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,CAAC,YAAY,CAAC;IAC3C,OAAO,eAAe,CAAC;AAC3B,CAAC;AAED,SAAS,cAAc,CAAC,GAA2B,EAAE,aAAuB;IAExE,IAAI,eAAe,GAAG,qBAAqB,CAAC,GAAG,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;IAC1E,eAAe,CAAC,QAAQ,GAAG,KAAK,CAAC;IACjC,OAAO,eAAe,CAAC;AAC3B,CAAC;AAED,SAAS,gBAAgB,CAAC,GAAyB;IAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;AACpD,CAAC"}
|
package/package.json
CHANGED
|
@@ -33,14 +33,14 @@ export const z_mongodb_id_optional = z.custom<string>((val) => {
|
|
|
33
33
|
|
|
34
34
|
export function mongoose_from_zod<T>(schema_name: string, zod_definition: z.core.$ZodType) {
|
|
35
35
|
let mongoose_schema = schema_from_zod(zod_definition);
|
|
36
|
-
return mongoose.model<T>(schema_name, mongoose_schema);
|
|
36
|
+
return mongoose.model<T>(schema_name, new Schema(mongoose_schema, {typeKey: 'mongoose_type'}));
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
export function schema_from_zod(zod_definition: z.core.$ZodType): any {
|
|
40
40
|
let mongoose_schema = schema_entry_from_zod(zod_definition as z.ZodType);
|
|
41
|
-
delete mongoose_schema.
|
|
42
|
-
delete mongoose_schema.
|
|
43
|
-
return mongoose_schema.
|
|
41
|
+
delete mongoose_schema.mongoose_type.required;
|
|
42
|
+
delete mongoose_schema.mongoose_type._id;
|
|
43
|
+
return mongoose_schema.mongoose_type;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export function schema_entry_from_zod(zod_definition: z.ZodType, loop_detector: Set<any> = new Set()): any {
|
|
@@ -91,7 +91,8 @@ export function schema_entry_from_zod(zod_definition: z.ZodType, loop_detector:
|
|
|
91
91
|
result.required = !zod_definition.safeParse(undefined).success
|
|
92
92
|
return result;
|
|
93
93
|
case "any" :
|
|
94
|
-
result = {
|
|
94
|
+
result = { mongoose_type: Schema.Types.Mixed, required: false };
|
|
95
|
+
return result;
|
|
95
96
|
case "default":
|
|
96
97
|
result = parse_default(zod_definition._zod.def as z.core.$ZodDefaultDef, loop_detector);
|
|
97
98
|
result.required = true;
|
|
@@ -127,7 +128,7 @@ export function schema_entry_from_zod(zod_definition: z.ZodType, loop_detector:
|
|
|
127
128
|
|
|
128
129
|
function parse_object(def: z.core.$ZodObjectDef, loop_detector: Set<any>): any {
|
|
129
130
|
if(loop_detector.has(def)) {
|
|
130
|
-
return {
|
|
131
|
+
return {mongoose_type: Schema.Types.Mixed, required: true}
|
|
131
132
|
}
|
|
132
133
|
loop_detector.add(def);
|
|
133
134
|
|
|
@@ -136,24 +137,24 @@ function parse_object(def: z.core.$ZodObjectDef, loop_detector: Set<any>): any {
|
|
|
136
137
|
//@ts-ignore
|
|
137
138
|
retval[key] = schema_entry_from_zod(value, loop_detector);
|
|
138
139
|
}
|
|
139
|
-
return {
|
|
140
|
+
return {mongoose_type: retval, required: true};
|
|
140
141
|
}
|
|
141
142
|
|
|
142
143
|
function parse_array(def: z.core.$ZodArrayDef, loop_detector: Set<any>): any {
|
|
143
144
|
//@ts-ignore
|
|
144
|
-
let retval = {
|
|
145
|
+
let retval = { mongoose_type: [schema_entry_from_zod(def.element, loop_detector)] } as any;
|
|
145
146
|
retval.required = true;
|
|
146
147
|
return retval;
|
|
147
148
|
}
|
|
148
149
|
|
|
149
150
|
function parse_enum(def: z.core.$ZodEnumDef): any {
|
|
150
|
-
let retval = {
|
|
151
|
+
let retval = { mongoose_type: String } as any;
|
|
151
152
|
retval.required = true;
|
|
152
153
|
return retval;
|
|
153
154
|
}
|
|
154
155
|
|
|
155
156
|
function parse_union(def: z.core.$ZodUnionDef): any {
|
|
156
|
-
let retval = {
|
|
157
|
+
let retval = { mongoose_type: Schema.Types.Mixed } as any;
|
|
157
158
|
retval.required = true;
|
|
158
159
|
return retval;
|
|
159
160
|
}
|
|
@@ -161,30 +162,30 @@ function parse_union(def: z.core.$ZodUnionDef): any {
|
|
|
161
162
|
function parse_record(def: z.core.$ZodRecordDef, loop_detector: Set<any>): any {
|
|
162
163
|
if(def.keyType._zod.def.type !== 'string') { throw new Error('mongoDB only supports maps where the key is a string.'); }
|
|
163
164
|
//@ts-ignore
|
|
164
|
-
let retval = {
|
|
165
|
+
let retval = { mongoose_type: Schema.Types.Map, of: schema_entry_from_zod(def.valueType, loop_detector), required: true}
|
|
165
166
|
retval.required = true;
|
|
166
167
|
return retval;
|
|
167
168
|
}
|
|
168
169
|
|
|
169
170
|
function parse_string(def: z.core.$ZodStringDef): any {
|
|
170
|
-
let retval = {
|
|
171
|
+
let retval = { mongoose_type: String } as any;
|
|
171
172
|
// for fixing the optional issue
|
|
172
173
|
// https://github.com/colinhacks/zod/issues/4824
|
|
173
174
|
return retval;
|
|
174
175
|
}
|
|
175
176
|
|
|
176
177
|
function parse_number(def: z.core.$ZodNumberDef): any {
|
|
177
|
-
let retval = {
|
|
178
|
+
let retval = { mongoose_type: Number } as any;
|
|
178
179
|
return retval;
|
|
179
180
|
}
|
|
180
181
|
|
|
181
182
|
function parse_boolean(def: z.core.$ZodBooleanDef): any {
|
|
182
|
-
let retval = {
|
|
183
|
+
let retval = { mongoose_type: Boolean } as any;
|
|
183
184
|
return retval;
|
|
184
185
|
}
|
|
185
186
|
|
|
186
187
|
function parse_date(def: z.core.$ZodDateDef): any {
|
|
187
|
-
let retval = {
|
|
188
|
+
let retval = { mongoose_type: Date } as any;
|
|
188
189
|
return retval;
|
|
189
190
|
}
|
|
190
191
|
|
|
@@ -203,5 +204,5 @@ function parse_optional(def: z.core.$ZodOptionalDef, loop_detector: Set<any>): a
|
|
|
203
204
|
}
|
|
204
205
|
|
|
205
206
|
function parse_mongodb_id(def: z.core.$ZodCustomDef): any {
|
|
206
|
-
return {
|
|
207
|
+
return { mongoose_type: Schema.Types.ObjectId };
|
|
207
208
|
}
|
|
@@ -50,33 +50,33 @@ describe('Mongoose from Zod', function () {
|
|
|
50
50
|
it(`should convert ${basic_type.label} to mongoose type`, function () {
|
|
51
51
|
let zodSchema = z.object({ test_value: basic_type.zod_function() })
|
|
52
52
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
53
|
-
assert.deepEqual({ test_value: {
|
|
53
|
+
assert.deepEqual({ test_value: { mongoose_type: basic_type.mongoose_type, required: true } }, mongooseSchema)
|
|
54
54
|
});
|
|
55
55
|
|
|
56
56
|
it(`should convert ${basic_type.label} to mongoose type with optional`, function () {
|
|
57
57
|
let zodSchema = z.object({ test_value: basic_type.zod_function().optional() })
|
|
58
58
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
59
|
-
assert.deepEqual({ test_value: {
|
|
59
|
+
assert.deepEqual({ test_value: { mongoose_type: basic_type.mongoose_type, required: false } }, mongooseSchema)
|
|
60
60
|
});
|
|
61
61
|
|
|
62
62
|
it(`should convert ${basic_type.label} to mongoose type with default values`, function () {
|
|
63
63
|
//@ts-ignore
|
|
64
64
|
let zodSchema = z.object({ test_value: basic_type.zod_function().default(basic_type.default_val) })
|
|
65
65
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
66
|
-
assert.deepEqual({ test_value: {
|
|
66
|
+
assert.deepEqual({ test_value: { mongoose_type: basic_type.mongoose_type, required: true, default: basic_type.default_val } }, mongooseSchema)
|
|
67
67
|
});
|
|
68
68
|
|
|
69
69
|
it(`should convert ${basic_type.label} to mongoose type with nullable`, function () {
|
|
70
70
|
let zodSchema = z.object({ test_value: basic_type.zod_function().nullable() })
|
|
71
71
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
72
|
-
assert.deepEqual({ test_value: {
|
|
72
|
+
assert.deepEqual({ test_value: { mongoose_type: basic_type.mongoose_type, required: true } }, mongooseSchema)
|
|
73
73
|
});
|
|
74
74
|
|
|
75
75
|
it(`should convert ${basic_type.label} to mongoose type with optional AND default values`, function () {
|
|
76
76
|
//@ts-ignore
|
|
77
77
|
let zodSchema = z.object({ test_value: basic_type.zod_function().default(basic_type.default_val).optional() })
|
|
78
78
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
79
|
-
assert.deepEqual({ test_value: {
|
|
79
|
+
assert.deepEqual({ test_value: { mongoose_type: basic_type.mongoose_type, required: false, default: basic_type.default_val } }, mongooseSchema)
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
82
|
|
|
@@ -87,20 +87,20 @@ describe('Mongoose from Zod', function () {
|
|
|
87
87
|
it(`should convert a nested object containing a ${basic_type.label} property to mongoose type`, function () {
|
|
88
88
|
let zodSchema = z.object({ test_value: z.object({test_value: basic_type.zod_function() }) })
|
|
89
89
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
90
|
-
assert.deepEqual({ test_value: {
|
|
90
|
+
assert.deepEqual({ test_value: { mongoose_type: {test_value: { mongoose_type: basic_type.mongoose_type, required: true }}, required: true} }, mongooseSchema)
|
|
91
91
|
});
|
|
92
92
|
|
|
93
93
|
it(`should convert a nested object containing a ${basic_type.label} property to mongoose type with optional`, function () {
|
|
94
94
|
let zodSchema = z.object({ test_value: z.object({test_value: basic_type.zod_function().optional() }) })
|
|
95
95
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
96
|
-
assert.deepEqual({ test_value: {
|
|
96
|
+
assert.deepEqual({ test_value: { mongoose_type: {test_value: { mongoose_type: basic_type.mongoose_type, required: false }}, required: true } }, mongooseSchema)
|
|
97
97
|
});
|
|
98
98
|
|
|
99
99
|
it(`should convert a nested object containing a ${basic_type.label} property to mongoose type with default values`, function () {
|
|
100
100
|
//@ts-ignore
|
|
101
101
|
let zodSchema = z.object({ test_value: z.object({test_value: basic_type.zod_function().default(basic_type.default_val) }) })
|
|
102
102
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
103
|
-
assert.deepEqual({ test_value: {
|
|
103
|
+
assert.deepEqual({ test_value: { mongoose_type: {test_value: { mongoose_type: basic_type.mongoose_type, required: true, default: basic_type.default_val }}, required: true } }, mongooseSchema)
|
|
104
104
|
});
|
|
105
105
|
}
|
|
106
106
|
|
|
@@ -111,19 +111,19 @@ describe('Mongoose from Zod', function () {
|
|
|
111
111
|
it(`should convert ${basic_type.label} array to mongoose type`, function () {
|
|
112
112
|
let zodSchema = z.object({ test_value: z.array(basic_type.zod_function()) })
|
|
113
113
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
114
|
-
assert.deepEqual({ test_value: {
|
|
114
|
+
assert.deepEqual({ test_value: {mongoose_type: [{ mongoose_type: basic_type.mongoose_type, required: true }], required: true} }, mongooseSchema)
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
it(`should convert ${basic_type.label} array to mongoose type with optional`, function () {
|
|
118
118
|
let zodSchema = z.object({ test_value: z.array(basic_type.zod_function()).optional() })
|
|
119
119
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
120
|
-
assert.deepEqual({ test_value: {
|
|
120
|
+
assert.deepEqual({ test_value: {mongoose_type: [{ mongoose_type: basic_type.mongoose_type, required: true }], required: false} }, mongooseSchema)
|
|
121
121
|
});
|
|
122
122
|
|
|
123
123
|
it(`should convert ${basic_type.label} array to mongoose type with default values`, function () {
|
|
124
124
|
let zodSchema = z.object({ test_value: z.array(basic_type.zod_function()).default([]) })
|
|
125
125
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
126
|
-
assert.deepEqual({ test_value: {
|
|
126
|
+
assert.deepEqual({ test_value: {mongoose_type: [{ mongoose_type: basic_type.mongoose_type, required: true }], required: true, default: []} }, mongooseSchema)
|
|
127
127
|
});
|
|
128
128
|
}
|
|
129
129
|
|
|
@@ -134,19 +134,19 @@ describe('Mongoose from Zod', function () {
|
|
|
134
134
|
it(`should convert ${basic_type.label} array to mongoose type`, function () {
|
|
135
135
|
let zodSchema = z.object({ test_value: z.array(basic_type.zod_function()) })
|
|
136
136
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
137
|
-
assert.deepEqual({ test_value: {
|
|
137
|
+
assert.deepEqual({ test_value: {mongoose_type: [{ mongoose_type: basic_type.mongoose_type, required: true }], required: true} }, mongooseSchema)
|
|
138
138
|
});
|
|
139
139
|
|
|
140
140
|
it(`should convert ${basic_type.label} array to mongoose type with optional`, function () {
|
|
141
141
|
let zodSchema = z.object({ test_value: z.array(basic_type.zod_function()).optional() })
|
|
142
142
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
143
|
-
assert.deepEqual({ test_value: {
|
|
143
|
+
assert.deepEqual({ test_value: {mongoose_type: [{ mongoose_type: basic_type.mongoose_type, required: true }], required: false} }, mongooseSchema)
|
|
144
144
|
});
|
|
145
145
|
|
|
146
146
|
it(`should convert ${basic_type.label} array to mongoose type with default values`, function () {
|
|
147
147
|
let zodSchema = z.object({ test_value: z.array(basic_type.zod_function()).default([]) })
|
|
148
148
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
149
|
-
assert.deepEqual({ test_value: {
|
|
149
|
+
assert.deepEqual({ test_value: {mongoose_type: [{ mongoose_type: basic_type.mongoose_type, required: true }], required: true, default: []} }, mongooseSchema)
|
|
150
150
|
});
|
|
151
151
|
}
|
|
152
152
|
|
|
@@ -157,20 +157,20 @@ describe('Mongoose from Zod', function () {
|
|
|
157
157
|
it(`should convert record of ${basic_type.label} to mongoose type`, function () {
|
|
158
158
|
let zodSchema = z.object({ test_value: z.record(z.string(), basic_type.zod_function()) })
|
|
159
159
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
160
|
-
assert.deepEqual({ test_value: {
|
|
160
|
+
assert.deepEqual({ test_value: {mongoose_type: Schema.Types.Map, of: { mongoose_type: basic_type.mongoose_type, required: true }, required: true} }, mongooseSchema)
|
|
161
161
|
});
|
|
162
162
|
|
|
163
163
|
it(`should convert record of ${basic_type.label} to mongoose type with optional`, function () {
|
|
164
164
|
let zodSchema = z.object({ test_value: z.record(z.string(), basic_type.zod_function()).optional() })
|
|
165
165
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
166
|
-
assert.deepEqual({ test_value: {
|
|
166
|
+
assert.deepEqual({ test_value: {mongoose_type: Schema.Types.Map, of: { mongoose_type: basic_type.mongoose_type, required: true }, required: false} }, mongooseSchema)
|
|
167
167
|
});
|
|
168
168
|
|
|
169
169
|
it(`should convert record of ${basic_type.label} to mongoose type with default values`, function () {
|
|
170
170
|
//@ts-ignore
|
|
171
171
|
let zodSchema = z.object({ test_value: z.record(z.string(), basic_type.zod_function()).default({}) })
|
|
172
172
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
173
|
-
assert.deepEqual({ test_value: {
|
|
173
|
+
assert.deepEqual({ test_value: {mongoose_type: Schema.Types.Map, of: { mongoose_type: basic_type.mongoose_type, required: true }, required: true, default: {}} }, mongooseSchema)
|
|
174
174
|
});
|
|
175
175
|
}
|
|
176
176
|
|
|
@@ -181,19 +181,19 @@ describe('Mongoose from Zod', function () {
|
|
|
181
181
|
it(`should convert enums to mongoose type`, function () {
|
|
182
182
|
let zodSchema = z.object({ test_value: z.enum(['chunky', 'funky', 'monkey']) })
|
|
183
183
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
184
|
-
assert.deepEqual({ test_value: {
|
|
184
|
+
assert.deepEqual({ test_value: {mongoose_type: String, required: true} }, mongooseSchema)
|
|
185
185
|
});
|
|
186
186
|
|
|
187
187
|
it(`should convert enums to mongoose type with optional`, function () {
|
|
188
188
|
let zodSchema = z.object({ test_value: z.enum(['chunky', 'funky', 'monkey']).optional() })
|
|
189
189
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
190
|
-
assert.deepEqual({ test_value: {
|
|
190
|
+
assert.deepEqual({ test_value: {mongoose_type: String, required: false} }, mongooseSchema)
|
|
191
191
|
});
|
|
192
192
|
|
|
193
193
|
it(`should convert enums to mongoose type with default values`, function () {
|
|
194
194
|
let zodSchema = z.object({ test_value: z.enum(['chunky', 'funky', 'monkey']).default('chunky') })
|
|
195
195
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
196
|
-
assert.deepEqual({ test_value: {
|
|
196
|
+
assert.deepEqual({ test_value: {mongoose_type: String, required: true, default: 'chunky'} }, mongooseSchema)
|
|
197
197
|
});
|
|
198
198
|
|
|
199
199
|
it(`should convert union types to a mixed schema`, function () {
|
|
@@ -201,7 +201,7 @@ describe('Mongoose from Zod', function () {
|
|
|
201
201
|
test_value: z.number().or(z.string())
|
|
202
202
|
})
|
|
203
203
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
204
|
-
assert.deepEqual({ test_value: {
|
|
204
|
+
assert.deepEqual({ test_value: {mongoose_type: Schema.Types.Mixed, required: true} }, mongooseSchema)
|
|
205
205
|
});
|
|
206
206
|
|
|
207
207
|
it(`should convert union types to a mixed schema with default values`, function () {
|
|
@@ -209,7 +209,7 @@ describe('Mongoose from Zod', function () {
|
|
|
209
209
|
test_value: z.number().or(z.string()).default(2)
|
|
210
210
|
})
|
|
211
211
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
212
|
-
assert.deepEqual({ test_value: {
|
|
212
|
+
assert.deepEqual({ test_value: {mongoose_type: Schema.Types.Mixed, required: true, default: 2} }, mongooseSchema)
|
|
213
213
|
});
|
|
214
214
|
|
|
215
215
|
it(`should convert union types to a mixed schema with optional`, function () {
|
|
@@ -217,7 +217,7 @@ describe('Mongoose from Zod', function () {
|
|
|
217
217
|
test_value: z.number().or(z.string()).optional()
|
|
218
218
|
})
|
|
219
219
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
220
|
-
assert.deepEqual({ test_value: {
|
|
220
|
+
assert.deepEqual({ test_value: {mongoose_type: Schema.Types.Mixed, required: false} }, mongooseSchema)
|
|
221
221
|
});
|
|
222
222
|
|
|
223
223
|
it(`should convert recursive schemas`, function () {
|
|
@@ -232,10 +232,10 @@ describe('Mongoose from Zod', function () {
|
|
|
232
232
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
233
233
|
|
|
234
234
|
assert.deepEqual({
|
|
235
|
-
type: {
|
|
236
|
-
operator: {
|
|
237
|
-
children: {
|
|
238
|
-
locked: {
|
|
235
|
+
type: {mongoose_type: String, required: true },
|
|
236
|
+
operator: {mongoose_type: String, required: true },
|
|
237
|
+
children: {mongoose_type: [{ mongoose_type: Schema.Types.Mixed, required: true }], required: true },
|
|
238
|
+
locked: {mongoose_type: Boolean, required: false },
|
|
239
239
|
}, mongooseSchema)
|
|
240
240
|
})
|
|
241
241
|
|
|
@@ -254,9 +254,9 @@ describe('Mongoose from Zod', function () {
|
|
|
254
254
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
255
255
|
|
|
256
256
|
assert.deepEqual({
|
|
257
|
-
a: {
|
|
258
|
-
b: {
|
|
259
|
-
c: {
|
|
257
|
+
a: { mongoose_type: { name: { mongoose_type: String, required: true } }, required: true },
|
|
258
|
+
b: { mongoose_type: { name: { mongoose_type: String, required: true } }, required: true },
|
|
259
|
+
c: { mongoose_type: { name: { mongoose_type: String, required: true } }, required: true },
|
|
260
260
|
}, mongooseSchema)
|
|
261
261
|
})
|
|
262
262
|
});
|