@liminalfunctions/framework 1.0.13 → 1.0.15
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/code_generation/generate_client_library.js +4 -4
- package/dist/code_generation/generate_client_library.js.map +1 -1
- package/dist/code_generation/utils/type_from_zod.d.ts +3 -1
- package/dist/code_generation/utils/type_from_zod.js +44 -24
- package/dist/code_generation/utils/type_from_zod.js.map +1 -1
- package/dist/utils/mongoose_from_zod.d.ts +2 -1
- package/dist/utils/mongoose_from_zod.js +4 -3
- package/dist/utils/mongoose_from_zod.js.map +1 -1
- package/dist/utils/query_validator_from_zod.js +4 -3
- package/dist/utils/query_validator_from_zod.js.map +1 -1
- package/dist/utils/zod_loop_seperator.d.ts +12 -0
- package/dist/utils/zod_loop_seperator.js +67 -0
- package/dist/utils/zod_loop_seperator.js.map +1 -0
- package/package.json +1 -1
- package/src/code_generation/generate_client_library.ts +4 -4
- package/src/code_generation/utils/type_from_zod.ts +53 -26
- package/src/utils/mongoose_from_zod.ts +11 -9
- package/src/utils/query_validator_from_zod.ts +8 -6
- package/src/utils/zod_loop_seperator.ts +91 -0
- package/test/0_0_loop_detect.test.ts +117 -0
- package/test/{0_0_mongoose_from_zod.test.ts → 0_1_mongoose_from_zod.test.ts} +8 -7
- package/test/{0_1_query_validator_from_zod.test.ts → 0_2_query_validator_from_zod.test.ts} +1 -10
- package/test/2_0_client_library_basic_type_generation.test.ts +31 -0
- /package/test/{0_2_query_validator_to_mongodb_query.test.ts → 0_3_query_validator_to_mongodb_query.test.ts} +0 -0
- /package/test/{0_3_cache.test.ts → 0_4_cache.test.ts} +0 -0
|
@@ -21,16 +21,16 @@ export async function generate_client_library(path, collection_registry, service
|
|
|
21
21
|
path_type_post: `types/${get_type_name(collection.collection_id)}_post`,
|
|
22
22
|
};
|
|
23
23
|
let collection_type_definition_builder = [];
|
|
24
|
-
let collection_type_main = type_from_zod(collection.validator
|
|
24
|
+
let collection_type_main = type_from_zod(collection.validator);
|
|
25
25
|
collection_type_definition_builder.push(`export type ${mustache_context.type_return} = ${collection_type_main[0]}`, ...collection_type_main.slice(1));
|
|
26
26
|
let collection_query_type_definition_builder = [];
|
|
27
|
-
let collection_type_query = type_from_zod(collection.query_validator_client
|
|
27
|
+
let collection_type_query = type_from_zod(collection.query_validator_client);
|
|
28
28
|
collection_query_type_definition_builder.push(`export type ${mustache_context.type_query} = ${collection_type_query[0]}`, ...collection_type_query.slice(1));
|
|
29
29
|
let collection_put_type_definition_builder = [];
|
|
30
|
-
let collection_type_put = type_from_zod(collection.put_validator
|
|
30
|
+
let collection_type_put = type_from_zod(collection.put_validator);
|
|
31
31
|
collection_put_type_definition_builder.push(`export type ${mustache_context.type_put} = ${collection_type_put[0]}`, ...collection_type_put.slice(1));
|
|
32
32
|
let collection_post_type_definition_builder = [];
|
|
33
|
-
let collection_type_post = type_from_zod(collection.post_validator
|
|
33
|
+
let collection_type_post = type_from_zod(collection.post_validator);
|
|
34
34
|
collection_post_type_definition_builder.push(`export type ${mustache_context.type_post} = ${collection_type_post[0]}`, ...collection_type_post.slice(1));
|
|
35
35
|
if (!existsSync([path, 'src'].join('/'))) {
|
|
36
36
|
await mkdir([path, 'src'].join('/'));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate_client_library.js","sourceRoot":"","sources":["../../src/code_generation/generate_client_library.ts"],"names":[],"mappings":"AAEA,OAAO,EAAU,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,QAAQ,MAAM,UAAU,CAAA;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAcrC,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAAc,IAAY,EAAE,mBAAuD,EAAE,YAAY,GAAG,iBAAiB;IAC9J,IAAI,WAAW,GAAgB;QAC3B,gBAAgB,EAAE,EA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,aAAa,CAAC,EAAC,CAAC;QAC5C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,GAAG;YACzC,QAAQ,EAAE,EAAE;YACZ,MAAM,EAAE,OAAO;SAClB,CAAA;IACL,CAAC;IAED,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;IAErD,IAAG,CAAC,OAAO,CAAC,UAAU,EAAC,CAAC;QACpB,OAAO,CAAC,UAAU,GAAG,UAAU,CAAC;QAChC,OAAO,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;IAChD,CAAC;IAED,OAAO,OAAO,CAAC;AACnB,CAAC"}
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
|
-
|
|
2
|
+
import { validator_group } from '../../utils/zod_loop_seperator.js';
|
|
3
|
+
export declare function type_from_zod(zod_definition: z.ZodType): string[];
|
|
4
|
+
export declare function parse_zod(zod_definition: z.ZodType, indent_level: number, loop_detector: Map<any, validator_group>, skip_once?: z.core.$ZodTypeDef): string[];
|
|
@@ -1,6 +1,15 @@
|
|
|
1
|
-
import { Schema } from "mongoose";
|
|
2
1
|
import { indent } from "./tab_indent.js";
|
|
3
|
-
|
|
2
|
+
import { find_loops } from '../../utils/zod_loop_seperator.js';
|
|
3
|
+
export function type_from_zod(zod_definition) {
|
|
4
|
+
let loops = find_loops(zod_definition);
|
|
5
|
+
let results = parse_zod(zod_definition, 0, loops);
|
|
6
|
+
for (let [key, loop] of loops.entries()) {
|
|
7
|
+
let loop_type = parse_zod(loop.validator, 0, loops, loop.def);
|
|
8
|
+
results.push(`type ${loop.meta.name} = ${loop_type[0]}`, ...loop_type.slice(1));
|
|
9
|
+
}
|
|
10
|
+
return results;
|
|
11
|
+
}
|
|
12
|
+
export function parse_zod(zod_definition, indent_level, loop_detector, skip_once) {
|
|
4
13
|
if (!zod_definition._zod) {
|
|
5
14
|
console.log('ISSUE');
|
|
6
15
|
console.log(zod_definition);
|
|
@@ -12,7 +21,7 @@ export function type_from_zod(zod_definition, indent_level) {
|
|
|
12
21
|
case "int":
|
|
13
22
|
return ['number'];
|
|
14
23
|
case "object":
|
|
15
|
-
return parse_object(zod_definition._zod.def, indent_level);
|
|
24
|
+
return parse_object(zod_definition._zod.def, indent_level, loop_detector, skip_once);
|
|
16
25
|
case "boolean":
|
|
17
26
|
return ['boolean'];
|
|
18
27
|
case "date":
|
|
@@ -22,19 +31,19 @@ export function type_from_zod(zod_definition, indent_level) {
|
|
|
22
31
|
case "null":
|
|
23
32
|
return ['null'];
|
|
24
33
|
case "array":
|
|
25
|
-
return parse_array(zod_definition._zod.def, indent_level);
|
|
34
|
+
return parse_array(zod_definition._zod.def, indent_level, loop_detector, skip_once);
|
|
26
35
|
case "nullable":
|
|
27
|
-
return [`${
|
|
36
|
+
return [`${parse_zod(zod_definition._zod.def.innerType, indent_level, loop_detector, skip_once)} | null`];
|
|
28
37
|
case "union":
|
|
29
|
-
return parse_union(zod_definition._zod.def, indent_level);
|
|
38
|
+
return parse_union(zod_definition._zod.def, indent_level, loop_detector, skip_once);
|
|
30
39
|
case "record":
|
|
31
|
-
return parse_record(zod_definition._zod.def, indent_level);
|
|
40
|
+
return parse_record(zod_definition._zod.def, indent_level, loop_detector, skip_once);
|
|
32
41
|
case "enum":
|
|
33
42
|
return parse_enum(zod_definition._zod.def);
|
|
34
43
|
case "readonly":
|
|
35
44
|
throw new Error(`Zod type not yet supported by type_from_zod: ${zod_definition._zod.def.type});`);
|
|
36
45
|
case "default":
|
|
37
|
-
return
|
|
46
|
+
return parse_zod(zod_definition._zod.def.innerType, indent_level, loop_detector, skip_once);
|
|
38
47
|
case "custom":
|
|
39
48
|
let result = [];
|
|
40
49
|
if (!zod_definition.meta()) {
|
|
@@ -52,7 +61,19 @@ export function type_from_zod(zod_definition, indent_level) {
|
|
|
52
61
|
throw new Error("Cannot process zod type: " + zod_definition._zod.def.type);
|
|
53
62
|
}
|
|
54
63
|
}
|
|
55
|
-
function parse_object(def, indent_level) {
|
|
64
|
+
function parse_object(def, indent_level, loop_detector, skip_once) {
|
|
65
|
+
if (loop_detector.has(def) && def !== skip_once) {
|
|
66
|
+
let loop = loop_detector.get(def);
|
|
67
|
+
let zod_object = loop.validator;
|
|
68
|
+
if (!loop.meta.name && zod_object.meta()?.id) {
|
|
69
|
+
loop.meta.name = `type_${zod_object.meta().id}`;
|
|
70
|
+
}
|
|
71
|
+
if (!loop.meta.name) {
|
|
72
|
+
loop.meta.name = `type_${randomString()}`;
|
|
73
|
+
}
|
|
74
|
+
return [loop.meta.name];
|
|
75
|
+
}
|
|
76
|
+
;
|
|
56
77
|
let retval = ['{'];
|
|
57
78
|
for (let [key, value] of Object.entries(def.shape)) {
|
|
58
79
|
let key_phrase = (value.safeParse(undefined).success || value._zod.def.type === 'optional') ? `"${key}"?:` : `"${key}":`;
|
|
@@ -60,7 +81,7 @@ function parse_object(def, indent_level) {
|
|
|
60
81
|
while (non_optional_type._zod.def.type === 'optional') {
|
|
61
82
|
non_optional_type = non_optional_type._zod.def.innerType;
|
|
62
83
|
}
|
|
63
|
-
let type_value =
|
|
84
|
+
let type_value = parse_zod(non_optional_type, indent_level + 1, loop_detector, def === skip_once ? undefined : skip_once);
|
|
64
85
|
if (type_value.length > 1) {
|
|
65
86
|
retval.push(indent(indent_level + 1, `${key_phrase} ${type_value[0]}`));
|
|
66
87
|
retval.push(...type_value.slice(1));
|
|
@@ -72,18 +93,18 @@ function parse_object(def, indent_level) {
|
|
|
72
93
|
retval.push(indent(indent_level, '}'));
|
|
73
94
|
return retval;
|
|
74
95
|
}
|
|
75
|
-
function parse_array(def, indent_level) {
|
|
76
|
-
let retval =
|
|
96
|
+
function parse_array(def, indent_level, loop_detector, skip_once) {
|
|
97
|
+
let retval = parse_zod(def.element, indent_level + 1, loop_detector, skip_once);
|
|
77
98
|
retval[retval.length - 1] = `${retval[retval.length - 1]}[]`;
|
|
78
99
|
return retval;
|
|
79
100
|
}
|
|
80
101
|
function parse_enum(def) {
|
|
81
102
|
return [`("${Object.values(def.entries).join('" | "')}")`];
|
|
82
103
|
}
|
|
83
|
-
function parse_record(def, indent_level) {
|
|
104
|
+
function parse_record(def, indent_level, loop_detector, skip_once) {
|
|
84
105
|
let retval = ['{'];
|
|
85
|
-
let key_phrase = `[key: ${
|
|
86
|
-
let type_value =
|
|
106
|
+
let key_phrase = `[key: ${parse_zod(def.keyType, indent_level + 1, loop_detector, skip_once)}]:`;
|
|
107
|
+
let type_value = parse_zod(def.valueType, indent_level + 1, loop_detector, skip_once);
|
|
87
108
|
if (type_value.length > 1) {
|
|
88
109
|
retval.push(indent(indent_level + 1, `${key_phrase} ${type_value[0]}`));
|
|
89
110
|
retval.push(...type_value.slice(1));
|
|
@@ -94,8 +115,8 @@ function parse_record(def, indent_level) {
|
|
|
94
115
|
retval.push(indent(indent_level, '}'));
|
|
95
116
|
return retval;
|
|
96
117
|
}
|
|
97
|
-
function parse_union(def, indent_level) {
|
|
98
|
-
let results = def.options.map(ele =>
|
|
118
|
+
function parse_union(def, indent_level, loop_detector, skip_once) {
|
|
119
|
+
let results = def.options.map(ele => parse_zod(ele, indent_level, loop_detector, skip_once));
|
|
99
120
|
let out = [];
|
|
100
121
|
for (let q = 0; q < results.length; q++) {
|
|
101
122
|
out.push(...results[q]);
|
|
@@ -105,12 +126,11 @@ function parse_union(def, indent_level) {
|
|
|
105
126
|
}
|
|
106
127
|
return out;
|
|
107
128
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return { type: Schema.Types.ObjectId };
|
|
129
|
+
const random_string_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
130
|
+
function randomString(length = 16) {
|
|
131
|
+
var result = '';
|
|
132
|
+
for (let i = length; i > 0; --i)
|
|
133
|
+
result += random_string_chars[Math.floor(Math.random() * random_string_chars.length)];
|
|
134
|
+
return result;
|
|
115
135
|
}
|
|
116
136
|
//# sourceMappingURL=type_from_zod.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type_from_zod.js","sourceRoot":"","sources":["../../../src/code_generation/utils/type_from_zod.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"type_from_zod.js","sourceRoot":"","sources":["../../../src/code_generation/utils/type_from_zod.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,UAAU,EAAmB,MAAM,mCAAmC,CAAA;AAe/E,MAAM,UAAU,aAAa,CAAC,cAAyB;IACnD,IAAI,KAAK,GAAG,UAAU,CAAC,cAA2B,CAAC,CAAC;IACpD,IAAI,OAAO,GAAG,SAAS,CAAC,cAAc,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;IAElD,KAAI,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAC,CAAC;QACpC,IAAI,SAAS,GAAG,SAAS,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,GAAoC,CAAC,CAAC;QAE/F,OAAO,CAAC,IAAI,CAAC,QAAQ,IAAI,CAAC,IAAI,CAAC,IAAc,MAAM,SAAS,CAAC,CAAC,CAAC,EAAE,EAC7D,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;IAM9B,CAAC;IAED,OAAO,OAAO,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,cAAyB,EAAE,YAAoB,EAAE,aAAwC,EAAE,SAA8B;IAC/I,IAAG,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC;QACtB,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACrB,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAChC,CAAC;IAED,QAAQ,cAAc,CAAC,IAAI,CAAC,GA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AAE;IACrC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;QAAE,MAAM,IAAI,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAC;IACvH,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
2
|
import mongoose from "mongoose";
|
|
3
|
+
import { validator_group } from './zod_loop_seperator.js';
|
|
3
4
|
export declare const z_mongodb_id: z.ZodCustom<string, string>;
|
|
4
5
|
export declare const z_mongodb_id_optional: z.ZodCustom<string, string>;
|
|
5
6
|
export declare function mongoose_from_zod<T>(schema_name: string, zod_definition: z.core.$ZodType): mongoose.Model<T, {}, {}, {}, mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T, {}, {}> & (mongoose.Require_id<T> extends infer T_1 ? T_1 extends mongoose.Require_id<T> ? T_1 extends {
|
|
@@ -8,4 +9,4 @@ export declare function mongoose_from_zod<T>(schema_name: string, zod_definition
|
|
|
8
9
|
__v: number;
|
|
9
10
|
} : never : never)>, any>;
|
|
10
11
|
export declare function schema_from_zod(zod_definition: z.core.$ZodType): any;
|
|
11
|
-
export declare function schema_entry_from_zod(zod_definition: z.ZodType, loop_detector
|
|
12
|
+
export declare function schema_entry_from_zod(zod_definition: z.ZodType, loop_detector: Map<any, validator_group>): any;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
2
|
import mongoose, { Schema } from "mongoose";
|
|
3
|
+
import { find_loops } from './zod_loop_seperator.js';
|
|
3
4
|
const underlying_mongodb_id_validator = z.string().length(24);
|
|
4
5
|
const underlying_mongodb_id_validator_optional = underlying_mongodb_id_validator.optional();
|
|
5
6
|
export const z_mongodb_id = z.custom((val) => {
|
|
@@ -34,12 +35,13 @@ export function mongoose_from_zod(schema_name, zod_definition) {
|
|
|
34
35
|
return mongoose.model(schema_name, new Schema(mongoose_schema, { typeKey: 'mongoose_type' }));
|
|
35
36
|
}
|
|
36
37
|
export function schema_from_zod(zod_definition) {
|
|
37
|
-
let
|
|
38
|
+
let loops = find_loops(zod_definition);
|
|
39
|
+
let mongoose_schema = schema_entry_from_zod(zod_definition, loops);
|
|
38
40
|
delete mongoose_schema.mongoose_type.required;
|
|
39
41
|
delete mongoose_schema.mongoose_type._id;
|
|
40
42
|
return mongoose_schema.mongoose_type;
|
|
41
43
|
}
|
|
42
|
-
export function schema_entry_from_zod(zod_definition, loop_detector
|
|
44
|
+
export function schema_entry_from_zod(zod_definition, loop_detector) {
|
|
43
45
|
if (!zod_definition) {
|
|
44
46
|
console.error('ISSUE');
|
|
45
47
|
console.error(zod_definition);
|
|
@@ -121,7 +123,6 @@ function parse_object(def, loop_detector) {
|
|
|
121
123
|
if (loop_detector.has(def)) {
|
|
122
124
|
return { mongoose_type: Schema.Types.Mixed, required: true };
|
|
123
125
|
}
|
|
124
|
-
loop_detector.add(def);
|
|
125
126
|
let retval = {};
|
|
126
127
|
for (let [key, value] of Object.entries(def.shape)) {
|
|
127
128
|
retval[key] = schema_entry_from_zod(value, loop_detector);
|
|
@@ -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;
|
|
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,C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IAAI,MAAM,GAAG,EAAE,aAAa,EAAE,OAAO,EAAS,CAAC;IAC/C,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,SAAS,UAAU,CAAC,GAAuB;IACvC,IAAI,MAAM,GAAG,EAAE,aAAa,EAAE,IAAI,EAAS,CAAC;IAC5C,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,SAAS,aAAa,CAAC,GAA0B,EAAE,aAAwC;IAEvF,IAAI,eAAe,GAAG,qBAAqB,CAAC,GAAG,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;IAC1E,eAAe,CAAC,OAAO,GAAG,GAAG,CAAC,YAAY,CAAC;IAC3C,OAAO,eAAe,CAAC;AAC3B,CAAC;AAED,SAAS,cAAc,CAAC,GAA2B,EAAE,aAAwC;IAEzF,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"}
|
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
2
|
import { z_mongodb_id } from "./mongoose_from_zod.js";
|
|
3
|
+
import { find_loops } from './zod_loop_seperator.js';
|
|
3
4
|
export function query_validator_from_zod(zod_definition, mode = 'server') {
|
|
5
|
+
let loops = find_loops(zod_definition);
|
|
4
6
|
let retval = {
|
|
5
7
|
limit: z.coerce.number().int().optional(),
|
|
6
8
|
cursor: z_mongodb_id.optional(),
|
|
7
9
|
sort_order: z.enum(['ascending', 'descending']).optional()
|
|
8
10
|
};
|
|
9
|
-
let object_filters = parse_object(zod_definition._zod.def, '',
|
|
11
|
+
let object_filters = parse_object(zod_definition._zod.def, '', loops, mode);
|
|
10
12
|
for (let filter of object_filters) {
|
|
11
13
|
retval[filter.path.slice(1)] = filter.filter;
|
|
12
14
|
}
|
|
13
15
|
retval.sort = z.enum(object_filters.filter(ele => ele.sortable).map(ele => ele.path.slice(1))).optional();
|
|
14
16
|
return z.object(retval).strict();
|
|
15
17
|
}
|
|
16
|
-
function parse_any(zod_definition, prefix, loop_detector
|
|
18
|
+
function parse_any(zod_definition, prefix, loop_detector, mode = 'server') {
|
|
17
19
|
switch (zod_definition._zod.def.type) {
|
|
18
20
|
case "enum":
|
|
19
21
|
return parse_enum(zod_definition._zod.def, prefix, mode);
|
|
@@ -69,7 +71,6 @@ function parse_object(def, prefix, loop_detector, mode) {
|
|
|
69
71
|
if (loop_detector.has(def)) {
|
|
70
72
|
return [];
|
|
71
73
|
}
|
|
72
|
-
loop_detector.add(def);
|
|
73
74
|
let retval = [];
|
|
74
75
|
for (let [key, value] of Object.entries(def.shape)) {
|
|
75
76
|
let filters = parse_any(value, `${prefix}.${key}`, loop_detector, mode);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query_validator_from_zod.js","sourceRoot":"","sources":["../../src/utils/query_validator_from_zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAA;AAE1B,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"query_validator_from_zod.js","sourceRoot":"","sources":["../../src/utils/query_validator_from_zod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAA;AAE1B,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,UAAU,EAAmB,MAAM,yBAAyB,CAAA;AASrE,MAAM,UAAU,wBAAwB,CAAC,cAA2B,EAAE,OAAa,QAAQ;IACvF,IAAI,KAAK,GAAG,UAAU,CAAC,cAA2B,CAAC,CAAC;IAEpD,IAAI,MAAM,GAAG;QACT,KAAK,EAAE,CA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AAE;YAC/B,QAAQ,EAAE,KAAK;SAClB;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from "zod/v4";
|
|
2
|
+
export type validator_group = {
|
|
3
|
+
handle: string;
|
|
4
|
+
validator: z.ZodType;
|
|
5
|
+
def: z.core.$ZodType;
|
|
6
|
+
appearances: number;
|
|
7
|
+
meta: {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare function is_validator_group(candidate: any): boolean;
|
|
12
|
+
export declare function find_loops(zod_definition: z.ZodType): Map<any, validator_group>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export function is_validator_group(candidate) {
|
|
2
|
+
return typeof candidate.handle === 'string' && candidate.validator;
|
|
3
|
+
}
|
|
4
|
+
export function find_loops(zod_definition) {
|
|
5
|
+
let potential_loops = discover_loops(zod_definition);
|
|
6
|
+
for (let [key, value] of potential_loops.entries()) {
|
|
7
|
+
if (value.appearances <= 1) {
|
|
8
|
+
potential_loops.delete(key);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return potential_loops;
|
|
12
|
+
}
|
|
13
|
+
function discover_loops(zod_definition, validator_groups = new Map()) {
|
|
14
|
+
if (!zod_definition) {
|
|
15
|
+
console.error('ISSUE');
|
|
16
|
+
console.error(zod_definition);
|
|
17
|
+
}
|
|
18
|
+
switch (zod_definition._zod.def.type) {
|
|
19
|
+
case "object":
|
|
20
|
+
parse_object(zod_definition, validator_groups);
|
|
21
|
+
break;
|
|
22
|
+
case "array":
|
|
23
|
+
discover_loops(zod_definition._zod.def.element, validator_groups);
|
|
24
|
+
break;
|
|
25
|
+
case "nullable":
|
|
26
|
+
case "optional":
|
|
27
|
+
case "default":
|
|
28
|
+
discover_loops(zod_definition._zod.def.innerType, validator_groups);
|
|
29
|
+
break;
|
|
30
|
+
case "record":
|
|
31
|
+
parse_record(zod_definition._zod.def, validator_groups);
|
|
32
|
+
break;
|
|
33
|
+
case "union":
|
|
34
|
+
parse_union(zod_definition._zod.def, validator_groups);
|
|
35
|
+
break;
|
|
36
|
+
default:
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
return validator_groups;
|
|
40
|
+
}
|
|
41
|
+
function parse_object(object_validator, validator_groups) {
|
|
42
|
+
let def = object_validator._zod.def;
|
|
43
|
+
if (validator_groups.has(def)) {
|
|
44
|
+
validator_groups.get(def).appearances++;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
validator_groups.set(def, {
|
|
48
|
+
appearances: 1,
|
|
49
|
+
handle: ``,
|
|
50
|
+
validator: object_validator,
|
|
51
|
+
def: def,
|
|
52
|
+
meta: {},
|
|
53
|
+
});
|
|
54
|
+
for (let [key, value] of Object.entries(def.shape)) {
|
|
55
|
+
discover_loops(value, validator_groups);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function parse_record(def, validator_groups) {
|
|
59
|
+
discover_loops(def.keyType, validator_groups);
|
|
60
|
+
discover_loops(def.valueType, validator_groups);
|
|
61
|
+
}
|
|
62
|
+
function parse_union(def, validator_groups) {
|
|
63
|
+
for (let option of def.options) {
|
|
64
|
+
discover_loops(option, validator_groups);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=zod_loop_seperator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod_loop_seperator.js","sourceRoot":"","sources":["../../src/utils/zod_loop_seperator.ts"],"names":[],"mappings":"AAWA,MAAM,UAAU,kBAAkB,CAAC,SAAc;IAC7C,OAAO,OAAO,SAAS,CAAC,MAAM,KAAK,QAAQ,IAAI,SAAS,CAAC,SAAS,CAAC;AACvE,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,cAAyB;IAChD,IAAI,eAAe,GAAG,cAAc,CAAC,cAAc,CAAC,CAAC;IACrD,KAAI,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,eAAe,CAAC,O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GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;QAC3B,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;QACxC,OAAO;IACX,CAAC;IACD,gBAAgB,CAAC,GAAG,CAAC,GAAG,EAAE;QACtB,WAAW,EAAE,CAAC;QACd,MAAM,EAAE,EAAE;QACV,SAAS,EAAE,gBAAgB;QAE3B,GAAG,EAAE,GAAG;QACR,IAAI,EAAE,EAAE;KACX,CAAC,CAAA;IACF,KAAI,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAC,CAAC;QAE/C,cAAc,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC;IAC5C,CAAC;AACL,CAAC;AAED,SAAS,YAAY,CAAC,GAAyB,EAAE,gBAA2C;IAExF,cAAc,CAAC,GAAG,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC;IAG9C,cAAc,CAAC,GAAG,CAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC;AACpD,CAAC;AAED,SAAS,WAAW,CAAC,GAAwB,EAAE,gBAA2C;IACtF,KAAI,IAAI,MAAM,IAAI,GAAG,CAAC,OAAO,EAAC,CAAC;QAE3B,cAAc,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;IAC7C,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -44,19 +44,19 @@ export async function generate_client_library<Collections>(path: string, collect
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
let collection_type_definition_builder = [] as string[];
|
|
47
|
-
let collection_type_main = type_from_zod(collection.validator
|
|
47
|
+
let collection_type_main = type_from_zod(collection.validator);
|
|
48
48
|
collection_type_definition_builder.push(`export type ${mustache_context.type_return} = ${collection_type_main[0]}`, ...collection_type_main.slice(1));
|
|
49
49
|
|
|
50
50
|
let collection_query_type_definition_builder = [] as string[];
|
|
51
|
-
let collection_type_query = type_from_zod(collection.query_validator_client
|
|
51
|
+
let collection_type_query = type_from_zod(collection.query_validator_client);
|
|
52
52
|
collection_query_type_definition_builder.push(`export type ${mustache_context.type_query} = ${collection_type_query[0]}`, ...collection_type_query.slice(1));
|
|
53
53
|
|
|
54
54
|
let collection_put_type_definition_builder = [] as string[];
|
|
55
|
-
let collection_type_put = type_from_zod(collection.put_validator
|
|
55
|
+
let collection_type_put = type_from_zod(collection.put_validator);
|
|
56
56
|
collection_put_type_definition_builder.push(`export type ${mustache_context.type_put} = ${collection_type_put[0]}`, ...collection_type_put.slice(1));
|
|
57
57
|
|
|
58
58
|
let collection_post_type_definition_builder = [] as string[];
|
|
59
|
-
let collection_type_post = type_from_zod(collection.post_validator
|
|
59
|
+
let collection_type_post = type_from_zod(collection.post_validator);
|
|
60
60
|
collection_post_type_definition_builder.push(`export type ${mustache_context.type_post} = ${collection_type_post[0]}`, ...collection_type_post.slice(1));
|
|
61
61
|
|
|
62
62
|
if(!existsSync([path, 'src'].join('/'))){ await mkdir([path, 'src'].join('/')); }
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { z, ZodType } from "zod/v4"
|
|
1
|
+
import { z, ZodObject, ZodType } from "zod/v4"
|
|
2
2
|
import mongoose, { Schema } from "mongoose";
|
|
3
|
-
import { indent } from "./tab_indent.js"
|
|
3
|
+
import { indent } from "./tab_indent.js";
|
|
4
|
+
import { find_loops, validator_group } from '../../utils/zod_loop_seperator.js'
|
|
5
|
+
|
|
4
6
|
|
|
5
7
|
/*export function mongoose_from_zod<T>(schema_name: string, zod_definition: z.core.$ZodType) {
|
|
6
8
|
let mongoose_schema = schema_from_zod(zod_definition);
|
|
@@ -14,7 +16,26 @@ import { indent } from "./tab_indent.js"
|
|
|
14
16
|
return mongoose_schema.type;
|
|
15
17
|
}*/
|
|
16
18
|
|
|
17
|
-
export function type_from_zod(zod_definition: z.ZodType
|
|
19
|
+
export function type_from_zod(zod_definition: z.ZodType){
|
|
20
|
+
let loops = find_loops(zod_definition as z.ZodType);
|
|
21
|
+
let results = parse_zod(zod_definition, 0, loops);
|
|
22
|
+
|
|
23
|
+
for(let [key, loop] of loops.entries()){
|
|
24
|
+
let loop_type = parse_zod(loop.validator, 0, loops, loop.def as unknown as z.core.$ZodTypeDef);
|
|
25
|
+
|
|
26
|
+
results.push(`type ${loop.meta.name as string} = ${loop_type[0]}`,
|
|
27
|
+
...loop_type.slice(1))
|
|
28
|
+
/*results = [
|
|
29
|
+
`type ${loop.meta.name as string} = ${loop_type[0]}`,
|
|
30
|
+
...loop_type.slice(1),
|
|
31
|
+
...results
|
|
32
|
+
]*/
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return results;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function parse_zod(zod_definition: z.ZodType, indent_level: number, loop_detector: Map<any, validator_group>, skip_once?: z.core.$ZodTypeDef): string[] {
|
|
18
39
|
if(!zod_definition._zod) {
|
|
19
40
|
console.log('ISSUE');
|
|
20
41
|
console.log(zod_definition);
|
|
@@ -27,7 +48,7 @@ export function type_from_zod(zod_definition: z.ZodType, indent_level: number):
|
|
|
27
48
|
case "int":
|
|
28
49
|
return ['number'];
|
|
29
50
|
case "object":
|
|
30
|
-
return parse_object(zod_definition._zod.def as z.core.$ZodObjectDef, indent_level);
|
|
51
|
+
return parse_object(zod_definition._zod.def as z.core.$ZodObjectDef, indent_level, loop_detector, skip_once);
|
|
31
52
|
case "boolean":
|
|
32
53
|
return ['boolean'];
|
|
33
54
|
case "date":
|
|
@@ -37,23 +58,23 @@ export function type_from_zod(zod_definition: z.ZodType, indent_level: number):
|
|
|
37
58
|
case "null":
|
|
38
59
|
return ['null']
|
|
39
60
|
case "array":
|
|
40
|
-
return parse_array(zod_definition._zod.def as z.core.$ZodArrayDef, indent_level)
|
|
61
|
+
return parse_array(zod_definition._zod.def as z.core.$ZodArrayDef, indent_level, loop_detector, skip_once)
|
|
41
62
|
/*
|
|
42
63
|
case "any":
|
|
43
64
|
return ["any"]*/
|
|
44
65
|
case "nullable":
|
|
45
66
|
//@ts-expect-error
|
|
46
|
-
return [`${
|
|
67
|
+
return [`${parse_zod((zod_definition._zod.def as z.core.$ZodNullable).innerType as ZodType, indent_level, loop_detector, skip_once)} | null`]
|
|
47
68
|
case "union":
|
|
48
|
-
return parse_union(zod_definition._zod.def as z.core.$ZodUnionDef, indent_level);
|
|
69
|
+
return parse_union(zod_definition._zod.def as z.core.$ZodUnionDef, indent_level, loop_detector, skip_once);
|
|
49
70
|
case "record":
|
|
50
|
-
return parse_record(zod_definition._zod.def as z.core.$ZodRecordDef, indent_level);
|
|
71
|
+
return parse_record(zod_definition._zod.def as z.core.$ZodRecordDef, indent_level, loop_detector, skip_once);
|
|
51
72
|
case "enum":
|
|
52
73
|
return parse_enum(zod_definition._zod.def as z.core.$ZodEnumDef)
|
|
53
74
|
case "readonly":
|
|
54
75
|
throw new Error(`Zod type not yet supported by type_from_zod: ${zod_definition._zod.def.type});`)
|
|
55
76
|
case "default":
|
|
56
|
-
return
|
|
77
|
+
return parse_zod((zod_definition._zod.def as z.core.$ZodDefaultDef).innerType as ZodType, indent_level, loop_detector, skip_once);
|
|
57
78
|
case "custom":
|
|
58
79
|
let result = [];
|
|
59
80
|
if(!zod_definition.meta()) {
|
|
@@ -74,7 +95,16 @@ export function type_from_zod(zod_definition: z.ZodType, indent_level: number):
|
|
|
74
95
|
}
|
|
75
96
|
}
|
|
76
97
|
|
|
77
|
-
function parse_object(def: z.core.$ZodObjectDef, indent_level: number): string[] {
|
|
98
|
+
function parse_object(def: z.core.$ZodObjectDef, indent_level: number, loop_detector: Map<any, validator_group>, skip_once: z.core.$ZodTypeDef): string[] {
|
|
99
|
+
if(loop_detector.has(def) && def !== skip_once){
|
|
100
|
+
let loop = loop_detector.get(def);
|
|
101
|
+
let zod_object = loop.validator;
|
|
102
|
+
//@ts-ignore
|
|
103
|
+
if(!loop.meta.name && zod_object.meta()?.id) { loop.meta.name = `type_${zod_object.meta().id}` }
|
|
104
|
+
if(!loop.meta.name) { loop.meta.name = `type_${randomString()}` }
|
|
105
|
+
return [ loop.meta.name ];
|
|
106
|
+
};
|
|
107
|
+
|
|
78
108
|
let retval = ['{']
|
|
79
109
|
for(let [key, value] of Object.entries(def.shape)){
|
|
80
110
|
//@ts-ignore
|
|
@@ -84,7 +114,7 @@ function parse_object(def: z.core.$ZodObjectDef, indent_level: number): string[]
|
|
|
84
114
|
|
|
85
115
|
//@ts-ignore
|
|
86
116
|
while(non_optional_type._zod.def.type === 'optional'){ non_optional_type = non_optional_type._zod.def.innerType;}
|
|
87
|
-
let type_value =
|
|
117
|
+
let type_value = parse_zod(non_optional_type as ZodType, indent_level + 1, loop_detector, def === skip_once ? undefined : skip_once)
|
|
88
118
|
|
|
89
119
|
if(type_value.length > 1 ){
|
|
90
120
|
retval.push(indent(indent_level + 1, `${key_phrase} ${type_value[0]}`))
|
|
@@ -98,9 +128,9 @@ function parse_object(def: z.core.$ZodObjectDef, indent_level: number): string[]
|
|
|
98
128
|
return retval;
|
|
99
129
|
}
|
|
100
130
|
|
|
101
|
-
function parse_array(def: z.core.$ZodArrayDef, indent_level: number): any {
|
|
131
|
+
function parse_array(def: z.core.$ZodArrayDef, indent_level: number, loop_detector: Map<any, validator_group>, skip_once: z.core.$ZodTypeDef): any {
|
|
102
132
|
//@ts-ignore
|
|
103
|
-
let retval =
|
|
133
|
+
let retval = parse_zod(def.element as z.ZodType, indent_level + 1, loop_detector, skip_once)
|
|
104
134
|
retval[retval.length - 1] = `${retval[retval.length - 1]}[]`
|
|
105
135
|
return retval;
|
|
106
136
|
}
|
|
@@ -109,13 +139,13 @@ function parse_enum(def: z.core.$ZodEnumDef): any {
|
|
|
109
139
|
return [ `("${Object.values(def.entries).join('" | "')}")`];
|
|
110
140
|
}
|
|
111
141
|
|
|
112
|
-
function parse_record(def: z.core.$ZodRecordDef, indent_level: number): any {
|
|
142
|
+
function parse_record(def: z.core.$ZodRecordDef, indent_level: number, loop_detector: Map<any, validator_group>, skip_once: z.core.$ZodTypeDef): any {
|
|
113
143
|
let retval = ['{']
|
|
114
144
|
//@ts-ignore
|
|
115
|
-
let key_phrase = `[key: ${
|
|
145
|
+
let key_phrase = `[key: ${parse_zod(def.keyType, indent_level + 1, loop_detector, skip_once)}]:`;
|
|
116
146
|
|
|
117
147
|
//@ts-ignore
|
|
118
|
-
let type_value =
|
|
148
|
+
let type_value = parse_zod(def.valueType, indent_level + 1, loop_detector, skip_once)
|
|
119
149
|
|
|
120
150
|
if(type_value.length > 1 ){
|
|
121
151
|
retval.push(indent(indent_level + 1, `${key_phrase} ${type_value[0]}`))
|
|
@@ -128,8 +158,8 @@ function parse_record(def: z.core.$ZodRecordDef, indent_level: number): any {
|
|
|
128
158
|
return retval;
|
|
129
159
|
}
|
|
130
160
|
|
|
131
|
-
function parse_union(def: z.core.$ZodUnionDef, indent_level: number): any {
|
|
132
|
-
let results = def.options.map(ele =>
|
|
161
|
+
function parse_union(def: z.core.$ZodUnionDef, indent_level: number, loop_detector: Map<any, validator_group>, skip_once: z.core.$ZodTypeDef): any {
|
|
162
|
+
let results = def.options.map(ele => parse_zod(ele as ZodType, indent_level, loop_detector, skip_once));
|
|
133
163
|
let out = [];
|
|
134
164
|
for(let q = 0; q < results.length; q++) {
|
|
135
165
|
out.push(...results[q]);
|
|
@@ -140,13 +170,10 @@ function parse_union(def: z.core.$ZodUnionDef, indent_level: number): any {
|
|
|
140
170
|
return out;
|
|
141
171
|
}
|
|
142
172
|
|
|
143
|
-
function parse_optional(def: z.core.$ZodOptionalDef): any {
|
|
144
|
-
//@ts-ignore
|
|
145
|
-
let type_definition = schema_entry_from_zod(def.innerType);
|
|
146
|
-
type_definition.required = false;
|
|
147
|
-
return type_definition;
|
|
148
|
-
}
|
|
149
173
|
|
|
150
|
-
|
|
151
|
-
|
|
174
|
+
const random_string_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
175
|
+
function randomString(length: number = 16) {
|
|
176
|
+
var result = '';
|
|
177
|
+
for (let i = length; i > 0; --i) result += random_string_chars[Math.floor(Math.random() * random_string_chars.length)];
|
|
178
|
+
return result;
|
|
152
179
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { z } from "zod/v4"
|
|
2
2
|
import mongoose, { Schema } from "mongoose";
|
|
3
3
|
|
|
4
|
+
import { find_loops, validator_group } from './zod_loop_seperator.js'
|
|
5
|
+
|
|
4
6
|
//export const z_mongodb_id = z.string().length(24).describe('F_Mongodb_ID');
|
|
5
7
|
//export const mongodb_id = () => z_mongodb_id;
|
|
6
8
|
const underlying_mongodb_id_validator = z.string().length(24);
|
|
@@ -37,13 +39,14 @@ export function mongoose_from_zod<T>(schema_name: string, zod_definition: z.core
|
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
export function schema_from_zod(zod_definition: z.core.$ZodType): any {
|
|
40
|
-
let
|
|
42
|
+
let loops = find_loops(zod_definition as z.ZodType);
|
|
43
|
+
let mongoose_schema = schema_entry_from_zod(zod_definition as z.ZodType, loops);
|
|
41
44
|
delete mongoose_schema.mongoose_type.required;
|
|
42
45
|
delete mongoose_schema.mongoose_type._id;
|
|
43
46
|
return mongoose_schema.mongoose_type;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
|
-
export function schema_entry_from_zod(zod_definition: z.ZodType, loop_detector:
|
|
49
|
+
export function schema_entry_from_zod(zod_definition: z.ZodType, loop_detector: Map<any, validator_group>): any {
|
|
47
50
|
if(!zod_definition) {
|
|
48
51
|
console.error('ISSUE');
|
|
49
52
|
console.error(zod_definition);
|
|
@@ -126,11 +129,10 @@ export function schema_entry_from_zod(zod_definition: z.ZodType, loop_detector:
|
|
|
126
129
|
}
|
|
127
130
|
}
|
|
128
131
|
|
|
129
|
-
function parse_object(def: z.core.$ZodObjectDef, loop_detector:
|
|
132
|
+
function parse_object(def: z.core.$ZodObjectDef, loop_detector: Map<any, validator_group> ): any {
|
|
130
133
|
if(loop_detector.has(def)) {
|
|
131
|
-
return {mongoose_type: Schema.Types.Mixed, required: true}
|
|
134
|
+
return { mongoose_type: Schema.Types.Mixed, required: true }
|
|
132
135
|
}
|
|
133
|
-
loop_detector.add(def);
|
|
134
136
|
|
|
135
137
|
let retval = {} as any;
|
|
136
138
|
for(let [key, value] of Object.entries(def.shape)){
|
|
@@ -140,7 +142,7 @@ function parse_object(def: z.core.$ZodObjectDef, loop_detector: Set<any>): any {
|
|
|
140
142
|
return {mongoose_type: retval, required: true};
|
|
141
143
|
}
|
|
142
144
|
|
|
143
|
-
function parse_array(def: z.core.$ZodArrayDef, loop_detector:
|
|
145
|
+
function parse_array(def: z.core.$ZodArrayDef, loop_detector: Map<any, validator_group> ): any {
|
|
144
146
|
//@ts-ignore
|
|
145
147
|
let retval = { mongoose_type: [schema_entry_from_zod(def.element, loop_detector)] } as any;
|
|
146
148
|
retval.required = true;
|
|
@@ -159,7 +161,7 @@ function parse_union(def: z.core.$ZodUnionDef): any {
|
|
|
159
161
|
return retval;
|
|
160
162
|
}
|
|
161
163
|
|
|
162
|
-
function parse_record(def: z.core.$ZodRecordDef, loop_detector:
|
|
164
|
+
function parse_record(def: z.core.$ZodRecordDef, loop_detector: Map<any, validator_group> ): any {
|
|
163
165
|
if(def.keyType._zod.def.type !== 'string') { throw new Error('mongoDB only supports maps where the key is a string.'); }
|
|
164
166
|
//@ts-ignore
|
|
165
167
|
let retval = { mongoose_type: Schema.Types.Map, of: schema_entry_from_zod(def.valueType, loop_detector), required: true}
|
|
@@ -189,14 +191,14 @@ function parse_date(def: z.core.$ZodDateDef): any {
|
|
|
189
191
|
return retval;
|
|
190
192
|
}
|
|
191
193
|
|
|
192
|
-
function parse_default(def: z.core.$ZodDefaultDef, loop_detector:
|
|
194
|
+
function parse_default(def: z.core.$ZodDefaultDef, loop_detector: Map<any, validator_group> ): any {
|
|
193
195
|
//@ts-ignore
|
|
194
196
|
let type_definition = schema_entry_from_zod(def.innerType, loop_detector);
|
|
195
197
|
type_definition.default = def.defaultValue;
|
|
196
198
|
return type_definition;
|
|
197
199
|
}
|
|
198
200
|
|
|
199
|
-
function parse_optional(def: z.core.$ZodOptionalDef, loop_detector:
|
|
201
|
+
function parse_optional(def: z.core.$ZodOptionalDef, loop_detector: Map<any, validator_group> ): any {
|
|
200
202
|
//@ts-ignore
|
|
201
203
|
let type_definition = schema_entry_from_zod(def.innerType, loop_detector);
|
|
202
204
|
type_definition.required = false;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { z } from "zod/v4"
|
|
2
2
|
import { $ZodLooseShape } from "zod/v4/core";
|
|
3
3
|
import { z_mongodb_id } from "./mongoose_from_zod.js";
|
|
4
|
+
import { find_loops, validator_group } from './zod_loop_seperator.js'
|
|
4
5
|
|
|
5
6
|
type type_filters = {
|
|
6
7
|
path: string,
|
|
@@ -10,12 +11,15 @@ type type_filters = {
|
|
|
10
11
|
type Mode = 'client' | 'server'
|
|
11
12
|
|
|
12
13
|
export function query_validator_from_zod(zod_definition: z.ZodObject, mode: Mode = 'server'){
|
|
14
|
+
let loops = find_loops(zod_definition as z.ZodType);
|
|
15
|
+
|
|
13
16
|
let retval = {
|
|
14
17
|
limit: z.coerce.number().int().optional(),
|
|
15
18
|
cursor: z_mongodb_id.optional(),
|
|
16
19
|
sort_order: z.enum([/*'asc', 'desc', */'ascending', 'descending']).optional()
|
|
17
20
|
} as $ZodLooseShape;
|
|
18
|
-
|
|
21
|
+
|
|
22
|
+
let object_filters = parse_object(zod_definition._zod.def, '', loops, mode);
|
|
19
23
|
for(let filter of object_filters){
|
|
20
24
|
retval[filter.path.slice(1)] = filter.filter;
|
|
21
25
|
}
|
|
@@ -25,7 +29,7 @@ export function query_validator_from_zod(zod_definition: z.ZodObject, mode: Mode
|
|
|
25
29
|
return z.object(retval).strict();
|
|
26
30
|
}
|
|
27
31
|
|
|
28
|
-
function parse_any(zod_definition: z.ZodTypeAny, prefix: string, loop_detector:
|
|
32
|
+
function parse_any(zod_definition: z.ZodTypeAny, prefix: string, loop_detector: Map<any, validator_group>, mode: Mode = 'server'): type_filters {
|
|
29
33
|
switch (zod_definition._zod.def.type) {
|
|
30
34
|
case "enum":
|
|
31
35
|
return parse_enum(zod_definition._zod.def as z.core.$ZodEnumDef, prefix, mode);
|
|
@@ -63,7 +67,7 @@ function parse_any(zod_definition: z.ZodTypeAny, prefix: string, loop_detector:
|
|
|
63
67
|
}
|
|
64
68
|
}
|
|
65
69
|
|
|
66
|
-
function parse_array(def: z.core.$ZodArrayDef, prefix: string, loop_detector:
|
|
70
|
+
function parse_array(def: z.core.$ZodArrayDef, prefix: string, loop_detector: Map<any, validator_group>, mode: Mode) {
|
|
67
71
|
let simple_children = ['enum', 'string', 'number', 'int', 'boolean']
|
|
68
72
|
if(simple_children.includes(def.element._zod.def.type)) {
|
|
69
73
|
//@ts-ignore
|
|
@@ -84,12 +88,10 @@ function parse_array(def: z.core.$ZodArrayDef, prefix: string, loop_detector: Se
|
|
|
84
88
|
return [];
|
|
85
89
|
}
|
|
86
90
|
|
|
87
|
-
function parse_object(def: z.core.$ZodObjectDef, prefix: string, loop_detector:
|
|
91
|
+
function parse_object(def: z.core.$ZodObjectDef, prefix: string, loop_detector: Map<any, validator_group>, mode: Mode): type_filters {
|
|
88
92
|
if(loop_detector.has(def)) {
|
|
89
93
|
return [];
|
|
90
94
|
}
|
|
91
|
-
loop_detector.add(def);
|
|
92
|
-
|
|
93
95
|
|
|
94
96
|
let retval = [] as type_filters;
|
|
95
97
|
for(let [key, value] of Object.entries(def.shape)){
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { z } from "zod/v4"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export type validator_group = {
|
|
5
|
+
handle: string,
|
|
6
|
+
validator: z.ZodType,
|
|
7
|
+
def: z.core.$ZodType
|
|
8
|
+
appearances: number,
|
|
9
|
+
meta: {[key: string]: any}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function is_validator_group(candidate: any): boolean {
|
|
13
|
+
return typeof candidate.handle === 'string' && candidate.validator;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function find_loops(zod_definition: z.ZodType){
|
|
17
|
+
let potential_loops = discover_loops(zod_definition);
|
|
18
|
+
for(let [key, value] of potential_loops.entries()){
|
|
19
|
+
if(value.appearances <= 1){ potential_loops.delete(key); }
|
|
20
|
+
}
|
|
21
|
+
return potential_loops;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function discover_loops(
|
|
25
|
+
zod_definition: z.ZodType,
|
|
26
|
+
validator_groups: Map<any, validator_group> = new Map()){
|
|
27
|
+
if(!zod_definition) {
|
|
28
|
+
console.error('ISSUE');
|
|
29
|
+
console.error(zod_definition);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
switch (zod_definition._zod.def.type) {
|
|
33
|
+
case "object":
|
|
34
|
+
parse_object(zod_definition as z.ZodObject, validator_groups);
|
|
35
|
+
break;
|
|
36
|
+
case "array" :
|
|
37
|
+
//@ts-expect-error
|
|
38
|
+
discover_loops(zod_definition._zod.def.element, validator_groups);
|
|
39
|
+
break;
|
|
40
|
+
case "nullable":
|
|
41
|
+
case "optional":
|
|
42
|
+
case "default":
|
|
43
|
+
//@ts-expect-error
|
|
44
|
+
discover_loops(zod_definition._zod.def.innerType, validator_groups);
|
|
45
|
+
break;
|
|
46
|
+
case "record":
|
|
47
|
+
parse_record(zod_definition._zod.def as z.core.$ZodRecordDef, validator_groups);
|
|
48
|
+
break;
|
|
49
|
+
case "union":
|
|
50
|
+
parse_union(zod_definition._zod.def as z.core.$ZodUnionDef, validator_groups);
|
|
51
|
+
break;
|
|
52
|
+
default:
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
return validator_groups;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function parse_object(object_validator: z.ZodObject, validator_groups: Map<any, validator_group>) {
|
|
59
|
+
let def = object_validator._zod.def;
|
|
60
|
+
if(validator_groups.has(def)) {
|
|
61
|
+
validator_groups.get(def).appearances++;
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
validator_groups.set(def, {
|
|
65
|
+
appearances: 1,
|
|
66
|
+
handle: ``,
|
|
67
|
+
validator: object_validator,
|
|
68
|
+
//@ts-ignore
|
|
69
|
+
def: def,
|
|
70
|
+
meta: {},
|
|
71
|
+
})
|
|
72
|
+
for(let [key, value] of Object.entries(def.shape)){
|
|
73
|
+
//@ts-ignore
|
|
74
|
+
discover_loops(value, validator_groups);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function parse_record(def: z.core.$ZodRecordDef, validator_groups: Map<any, validator_group>): any {
|
|
79
|
+
//@ts-ignore
|
|
80
|
+
discover_loops(def.keyType, validator_groups);
|
|
81
|
+
|
|
82
|
+
//@ts-ignore
|
|
83
|
+
discover_loops(def.valueType, validator_groups);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function parse_union(def: z.core.$ZodUnionDef, validator_groups: Map<any, validator_group>): any {
|
|
87
|
+
for(let option of def.options){
|
|
88
|
+
//@ts-ignore
|
|
89
|
+
discover_loops(option, validator_groups);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import assert from "assert";
|
|
2
|
+
import { z, ZodBoolean, ZodDate, ZodNumber, ZodString } from 'zod'
|
|
3
|
+
|
|
4
|
+
import { find_loops } from '../dist/utils/zod_loop_seperator.js';
|
|
5
|
+
import { Schema } from 'mongoose'
|
|
6
|
+
import { required } from "zod/mini";
|
|
7
|
+
|
|
8
|
+
process.env.DEBUG = 'express:*'
|
|
9
|
+
|
|
10
|
+
describe('Mongoose from Zod', function () {
|
|
11
|
+
|
|
12
|
+
it('should detect no loops in an empty object', function () {
|
|
13
|
+
let zodSchema = z.object({ })
|
|
14
|
+
let loops = find_loops(zodSchema);
|
|
15
|
+
assert.equal(loops.size, 0)
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should detect a loop if one exists', function () {
|
|
19
|
+
let zodSchema = z.object({
|
|
20
|
+
val: z.string(),
|
|
21
|
+
get looped() {
|
|
22
|
+
return zodSchema
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
let loops = find_loops(zodSchema);
|
|
26
|
+
assert.equal(loops.size, 1)
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should detect multiple loops if they exist', function () {
|
|
30
|
+
|
|
31
|
+
let looped_1 = z.object({
|
|
32
|
+
val: z.string(),
|
|
33
|
+
get looped_1() {
|
|
34
|
+
return looped_1;
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
let looped_2 = z.object({
|
|
39
|
+
val: z.string(),
|
|
40
|
+
get looped_2() {
|
|
41
|
+
return looped_2;
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
let loops = find_loops(z.object({
|
|
45
|
+
looped_1: looped_1,
|
|
46
|
+
looped_2: looped_2,
|
|
47
|
+
}));
|
|
48
|
+
assert.equal(loops.size, 2)
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should detect loops within arrays', function () {
|
|
52
|
+
let zodSchema = z.object({
|
|
53
|
+
val: z.string(),
|
|
54
|
+
get looped() {
|
|
55
|
+
return z.array(zodSchema)
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
let loops = find_loops(zodSchema);
|
|
59
|
+
assert.equal(loops.size, 1)
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should detect loops within nullable', function () {
|
|
63
|
+
let zodSchema = z.object({
|
|
64
|
+
val: z.string(),
|
|
65
|
+
get looped() {
|
|
66
|
+
return z.nullable(zodSchema)
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
let loops = find_loops(zodSchema);
|
|
70
|
+
assert.equal(loops.size, 1)
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should detect loops within optional', function () {
|
|
74
|
+
let zodSchema = z.object({
|
|
75
|
+
val: z.string(),
|
|
76
|
+
get looped() {
|
|
77
|
+
return z.optional(zodSchema)
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
let loops = find_loops(zodSchema);
|
|
81
|
+
assert.equal(loops.size, 1)
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should detect loops within default', function () {
|
|
85
|
+
let zodSchema = z.object({
|
|
86
|
+
val: z.string(),
|
|
87
|
+
get looped() {
|
|
88
|
+
//@ts-ignore
|
|
89
|
+
return zodSchema.default({val: 'test', looped: undefined})
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
let loops = find_loops(zodSchema);
|
|
93
|
+
assert.equal(loops.size, 1)
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should detect loops within record', function () {
|
|
97
|
+
let zodSchema = z.object({
|
|
98
|
+
val: z.string(),
|
|
99
|
+
get looped() {
|
|
100
|
+
return z.record(z.string(), zodSchema)
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
let loops = find_loops(zodSchema);
|
|
104
|
+
assert.equal(loops.size, 1)
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should detect loops within union', function () {
|
|
108
|
+
let zodSchema = z.object({
|
|
109
|
+
val: z.string(),
|
|
110
|
+
get looped() {
|
|
111
|
+
return z.string().or(zodSchema)
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
let loops = find_loops(zodSchema);
|
|
115
|
+
assert.equal(loops.size, 1)
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -221,21 +221,22 @@ describe('Mongoose from Zod', function () {
|
|
|
221
221
|
});
|
|
222
222
|
|
|
223
223
|
it(`should convert recursive schemas`, function () {
|
|
224
|
-
let
|
|
224
|
+
let recursive_child = z.object({
|
|
225
225
|
type: z.enum(['group']),
|
|
226
226
|
operator: z.enum(['all', 'any']),
|
|
227
227
|
get children() {
|
|
228
|
-
return z.array(
|
|
228
|
+
return z.array(recursive_child)
|
|
229
229
|
},
|
|
230
230
|
locked: z.boolean().optional()
|
|
231
231
|
})
|
|
232
|
+
|
|
233
|
+
let zodSchema = z.object({
|
|
234
|
+
children: z.array(recursive_child)
|
|
235
|
+
})
|
|
232
236
|
let mongooseSchema = schema_from_zod(zodSchema)
|
|
233
237
|
|
|
234
|
-
assert.deepEqual({
|
|
235
|
-
|
|
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 },
|
|
238
|
+
assert.deepEqual({
|
|
239
|
+
children: {mongoose_type: [{mongoose_type: Schema.Types.Mixed, required: true}], required: true },
|
|
239
240
|
}, mongooseSchema)
|
|
240
241
|
})
|
|
241
242
|
|
|
@@ -146,19 +146,10 @@ describe('query validator from zod', function () {
|
|
|
146
146
|
|
|
147
147
|
let query_validator = query_validator_from_zod(zod_validator);
|
|
148
148
|
|
|
149
|
-
assert.deepEqual(
|
|
150
|
-
query_validator.parse({
|
|
151
|
-
name: 'fungus',
|
|
152
|
-
}),
|
|
153
|
-
{
|
|
154
|
-
name: 'fungus',
|
|
155
|
-
}
|
|
156
|
-
);
|
|
157
|
-
|
|
158
149
|
assert.throws(() => {
|
|
159
150
|
assert.deepEqual(
|
|
160
151
|
query_validator.parse({
|
|
161
|
-
'
|
|
152
|
+
'name': 'fungus',
|
|
162
153
|
}),
|
|
163
154
|
{}
|
|
164
155
|
);
|
|
@@ -505,4 +505,35 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
505
505
|
}`)
|
|
506
506
|
)
|
|
507
507
|
});
|
|
508
|
+
|
|
509
|
+
it(`should be able to generate a recursive object`, async function () {
|
|
510
|
+
const recursive = z.object({
|
|
511
|
+
name: z.string(),
|
|
512
|
+
get child() {
|
|
513
|
+
return recursive;
|
|
514
|
+
}
|
|
515
|
+
}).meta({id: 'test_recursive_object'})
|
|
516
|
+
|
|
517
|
+
const validate_test_collection = z.object({
|
|
518
|
+
test: recursive
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
let test_collection = new F_Collection('test_collection', validate_test_collection);
|
|
522
|
+
|
|
523
|
+
let proto_registry = new F_Collection_Registry();
|
|
524
|
+
let registry = proto_registry.register(test_collection);
|
|
525
|
+
|
|
526
|
+
await generate_client_library('./test/tmp', registry);
|
|
527
|
+
|
|
528
|
+
assert.equal(
|
|
529
|
+
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
530
|
+
remove_whitespace(`export type test_collection = {
|
|
531
|
+
"test": type_test_recursive_object
|
|
532
|
+
}
|
|
533
|
+
type type_test_recursive_object = {
|
|
534
|
+
"name": string
|
|
535
|
+
"child": type_test_recursive_object
|
|
536
|
+
}`)
|
|
537
|
+
)
|
|
538
|
+
});
|
|
508
539
|
});
|
|
File without changes
|
|
File without changes
|