@liminalfunctions/framework 1.0.44 → 1.0.47
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/README.md +315 -4
- package/dist/F_Collection.js +6 -0
- package/dist/F_Collection.js.map +1 -1
- package/dist/F_Collection_Registry.js +1 -1
- package/dist/F_Collection_Registry.js.map +1 -1
- package/dist/F_Compile.d.ts +2 -1
- package/dist/F_Compile.js +18 -1
- package/dist/F_Compile.js.map +1 -1
- package/dist/utils/mongoose_from_zod.js +19 -0
- package/dist/utils/mongoose_from_zod.js.map +1 -1
- package/dist/utils/query_object_to_mongodb_query.d.ts +13 -0
- package/dist/utils/query_object_to_mongodb_query.js +2 -2
- package/dist/utils/query_object_to_mongodb_query.js.map +1 -1
- package/package.json +1 -1
- package/src/F_Collection.ts +7 -0
- package/src/F_Collection_Registry.ts +1 -1
- package/src/F_Compile.ts +32 -1
- package/src/utils/mongoose_from_zod.ts +22 -3
- package/src/utils/query_object_to_mongodb_query.ts +2 -2
- package/test/0_1_mongoose_from_zod.test.ts +37 -0
- package/test/2_0_client_library_basic_type_generation.test.ts +48 -2
- package/test/2_0_client_library_query_type_generation.test.ts +63 -38
|
@@ -23,7 +23,7 @@ export class F_Collection_Registry<Collections = {}> {
|
|
|
23
23
|
|
|
24
24
|
compile(app: Router, api_prefix: string) {
|
|
25
25
|
for(let collection of Object.values(this.collections)){
|
|
26
|
-
compile(app, collection, api_prefix)
|
|
26
|
+
compile(app, collection, api_prefix, this)
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
}
|
package/src/F_Compile.ts
CHANGED
|
@@ -6,20 +6,51 @@ import { F_Collection } from "./F_Collection.js";
|
|
|
6
6
|
import { F_Security_Model, Authenticated_Request } from "./F_Security_Models/F_Security_Model.js";
|
|
7
7
|
import { convert_null, query_object_to_mongodb_limits, query_object_to_mongodb_query } from "./utils/query_object_to_mongodb_query.js";
|
|
8
8
|
import { z_mongodb_id } from "./utils/mongoose_from_zod.js";
|
|
9
|
+
import { F_Collection_Registry } from "./F_Collection_Registry.js";
|
|
9
10
|
|
|
10
11
|
/*process.on('unhandledRejection', (reason, promise) => {
|
|
11
12
|
console.log(`CAUGHT UNHANDLED REJECTION`)
|
|
12
13
|
console.log(reason)
|
|
13
14
|
})*/
|
|
14
15
|
|
|
15
|
-
export function compile<Collection_ID extends string, ZodSchema extends z.ZodObject>(
|
|
16
|
+
export function compile<Collection_ID extends string, ZodSchema extends z.ZodObject>(
|
|
17
|
+
app: Router,
|
|
18
|
+
collection: F_Collection<Collection_ID, ZodSchema>,
|
|
19
|
+
api_prefix: string,
|
|
20
|
+
collection_registry: F_Collection_Registry<any>){
|
|
16
21
|
/*app.use((req, res, next) => {
|
|
17
22
|
console.log(`${req.method} ${req.originalUrl}`)
|
|
18
23
|
next();
|
|
19
24
|
})*/
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
for(let access_layers of collection.access_layers){
|
|
28
|
+
for(let layer of access_layers.layers){
|
|
29
|
+
// verify that the collection is not in its own layers
|
|
30
|
+
if(layer === collection.collection_id){
|
|
31
|
+
throw new Error(`Error compiling collection ${collection.collection_id}: a collection cannot be a member of it's own layer. Remove "${collection.collection_id}" from the collection's layers.`)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// verify that each layer has a corresponding collection
|
|
35
|
+
if(!collection_registry.collections[layer]){
|
|
36
|
+
throw new Error(`Error compiling collection ${collection.collection_id}: collection registry does not have a collection with the ID "${layer}". Each layer must be a valid collection ID.`)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if(!Object.hasOwn(collection.validator._zod.def.shape, `${layer}_id`)) {
|
|
40
|
+
throw new Error(`Error compiling collection ${collection.collection_id}: collection does not have a field "${layer}_id. Either remove ${layer} from the collection's layers, or add a field ${layer}_id`)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let layer_id_is_mongodb_id = collection.validator._zod.def.shape[`${layer}_id`].meta()?.framework_override_type === 'mongodb_id';
|
|
44
|
+
if(!layer_id_is_mongodb_id){
|
|
45
|
+
throw new Error(`Error compiling collection ${collection.collection_id}: ${layer}_id must be a mongodb ID. use the z_mongodb_id, z_mongodb_id_nullable, or z_mongodb_id_optional special fields.`)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
20
49
|
|
|
50
|
+
// set up the Express endpoints
|
|
21
51
|
for(let access_layers of collection.access_layers){
|
|
22
52
|
let base_layers_path_components = access_layers.layers.flatMap(ele => [ele, ':' + ele]);
|
|
53
|
+
|
|
23
54
|
|
|
24
55
|
let get_one_path = [
|
|
25
56
|
api_prefix,
|
|
@@ -2,14 +2,16 @@ import { z } from "zod/v4"
|
|
|
2
2
|
import mongoose, { Schema } from "mongoose";
|
|
3
3
|
|
|
4
4
|
import { find_loops, validator_group } from './zod_loop_seperator.js'
|
|
5
|
-
import {
|
|
5
|
+
import { complex_query_map, query_meta_map } from "./query_object_to_mongodb_query.js";
|
|
6
6
|
|
|
7
|
-
//export const z_mongodb_id = z.string().length(24).describe('F_Mongodb_ID');
|
|
8
|
-
//export const mongodb_id = () => z_mongodb_id;
|
|
9
7
|
const underlying_mongodb_id_validator = z.string().length(24);
|
|
10
8
|
const underlying_mongodb_id_validator_optional = underlying_mongodb_id_validator.optional();
|
|
11
9
|
const underlying_mongodb_id_validator_nullable = underlying_mongodb_id_validator.nullable();
|
|
12
10
|
|
|
11
|
+
const forbidden_prefixes = ['$'];
|
|
12
|
+
const forbidden_keys = [...Object.keys(query_meta_map)]
|
|
13
|
+
const forbidden_suffixes = [...Object.keys(complex_query_map)]
|
|
14
|
+
|
|
13
15
|
export const z_mongodb_id = z.custom<string>((val) => {
|
|
14
16
|
if(!val){ return false; }
|
|
15
17
|
let parsed = underlying_mongodb_id_validator.safeParse(val);
|
|
@@ -149,6 +151,23 @@ function parse_object(def: z.core.$ZodObjectDef, loop_detector: Map<any, validat
|
|
|
149
151
|
|
|
150
152
|
let retval = {} as any;
|
|
151
153
|
for(let [key, value] of Object.entries(def.shape)){
|
|
154
|
+
for(let forbidden_key of forbidden_keys){
|
|
155
|
+
if(key.toLowerCase() === forbidden_key){
|
|
156
|
+
throw new Error(`"${forbidden_key}" is a forbidden field name because it's used in query parameters. Please check your validators and replace this key with something else.`)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
for(let forbidden_prefix of forbidden_prefixes){
|
|
161
|
+
if(key.toLowerCase().startsWith(forbidden_prefix)){
|
|
162
|
+
throw new Error(`"${key}" is not a valid field name because it begins with "${forbidden_prefix}". Please check your validators and replace this key with something else.`)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
for(let forbidden_suffix of forbidden_suffixes){
|
|
167
|
+
if(key.toLowerCase().endsWith(forbidden_suffix)){
|
|
168
|
+
throw new Error(`"${key}" is not a valid field name because it ends with "${forbidden_suffix}". Please check your validators and replace this key with something else.`)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
152
171
|
//@ts-ignore
|
|
153
172
|
retval[key] = schema_entry_from_zod(value, loop_detector);
|
|
154
173
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { QueryWithHelpers } from "mongoose";
|
|
2
2
|
|
|
3
|
-
let complex_query_map = {
|
|
3
|
+
export let complex_query_map = {
|
|
4
4
|
'_gt': '$gt',
|
|
5
5
|
'_lt': '$lt',
|
|
6
6
|
'_gte': '$gte',
|
|
@@ -15,7 +15,7 @@ let complex_query_map = {
|
|
|
15
15
|
'_in': '$in'
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
let query_meta_map = {
|
|
18
|
+
export let query_meta_map = {
|
|
19
19
|
'limit': true,
|
|
20
20
|
'cursor': true,
|
|
21
21
|
'sort': true,
|
|
@@ -277,4 +277,41 @@ describe('Mongoose from Zod', function () {
|
|
|
277
277
|
c: { mongoose_type: { name: { mongoose_type: String, required: true } }, required: true },
|
|
278
278
|
}, mongooseSchema)
|
|
279
279
|
})
|
|
280
|
+
|
|
281
|
+
/*
|
|
282
|
+
forbidden keys
|
|
283
|
+
*/
|
|
284
|
+
it(`should error if a key starts with $`, function () {
|
|
285
|
+
let zodSchema = z.object({
|
|
286
|
+
$where: z.string()
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
assert.throws(() => {
|
|
290
|
+
let mongooseSchema = schema_from_zod(zodSchema);
|
|
291
|
+
})
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
for(let key of ['limit', 'cursor', 'sort', 'sort_order']){
|
|
295
|
+
it(`should error if a key is ${key}`, function () {
|
|
296
|
+
let zodSchema = z.object({
|
|
297
|
+
[key]: z.string()
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
assert.throws(() => {
|
|
301
|
+
let mongooseSchema = schema_from_zod(zodSchema);
|
|
302
|
+
})
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
for(let key of ['_gt', '_lt', '_gte', '_lte', '_in']){
|
|
307
|
+
it(`should error if a key ends with ${key}`, function () {
|
|
308
|
+
let zodSchema = z.object({
|
|
309
|
+
[`id${key}`]: z.string()
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
assert.throws(() => {
|
|
313
|
+
let mongooseSchema = schema_from_zod(zodSchema);
|
|
314
|
+
})
|
|
315
|
+
});
|
|
316
|
+
}
|
|
280
317
|
});
|
|
@@ -38,7 +38,9 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
it(`should be able to generate a plain object`, async function () {
|
|
41
|
-
const validate_test_collection = z.object({
|
|
41
|
+
const validate_test_collection = z.object({
|
|
42
|
+
_id: z_mongodb_id
|
|
43
|
+
});
|
|
42
44
|
|
|
43
45
|
let test_collection = new F_Collection('test_collection', 'test_collection', validate_test_collection);
|
|
44
46
|
|
|
@@ -49,12 +51,13 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
49
51
|
|
|
50
52
|
assert.equal(
|
|
51
53
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
52
|
-
remove_whitespace(`export type test_collection = {}`)
|
|
54
|
+
remove_whitespace(`export type test_collection = {"_id": string}`)
|
|
53
55
|
)
|
|
54
56
|
});
|
|
55
57
|
|
|
56
58
|
it(`should be able to generate a plain object containing a string`, async function () {
|
|
57
59
|
const validate_test_collection = z.object({
|
|
60
|
+
_id: z_mongodb_id,
|
|
58
61
|
test: z.string(),
|
|
59
62
|
});
|
|
60
63
|
|
|
@@ -68,6 +71,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
68
71
|
assert.equal(
|
|
69
72
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
70
73
|
remove_whitespace(`export type test_collection = {
|
|
74
|
+
"_id": string
|
|
71
75
|
"test": string
|
|
72
76
|
}`)
|
|
73
77
|
)
|
|
@@ -75,6 +79,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
75
79
|
|
|
76
80
|
it(`should be able to generate a plain object containing a number`, async function () {
|
|
77
81
|
const validate_test_collection = z.object({
|
|
82
|
+
_id: z_mongodb_id,
|
|
78
83
|
test: z.number(),
|
|
79
84
|
});
|
|
80
85
|
|
|
@@ -88,6 +93,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
88
93
|
assert.equal(
|
|
89
94
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
90
95
|
remove_whitespace(`export type test_collection = {
|
|
96
|
+
"_id": string
|
|
91
97
|
"test": number
|
|
92
98
|
}`)
|
|
93
99
|
)
|
|
@@ -95,6 +101,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
95
101
|
|
|
96
102
|
it(`should be able to generate a plain object containing a boolean`, async function () {
|
|
97
103
|
const validate_test_collection = z.object({
|
|
104
|
+
_id: z_mongodb_id,
|
|
98
105
|
test: z.boolean(),
|
|
99
106
|
});
|
|
100
107
|
|
|
@@ -108,6 +115,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
108
115
|
assert.equal(
|
|
109
116
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
110
117
|
remove_whitespace(`export type test_collection = {
|
|
118
|
+
"_id": string
|
|
111
119
|
"test": boolean
|
|
112
120
|
}`)
|
|
113
121
|
)
|
|
@@ -115,6 +123,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
115
123
|
|
|
116
124
|
it(`should be able to generate a plain object containing a date`, async function () {
|
|
117
125
|
const validate_test_collection = z.object({
|
|
126
|
+
_id: z_mongodb_id,
|
|
118
127
|
test: z.date(),
|
|
119
128
|
});
|
|
120
129
|
|
|
@@ -128,6 +137,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
128
137
|
assert.equal(
|
|
129
138
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
130
139
|
remove_whitespace(`export type test_collection = {
|
|
140
|
+
"_id": string
|
|
131
141
|
"test": Date
|
|
132
142
|
}`)
|
|
133
143
|
)
|
|
@@ -135,6 +145,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
135
145
|
|
|
136
146
|
it(`should be able to generate a plain object containing an objectID`, async function () {
|
|
137
147
|
const validate_test_collection = z.object({
|
|
148
|
+
_id: z_mongodb_id,
|
|
138
149
|
test: z_mongodb_id,
|
|
139
150
|
});
|
|
140
151
|
|
|
@@ -148,6 +159,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
148
159
|
assert.equal(
|
|
149
160
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
150
161
|
remove_whitespace(`export type test_collection = {
|
|
162
|
+
"_id": string
|
|
151
163
|
"test": string
|
|
152
164
|
}`)
|
|
153
165
|
)
|
|
@@ -155,6 +167,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
155
167
|
|
|
156
168
|
it(`should be able to generate a plain object containing a nullable string`, async function () {
|
|
157
169
|
const validate_test_collection = z.object({
|
|
170
|
+
_id: z_mongodb_id,
|
|
158
171
|
test: z.nullable(z.string()),
|
|
159
172
|
});
|
|
160
173
|
|
|
@@ -168,6 +181,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
168
181
|
assert.equal(
|
|
169
182
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
170
183
|
remove_whitespace(`export type test_collection = {
|
|
184
|
+
"_id": string
|
|
171
185
|
"test": string | null
|
|
172
186
|
}`)
|
|
173
187
|
)
|
|
@@ -175,6 +189,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
175
189
|
|
|
176
190
|
it(`should be able to generate a plain object containing union types`, async function () {
|
|
177
191
|
const validate_test_collection = z.object({
|
|
192
|
+
_id: z_mongodb_id,
|
|
178
193
|
test: z.string().or(z.number()),
|
|
179
194
|
});
|
|
180
195
|
|
|
@@ -188,6 +203,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
188
203
|
assert.equal(
|
|
189
204
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
190
205
|
remove_whitespace(`export type test_collection = {
|
|
206
|
+
"_id": string
|
|
191
207
|
"test": string | number
|
|
192
208
|
}`)
|
|
193
209
|
)
|
|
@@ -195,6 +211,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
195
211
|
|
|
196
212
|
it(`should be able to generate a plain object containing union types wrapped in nullable`, async function () {
|
|
197
213
|
const validate_test_collection = z.object({
|
|
214
|
+
_id: z_mongodb_id,
|
|
198
215
|
test: z.nullable(z.string().or(z.number())),
|
|
199
216
|
});
|
|
200
217
|
|
|
@@ -208,6 +225,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
208
225
|
assert.equal(
|
|
209
226
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
210
227
|
remove_whitespace(`export type test_collection = {
|
|
228
|
+
"_id": string
|
|
211
229
|
"test": string | number | null
|
|
212
230
|
}`)
|
|
213
231
|
)
|
|
@@ -215,6 +233,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
215
233
|
|
|
216
234
|
it(`should be able to generate a plain object containing union of object types`, async function () {
|
|
217
235
|
const validate_test_collection = z.object({
|
|
236
|
+
_id: z_mongodb_id,
|
|
218
237
|
test: z.object({
|
|
219
238
|
sub: z.string()
|
|
220
239
|
}).or(z.object({
|
|
@@ -232,6 +251,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
232
251
|
assert.equal(
|
|
233
252
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
234
253
|
remove_whitespace(`export type test_collection = {
|
|
254
|
+
"_id": string
|
|
235
255
|
"test": {"sub": string} | {"sub2": number}
|
|
236
256
|
}`)
|
|
237
257
|
)
|
|
@@ -239,6 +259,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
239
259
|
|
|
240
260
|
it(`should be able to generate an enum`, async function () {
|
|
241
261
|
const validate_test_collection = z.object({
|
|
262
|
+
_id: z_mongodb_id,
|
|
242
263
|
test: z.enum(["red", "green", "blue"]),
|
|
243
264
|
});
|
|
244
265
|
|
|
@@ -252,6 +273,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
252
273
|
assert.equal(
|
|
253
274
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
254
275
|
remove_whitespace(`export type test_collection = {
|
|
276
|
+
"_id": string
|
|
255
277
|
"test": ("red" | "green" | "blue")
|
|
256
278
|
}`)
|
|
257
279
|
)
|
|
@@ -259,6 +281,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
259
281
|
|
|
260
282
|
it(`should be able to generate an array of enum`, async function () {
|
|
261
283
|
const validate_test_collection = z.object({
|
|
284
|
+
_id: z_mongodb_id,
|
|
262
285
|
test: z.array(z.enum(["red", "green", "blue"])),
|
|
263
286
|
});
|
|
264
287
|
|
|
@@ -272,6 +295,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
272
295
|
assert.equal(
|
|
273
296
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
274
297
|
remove_whitespace(`export type test_collection = {
|
|
298
|
+
"_id": string
|
|
275
299
|
"test": ("red" | "green" | "blue")[]
|
|
276
300
|
}`)
|
|
277
301
|
)
|
|
@@ -279,6 +303,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
279
303
|
|
|
280
304
|
it(`should be able to generate a plain nested object`, async function () {
|
|
281
305
|
const validate_test_collection = z.object({
|
|
306
|
+
_id: z_mongodb_id,
|
|
282
307
|
test: z.object({
|
|
283
308
|
}),
|
|
284
309
|
});
|
|
@@ -293,6 +318,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
293
318
|
assert.equal(
|
|
294
319
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
295
320
|
remove_whitespace(`export type test_collection = {
|
|
321
|
+
"_id": string
|
|
296
322
|
"test": {}
|
|
297
323
|
}`)
|
|
298
324
|
)
|
|
@@ -300,6 +326,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
300
326
|
|
|
301
327
|
it(`should be able to generate a plain nested with basic fields`, async function () {
|
|
302
328
|
const validate_test_collection = z.object({
|
|
329
|
+
_id: z_mongodb_id,
|
|
303
330
|
test: z.object({
|
|
304
331
|
field_string: z.string(),
|
|
305
332
|
field_number: z.number(),
|
|
@@ -321,6 +348,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
321
348
|
assert.equal(
|
|
322
349
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
323
350
|
remove_whitespace(`export type test_collection = {
|
|
351
|
+
"_id": string
|
|
324
352
|
"test": {
|
|
325
353
|
"field_string": string
|
|
326
354
|
"field_number": number
|
|
@@ -336,6 +364,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
336
364
|
|
|
337
365
|
it(`should be able to generate arrays of primitive fields`, async function () {
|
|
338
366
|
const validate_test_collection = z.object({
|
|
367
|
+
_id: z_mongodb_id,
|
|
339
368
|
field_string: z.array(z.string()),
|
|
340
369
|
field_number: z.array(z.number()),
|
|
341
370
|
field_boolean: z.array(z.boolean()),
|
|
@@ -352,6 +381,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
352
381
|
assert.equal(
|
|
353
382
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
354
383
|
remove_whitespace(`export type test_collection = {
|
|
384
|
+
"_id": string
|
|
355
385
|
"field_string": string[]
|
|
356
386
|
"field_number": number[]
|
|
357
387
|
"field_boolean": boolean[]
|
|
@@ -362,6 +392,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
362
392
|
|
|
363
393
|
it(`should be able to generate arrays of objects containing primitive fields`, async function () {
|
|
364
394
|
const validate_test_collection = z.object({
|
|
395
|
+
_id: z_mongodb_id,
|
|
365
396
|
field_array: z.array(z.object({
|
|
366
397
|
field_string: z.array(z.string()),
|
|
367
398
|
field_number: z.array(z.number()),
|
|
@@ -381,6 +412,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
381
412
|
assert.equal(
|
|
382
413
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
383
414
|
remove_whitespace(`export type test_collection = {
|
|
415
|
+
"_id": string
|
|
384
416
|
"field_array": {
|
|
385
417
|
"field_string": string[]
|
|
386
418
|
"field_number": number[]
|
|
@@ -393,6 +425,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
393
425
|
|
|
394
426
|
it(`should be able to generate nested arrays`, async function () {
|
|
395
427
|
const validate_test_collection = z.object({
|
|
428
|
+
_id: z_mongodb_id,
|
|
396
429
|
field_array: z.array(
|
|
397
430
|
z.array(z.object({
|
|
398
431
|
field_string: z.array(z.string()),
|
|
@@ -413,6 +446,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
413
446
|
assert.equal(
|
|
414
447
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
415
448
|
remove_whitespace(`export type test_collection = {
|
|
449
|
+
"_id": string
|
|
416
450
|
"field_array": {
|
|
417
451
|
"field_string": string[]
|
|
418
452
|
"field_number": number[]
|
|
@@ -425,6 +459,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
425
459
|
|
|
426
460
|
it(`should be able to generate a plain object containing a primitive with a default`, async function () {
|
|
427
461
|
const validate_test_collection = z.object({
|
|
462
|
+
_id: z_mongodb_id,
|
|
428
463
|
test: z.string().default('ezikiel snograss'),
|
|
429
464
|
});
|
|
430
465
|
|
|
@@ -438,6 +473,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
438
473
|
assert.equal(
|
|
439
474
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
440
475
|
remove_whitespace(`export type test_collection = {
|
|
476
|
+
"_id": string
|
|
441
477
|
"test"?: string
|
|
442
478
|
}`)
|
|
443
479
|
)
|
|
@@ -445,6 +481,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
445
481
|
|
|
446
482
|
it(`should be able to generate a plain object containing an optional primitive`, async function () {
|
|
447
483
|
const validate_test_collection = z.object({
|
|
484
|
+
_id: z_mongodb_id,
|
|
448
485
|
test: z.string().optional(),
|
|
449
486
|
});
|
|
450
487
|
|
|
@@ -458,6 +495,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
458
495
|
assert.equal(
|
|
459
496
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
460
497
|
remove_whitespace(`export type test_collection = {
|
|
498
|
+
"_id": string
|
|
461
499
|
"test"?: string
|
|
462
500
|
}`)
|
|
463
501
|
)
|
|
@@ -465,6 +503,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
465
503
|
|
|
466
504
|
it(`should be able to generate a plain object containing a record`, async function () {
|
|
467
505
|
const validate_test_collection = z.object({
|
|
506
|
+
_id: z_mongodb_id,
|
|
468
507
|
test: z.record(z.string(), z.string())
|
|
469
508
|
});
|
|
470
509
|
|
|
@@ -478,6 +517,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
478
517
|
assert.equal(
|
|
479
518
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
480
519
|
remove_whitespace(`export type test_collection = {
|
|
520
|
+
"_id": string
|
|
481
521
|
"test": {[key: string]: string}
|
|
482
522
|
}`)
|
|
483
523
|
)
|
|
@@ -486,6 +526,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
486
526
|
|
|
487
527
|
it(`should be able to generate a plain object containing an object record`, async function () {
|
|
488
528
|
const validate_test_collection = z.object({
|
|
529
|
+
_id: z_mongodb_id,
|
|
489
530
|
test: z.record(z.string(), z.object({
|
|
490
531
|
test_2: z.string()
|
|
491
532
|
}))
|
|
@@ -501,6 +542,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
501
542
|
assert.equal(
|
|
502
543
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
503
544
|
remove_whitespace(`export type test_collection = {
|
|
545
|
+
"_id": string
|
|
504
546
|
"test": {[key: string]: { "test_2": string }}
|
|
505
547
|
}`)
|
|
506
548
|
)
|
|
@@ -508,6 +550,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
508
550
|
|
|
509
551
|
it(`should be able to generate a plain object containing an array record`, async function () {
|
|
510
552
|
const validate_test_collection = z.object({
|
|
553
|
+
_id: z_mongodb_id,
|
|
511
554
|
test: z.record(z.string(), z.array(z.string()))
|
|
512
555
|
});
|
|
513
556
|
|
|
@@ -521,6 +564,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
521
564
|
assert.equal(
|
|
522
565
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
523
566
|
remove_whitespace(`export type test_collection = {
|
|
567
|
+
"_id": string
|
|
524
568
|
"test": {[key: string]: string[]}
|
|
525
569
|
}`)
|
|
526
570
|
)
|
|
@@ -535,6 +579,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
535
579
|
}).meta({id: 'test_recursive_object'})
|
|
536
580
|
|
|
537
581
|
const validate_test_collection = z.object({
|
|
582
|
+
_id: z_mongodb_id,
|
|
538
583
|
test: recursive
|
|
539
584
|
});
|
|
540
585
|
|
|
@@ -548,6 +593,7 @@ describe('Client Library Generation: Basic Types', function () {
|
|
|
548
593
|
assert.equal(
|
|
549
594
|
remove_whitespace(await readFile('./test/tmp/src/types/test_collection.ts', { encoding: 'utf-8' })),
|
|
550
595
|
remove_whitespace(`export type test_collection = {
|
|
596
|
+
"_id": string
|
|
551
597
|
"test": type_test_recursive_object
|
|
552
598
|
}
|
|
553
599
|
type type_test_recursive_object = {
|