@tsed/mongoose 8.11.0 → 8.11.2-rc.1
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/lib/esm/MongooseModule.js +0 -1
- package/lib/esm/constants/constants.js +0 -1
- package/lib/esm/decorators/auto.js +0 -1
- package/lib/esm/decorators/discriminatorKey.js +0 -1
- package/lib/esm/decorators/dynamicRef.js +0 -1
- package/lib/esm/decorators/excludeIndexes.js +0 -1
- package/lib/esm/decorators/expires.js +0 -1
- package/lib/esm/decorators/immutable.js +0 -1
- package/lib/esm/decorators/indexed.js +0 -1
- package/lib/esm/decorators/lowercase.js +0 -1
- package/lib/esm/decorators/model.js +0 -1
- package/lib/esm/decorators/mongooseIndex.js +0 -1
- package/lib/esm/decorators/mongooseIndexes.js +0 -1
- package/lib/esm/decorators/mongoosePlugin.js +0 -1
- package/lib/esm/decorators/numberDecimal.js +0 -1
- package/lib/esm/decorators/objectID.js +0 -1
- package/lib/esm/decorators/postHook.js +0 -1
- package/lib/esm/decorators/preHook.js +0 -1
- package/lib/esm/decorators/ref.js +0 -1
- package/lib/esm/decorators/schema.js +0 -1
- package/lib/esm/decorators/schemaIgnore.js +0 -1
- package/lib/esm/decorators/select.js +0 -1
- package/lib/esm/decorators/sparse.js +0 -1
- package/lib/esm/decorators/text.js +0 -1
- package/lib/esm/decorators/trim.js +0 -1
- package/lib/esm/decorators/unique.js +0 -1
- package/lib/esm/decorators/uppercase.js +0 -1
- package/lib/esm/decorators/versionKey.js +0 -1
- package/lib/esm/decorators/virtualRef.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/interfaces/MongooseConnectionOptions.js +0 -1
- package/lib/esm/interfaces/MongooseDocument.js +0 -1
- package/lib/esm/interfaces/MongooseModel.js +0 -1
- package/lib/esm/interfaces/MongooseModelOptions.js +0 -1
- package/lib/esm/interfaces/MongooseSchemaOptions.js +0 -1
- package/lib/esm/interfaces/MongooseSchemaTypes.js +0 -1
- package/lib/esm/interfaces/MongooseVirtualRefOptions.js +0 -1
- package/lib/esm/interfaces/interfaces.js +0 -1
- package/lib/esm/registries/MongooseModels.js +0 -1
- package/lib/esm/services/MongooseConnections.js +0 -1
- package/lib/esm/services/MongooseService.js +0 -1
- package/lib/esm/utils/createModel.js +0 -1
- package/lib/esm/utils/createSchema.js +0 -1
- package/lib/esm/utils/resolveRefType.js +0 -1
- package/lib/esm/utils/schemaOptions.js +0 -1
- package/package.json +13 -10
- package/lib/esm/MongooseModule.js.map +0 -1
- package/lib/esm/constants/constants.js.map +0 -1
- package/lib/esm/decorators/auto.js.map +0 -1
- package/lib/esm/decorators/discriminatorKey.js.map +0 -1
- package/lib/esm/decorators/dynamicRef.js.map +0 -1
- package/lib/esm/decorators/excludeIndexes.js.map +0 -1
- package/lib/esm/decorators/expires.js.map +0 -1
- package/lib/esm/decorators/immutable.js.map +0 -1
- package/lib/esm/decorators/indexed.js.map +0 -1
- package/lib/esm/decorators/lowercase.js.map +0 -1
- package/lib/esm/decorators/model.js.map +0 -1
- package/lib/esm/decorators/mongooseIndex.js.map +0 -1
- package/lib/esm/decorators/mongooseIndexes.js.map +0 -1
- package/lib/esm/decorators/mongoosePlugin.js.map +0 -1
- package/lib/esm/decorators/numberDecimal.js.map +0 -1
- package/lib/esm/decorators/objectID.js.map +0 -1
- package/lib/esm/decorators/postHook.js.map +0 -1
- package/lib/esm/decorators/preHook.js.map +0 -1
- package/lib/esm/decorators/ref.js.map +0 -1
- package/lib/esm/decorators/schema.js.map +0 -1
- package/lib/esm/decorators/schemaIgnore.js.map +0 -1
- package/lib/esm/decorators/select.js.map +0 -1
- package/lib/esm/decorators/sparse.js.map +0 -1
- package/lib/esm/decorators/text.js.map +0 -1
- package/lib/esm/decorators/trim.js.map +0 -1
- package/lib/esm/decorators/unique.js.map +0 -1
- package/lib/esm/decorators/uppercase.js.map +0 -1
- package/lib/esm/decorators/versionKey.js.map +0 -1
- package/lib/esm/decorators/virtualRef.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/interfaces/MongooseConnectionOptions.js.map +0 -1
- package/lib/esm/interfaces/MongooseDocument.js.map +0 -1
- package/lib/esm/interfaces/MongooseModel.js.map +0 -1
- package/lib/esm/interfaces/MongooseModelOptions.js.map +0 -1
- package/lib/esm/interfaces/MongooseSchemaOptions.js.map +0 -1
- package/lib/esm/interfaces/MongooseSchemaTypes.js.map +0 -1
- package/lib/esm/interfaces/MongooseVirtualRefOptions.js.map +0 -1
- package/lib/esm/interfaces/interfaces.js.map +0 -1
- package/lib/esm/registries/MongooseModels.js.map +0 -1
- package/lib/esm/services/MongooseConnections.js.map +0 -1
- package/lib/esm/services/MongooseService.js.map +0 -1
- package/lib/esm/utils/createModel.js.map +0 -1
- package/lib/esm/utils/createSchema.js.map +0 -1
- package/lib/esm/utils/resolveRefType.js.map +0 -1
- package/lib/esm/utils/schemaOptions.js.map +0 -1
- package/src/MongooseModule.ts +0 -32
- package/src/constants/constants.ts +0 -20
- package/src/decorators/auto.spec.ts +0 -30
- package/src/decorators/auto.ts +0 -24
- package/src/decorators/discriminatorKey.ts +0 -1
- package/src/decorators/dynamicRef.spec.ts +0 -46
- package/src/decorators/dynamicRef.ts +0 -76
- package/src/decorators/excludeIndexes.spec.ts +0 -30
- package/src/decorators/excludeIndexes.ts +0 -24
- package/src/decorators/expires.spec.ts +0 -18
- package/src/decorators/expires.ts +0 -24
- package/src/decorators/immutable.spec.ts +0 -30
- package/src/decorators/immutable.ts +0 -26
- package/src/decorators/indexed.spec.ts +0 -19
- package/src/decorators/indexed.ts +0 -24
- package/src/decorators/lowercase.spec.ts +0 -30
- package/src/decorators/lowercase.ts +0 -24
- package/src/decorators/model.ts +0 -69
- package/src/decorators/mongooseIndex.spec.ts +0 -26
- package/src/decorators/mongooseIndex.ts +0 -35
- package/src/decorators/mongooseIndexes.spec.ts +0 -35
- package/src/decorators/mongooseIndexes.ts +0 -35
- package/src/decorators/mongoosePlugin.spec.ts +0 -25
- package/src/decorators/mongoosePlugin.ts +0 -20
- package/src/decorators/numberDecimal.spec.ts +0 -266
- package/src/decorators/numberDecimal.ts +0 -84
- package/src/decorators/objectID.spec.ts +0 -24
- package/src/decorators/objectID.ts +0 -39
- package/src/decorators/postHook.spec.ts +0 -44
- package/src/decorators/postHook.ts +0 -72
- package/src/decorators/preHook.spec.ts +0 -75
- package/src/decorators/preHook.ts +0 -79
- package/src/decorators/ref.spec.ts +0 -361
- package/src/decorators/ref.ts +0 -111
- package/src/decorators/schema.ts +0 -79
- package/src/decorators/schemaIgnore.spec.ts +0 -18
- package/src/decorators/schemaIgnore.ts +0 -24
- package/src/decorators/select.spec.ts +0 -18
- package/src/decorators/select.ts +0 -24
- package/src/decorators/sparse.spec.ts +0 -30
- package/src/decorators/sparse.ts +0 -26
- package/src/decorators/text.spec.ts +0 -30
- package/src/decorators/text.ts +0 -25
- package/src/decorators/trim.spec.ts +0 -18
- package/src/decorators/trim.ts +0 -23
- package/src/decorators/unique.spec.ts +0 -18
- package/src/decorators/unique.ts +0 -23
- package/src/decorators/uppercase.spec.ts +0 -30
- package/src/decorators/uppercase.ts +0 -24
- package/src/decorators/versionKey.spec.ts +0 -19
- package/src/decorators/versionKey.ts +0 -9
- package/src/decorators/virtualRef.spec.ts +0 -383
- package/src/decorators/virtualRef.ts +0 -73
- package/src/index.ts +0 -47
- package/src/interfaces/MongooseConnectionOptions.ts +0 -10
- package/src/interfaces/MongooseDocument.ts +0 -3
- package/src/interfaces/MongooseModel.ts +0 -10
- package/src/interfaces/MongooseModelOptions.ts +0 -8
- package/src/interfaces/MongooseSchemaOptions.ts +0 -61
- package/src/interfaces/MongooseSchemaTypes.ts +0 -5
- package/src/interfaces/MongooseVirtualRefOptions.ts +0 -10
- package/src/interfaces/interfaces.ts +0 -9
- package/src/registries/MongooseModels.ts +0 -3
- package/src/services/MongooseConnection.spec.ts +0 -70
- package/src/services/MongooseConnections.ts +0 -58
- package/src/services/MongooseService.spec.ts +0 -73
- package/src/services/MongooseService.ts +0 -77
- package/src/utils/buildMongooseSchema.spec.ts +0 -67
- package/src/utils/createModel.spec.ts +0 -49
- package/src/utils/createModel.ts +0 -54
- package/src/utils/createSchema.spec.ts +0 -817
- package/src/utils/createSchema.ts +0 -207
- package/src/utils/resolveRefType.spec.ts +0 -30
- package/src/utils/resolveRefType.ts +0 -18
- package/src/utils/schemaOptions.spec.ts +0 -68
- package/src/utils/schemaOptions.ts +0 -74
- package/vitest.config.mts +0 -25
|
@@ -1,817 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CollectionOf,
|
|
3
|
-
Default,
|
|
4
|
-
Enum,
|
|
5
|
-
getJsonSchema,
|
|
6
|
-
Maximum,
|
|
7
|
-
MaxLength,
|
|
8
|
-
Minimum,
|
|
9
|
-
MinLength,
|
|
10
|
-
Name,
|
|
11
|
-
Pattern,
|
|
12
|
-
Property,
|
|
13
|
-
Required
|
|
14
|
-
} from "@tsed/schema";
|
|
15
|
-
import {Schema as SchemaMongoose, Types} from "mongoose";
|
|
16
|
-
|
|
17
|
-
import {DiscriminatorKey} from "../decorators/discriminatorKey.js";
|
|
18
|
-
import {Model} from "../decorators/model.js";
|
|
19
|
-
import {ObjectID} from "../decorators/objectID.js";
|
|
20
|
-
import {Ref} from "../decorators/ref.js";
|
|
21
|
-
import {Schema} from "../decorators/schema.js";
|
|
22
|
-
import {SchemaIgnore} from "../decorators/schemaIgnore.js";
|
|
23
|
-
import {VersionKey} from "../decorators/versionKey.js";
|
|
24
|
-
import {VirtualRef, VirtualRefs} from "../decorators/virtualRef.js";
|
|
25
|
-
import {getSchema} from "./createSchema.js";
|
|
26
|
-
|
|
27
|
-
describe("createSchema", () => {
|
|
28
|
-
it("should create schema", () => {
|
|
29
|
-
enum MyEnum {
|
|
30
|
-
V1 = "v1",
|
|
31
|
-
V2 = "v2"
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// GIVEN
|
|
35
|
-
@Model()
|
|
36
|
-
class Test {
|
|
37
|
-
@Name("id")
|
|
38
|
-
_id: string;
|
|
39
|
-
|
|
40
|
-
@Minimum(0)
|
|
41
|
-
@Maximum(10)
|
|
42
|
-
test: number;
|
|
43
|
-
|
|
44
|
-
@MinLength(0)
|
|
45
|
-
@MaxLength(100)
|
|
46
|
-
@Pattern("pattern")
|
|
47
|
-
@Default("defaultValue")
|
|
48
|
-
name: string = "defaultValue";
|
|
49
|
-
|
|
50
|
-
@Enum(MyEnum)
|
|
51
|
-
enum: MyEnum;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// WHEN
|
|
55
|
-
const result = getSchema(Test);
|
|
56
|
-
|
|
57
|
-
// THEN
|
|
58
|
-
expect(result.obj).toEqual({
|
|
59
|
-
_id: {
|
|
60
|
-
required: false,
|
|
61
|
-
type: String
|
|
62
|
-
},
|
|
63
|
-
enum: {
|
|
64
|
-
enum: ["v1", "v2"],
|
|
65
|
-
required: false,
|
|
66
|
-
type: String
|
|
67
|
-
},
|
|
68
|
-
name: {
|
|
69
|
-
default: "defaultValue",
|
|
70
|
-
match: /pattern/,
|
|
71
|
-
maxlength: 100,
|
|
72
|
-
minlength: 0,
|
|
73
|
-
required: false,
|
|
74
|
-
type: String
|
|
75
|
-
},
|
|
76
|
-
test: {
|
|
77
|
-
max: 10,
|
|
78
|
-
min: 0,
|
|
79
|
-
required: false,
|
|
80
|
-
type: Number
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
it("should create schema with buffer", () => {
|
|
85
|
-
// GIVEN
|
|
86
|
-
@Model()
|
|
87
|
-
class Test {
|
|
88
|
-
@Name("id")
|
|
89
|
-
_id: string;
|
|
90
|
-
|
|
91
|
-
@Property(Buffer)
|
|
92
|
-
image: Buffer;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// WHEN
|
|
96
|
-
const result = getSchema(Test);
|
|
97
|
-
|
|
98
|
-
// THEN
|
|
99
|
-
expect(result.obj).toEqual({
|
|
100
|
-
_id: {
|
|
101
|
-
required: false,
|
|
102
|
-
type: String
|
|
103
|
-
},
|
|
104
|
-
image: {
|
|
105
|
-
required: false,
|
|
106
|
-
type: Buffer
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
it("should create schema with required property", () => {
|
|
111
|
-
// GIVEN
|
|
112
|
-
@Model()
|
|
113
|
-
class Test2 {
|
|
114
|
-
@Required()
|
|
115
|
-
test: string;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// WHEN
|
|
119
|
-
const result: any = getSchema(Test2);
|
|
120
|
-
|
|
121
|
-
// THEN
|
|
122
|
-
expect(result.obj.test.required).toBeInstanceOf(Function);
|
|
123
|
-
});
|
|
124
|
-
it("should create schema with subdocument", () => {
|
|
125
|
-
// GIVEN
|
|
126
|
-
enum MyEnum {
|
|
127
|
-
V1 = "v1",
|
|
128
|
-
V2 = "v2"
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
@Schema()
|
|
132
|
-
class Children {
|
|
133
|
-
@Name("id")
|
|
134
|
-
_id: string;
|
|
135
|
-
|
|
136
|
-
@Minimum(0)
|
|
137
|
-
@Maximum(10)
|
|
138
|
-
test: number;
|
|
139
|
-
|
|
140
|
-
@MinLength(0)
|
|
141
|
-
@MaxLength(100)
|
|
142
|
-
@Pattern("pattern")
|
|
143
|
-
@Default("defaultValue")
|
|
144
|
-
name: string = "defaultValue";
|
|
145
|
-
|
|
146
|
-
@Enum(MyEnum)
|
|
147
|
-
enum: MyEnum;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
@Model()
|
|
151
|
-
class Test3 {
|
|
152
|
-
@Property()
|
|
153
|
-
test: Children;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// WHEN
|
|
157
|
-
const testSchema = getSchema(Test3);
|
|
158
|
-
const childrenSchema = getSchema(Children);
|
|
159
|
-
|
|
160
|
-
// THEN
|
|
161
|
-
expect(testSchema.obj).toEqual({
|
|
162
|
-
test: {
|
|
163
|
-
type: childrenSchema,
|
|
164
|
-
required: false
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
expect(childrenSchema.obj).toEqual({
|
|
169
|
-
_id: {
|
|
170
|
-
required: false,
|
|
171
|
-
type: String
|
|
172
|
-
},
|
|
173
|
-
enum: {
|
|
174
|
-
enum: ["v1", "v2"],
|
|
175
|
-
required: false,
|
|
176
|
-
type: String
|
|
177
|
-
},
|
|
178
|
-
name: {
|
|
179
|
-
default: "defaultValue",
|
|
180
|
-
match: /pattern/,
|
|
181
|
-
maxlength: 100,
|
|
182
|
-
minlength: 0,
|
|
183
|
-
required: false,
|
|
184
|
-
type: String
|
|
185
|
-
},
|
|
186
|
-
test: {
|
|
187
|
-
max: 10,
|
|
188
|
-
min: 0,
|
|
189
|
-
required: false,
|
|
190
|
-
type: Number
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
it("should create schema with ref", () => {
|
|
195
|
-
// GIVEN
|
|
196
|
-
enum MyEnum {
|
|
197
|
-
V1 = "v1",
|
|
198
|
-
V2 = "v2"
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
@Model()
|
|
202
|
-
class Children {
|
|
203
|
-
@ObjectID("id")
|
|
204
|
-
_id: string;
|
|
205
|
-
|
|
206
|
-
@Minimum(0)
|
|
207
|
-
@Maximum(10)
|
|
208
|
-
test: number;
|
|
209
|
-
|
|
210
|
-
@MinLength(0)
|
|
211
|
-
@MaxLength(100)
|
|
212
|
-
@Pattern("pattern")
|
|
213
|
-
@Default("defaultValue")
|
|
214
|
-
name: string = "defaultValue";
|
|
215
|
-
|
|
216
|
-
@Enum(MyEnum)
|
|
217
|
-
enum: MyEnum;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
@Model()
|
|
221
|
-
class Test4 {
|
|
222
|
-
@Ref(Children)
|
|
223
|
-
test: Ref<Children>;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// WHEN
|
|
227
|
-
const testSchema = getSchema(Test4);
|
|
228
|
-
const childrenSchema = getSchema(Children);
|
|
229
|
-
|
|
230
|
-
// THEN
|
|
231
|
-
expect(testSchema.obj).toEqual({
|
|
232
|
-
test: {
|
|
233
|
-
type: SchemaMongoose.Types.ObjectId,
|
|
234
|
-
ref: "Children",
|
|
235
|
-
required: false
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
expect(childrenSchema.obj).toEqual({
|
|
240
|
-
_id: {
|
|
241
|
-
auto: true,
|
|
242
|
-
required: false,
|
|
243
|
-
type: Types.ObjectId
|
|
244
|
-
},
|
|
245
|
-
enum: {
|
|
246
|
-
enum: ["v1", "v2"],
|
|
247
|
-
required: false,
|
|
248
|
-
type: String
|
|
249
|
-
},
|
|
250
|
-
name: {
|
|
251
|
-
default: "defaultValue",
|
|
252
|
-
match: /pattern/,
|
|
253
|
-
maxlength: 100,
|
|
254
|
-
minlength: 0,
|
|
255
|
-
required: false,
|
|
256
|
-
type: String
|
|
257
|
-
},
|
|
258
|
-
test: {
|
|
259
|
-
max: 10,
|
|
260
|
-
min: 0,
|
|
261
|
-
required: false,
|
|
262
|
-
type: Number
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
expect(getJsonSchema(Test4)).toEqual({
|
|
267
|
-
definitions: {
|
|
268
|
-
Children: {
|
|
269
|
-
properties: {
|
|
270
|
-
enum: {
|
|
271
|
-
enum: ["v1", "v2"],
|
|
272
|
-
type: "string"
|
|
273
|
-
},
|
|
274
|
-
id: {
|
|
275
|
-
description: "An ObjectID",
|
|
276
|
-
examples: ["5ce7ad3028890bd71749d477"],
|
|
277
|
-
pattern: "^[0-9a-fA-F]{24}$",
|
|
278
|
-
type: "string"
|
|
279
|
-
},
|
|
280
|
-
name: {
|
|
281
|
-
default: "defaultValue",
|
|
282
|
-
maxLength: 100,
|
|
283
|
-
minLength: 0,
|
|
284
|
-
pattern: "pattern",
|
|
285
|
-
type: "string"
|
|
286
|
-
},
|
|
287
|
-
test: {
|
|
288
|
-
maximum: 10,
|
|
289
|
-
minimum: 0,
|
|
290
|
-
type: "number"
|
|
291
|
-
}
|
|
292
|
-
},
|
|
293
|
-
type: "object"
|
|
294
|
-
}
|
|
295
|
-
},
|
|
296
|
-
properties: {
|
|
297
|
-
test: {
|
|
298
|
-
oneOf: [
|
|
299
|
-
{
|
|
300
|
-
description: "A reference ObjectID",
|
|
301
|
-
examples: ["5ce7ad3028890bd71749d477"],
|
|
302
|
-
type: "string"
|
|
303
|
-
},
|
|
304
|
-
{
|
|
305
|
-
$ref: "#/definitions/Children"
|
|
306
|
-
}
|
|
307
|
-
]
|
|
308
|
-
}
|
|
309
|
-
},
|
|
310
|
-
type: "object"
|
|
311
|
-
});
|
|
312
|
-
expect(getJsonSchema(Children)).toEqual({
|
|
313
|
-
properties: {
|
|
314
|
-
enum: {
|
|
315
|
-
enum: ["v1", "v2"],
|
|
316
|
-
type: "string"
|
|
317
|
-
},
|
|
318
|
-
id: {
|
|
319
|
-
description: "An ObjectID",
|
|
320
|
-
examples: ["5ce7ad3028890bd71749d477"],
|
|
321
|
-
pattern: "^[0-9a-fA-F]{24}$",
|
|
322
|
-
type: "string"
|
|
323
|
-
},
|
|
324
|
-
name: {
|
|
325
|
-
default: "defaultValue",
|
|
326
|
-
maxLength: 100,
|
|
327
|
-
minLength: 0,
|
|
328
|
-
pattern: "pattern",
|
|
329
|
-
type: "string"
|
|
330
|
-
},
|
|
331
|
-
test: {
|
|
332
|
-
maximum: 10,
|
|
333
|
-
minimum: 0,
|
|
334
|
-
type: "number"
|
|
335
|
-
}
|
|
336
|
-
},
|
|
337
|
-
type: "object"
|
|
338
|
-
});
|
|
339
|
-
});
|
|
340
|
-
it("should create schema with virtual ref", () => {
|
|
341
|
-
// GIVEN
|
|
342
|
-
enum MyEnum {
|
|
343
|
-
V1 = "v1",
|
|
344
|
-
V2 = "v2"
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
@Model()
|
|
348
|
-
class Children2 {
|
|
349
|
-
@Name("id")
|
|
350
|
-
_id: string;
|
|
351
|
-
|
|
352
|
-
@Minimum(0)
|
|
353
|
-
@Maximum(10)
|
|
354
|
-
test: number;
|
|
355
|
-
|
|
356
|
-
@MinLength(0)
|
|
357
|
-
@MaxLength(100)
|
|
358
|
-
@Pattern("pattern")
|
|
359
|
-
@Default("defaultValue")
|
|
360
|
-
name: string = "defaultValue";
|
|
361
|
-
|
|
362
|
-
@Enum(MyEnum)
|
|
363
|
-
enum: MyEnum;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
@Model()
|
|
367
|
-
class Test5 {
|
|
368
|
-
@VirtualRef({ref: Children2, foreignField: "foo", justOne: true})
|
|
369
|
-
test: VirtualRef<Children2>;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
// WHEN
|
|
373
|
-
const testSchema: any = getSchema(Test5);
|
|
374
|
-
|
|
375
|
-
// THEN
|
|
376
|
-
expect(testSchema.obj).toEqual({});
|
|
377
|
-
expect(testSchema.virtuals.test.options).toMatchObject({
|
|
378
|
-
foreignField: "foo",
|
|
379
|
-
justOne: true,
|
|
380
|
-
localField: "_id",
|
|
381
|
-
options: undefined,
|
|
382
|
-
ref: "Children2"
|
|
383
|
-
});
|
|
384
|
-
});
|
|
385
|
-
it("should create schema with virtual ref (array)", () => {
|
|
386
|
-
// GIVEN
|
|
387
|
-
enum MyEnum {
|
|
388
|
-
V1 = "v1",
|
|
389
|
-
V2 = "v2"
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
@Model()
|
|
393
|
-
class Children2 {
|
|
394
|
-
@Name("id")
|
|
395
|
-
_id: string;
|
|
396
|
-
|
|
397
|
-
@Minimum(0)
|
|
398
|
-
@Maximum(10)
|
|
399
|
-
test: number;
|
|
400
|
-
|
|
401
|
-
@MinLength(0)
|
|
402
|
-
@MaxLength(100)
|
|
403
|
-
@Pattern("pattern")
|
|
404
|
-
@Default("defaultValue")
|
|
405
|
-
name: string = "defaultValue";
|
|
406
|
-
|
|
407
|
-
@Enum(MyEnum)
|
|
408
|
-
enum: MyEnum;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
@Model()
|
|
412
|
-
class Test5 {
|
|
413
|
-
@VirtualRef({ref: Children2, justOne: false, foreignField: "foo"})
|
|
414
|
-
test: VirtualRefs<Children2>;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
// WHEN
|
|
418
|
-
const testSchema: any = getSchema(Test5);
|
|
419
|
-
|
|
420
|
-
// THEN
|
|
421
|
-
expect(testSchema.obj).toEqual({});
|
|
422
|
-
expect(testSchema.virtuals.test.options).toMatchObject({
|
|
423
|
-
foreignField: "foo",
|
|
424
|
-
justOne: false,
|
|
425
|
-
localField: "_id",
|
|
426
|
-
options: undefined,
|
|
427
|
-
ref: "Children2"
|
|
428
|
-
});
|
|
429
|
-
});
|
|
430
|
-
it("should create schema with collection (Array of subdocument)", () => {
|
|
431
|
-
// GIVEN
|
|
432
|
-
enum MyEnum {
|
|
433
|
-
V1 = "v1",
|
|
434
|
-
V2 = "v2"
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
@Schema()
|
|
438
|
-
class Children {
|
|
439
|
-
@Name("id")
|
|
440
|
-
_id: string;
|
|
441
|
-
|
|
442
|
-
@Minimum(0)
|
|
443
|
-
@Maximum(10)
|
|
444
|
-
test: number;
|
|
445
|
-
|
|
446
|
-
@MinLength(0)
|
|
447
|
-
@MaxLength(100)
|
|
448
|
-
@Pattern("pattern")
|
|
449
|
-
@Default("defaultValue")
|
|
450
|
-
name: string = "defaultValue";
|
|
451
|
-
|
|
452
|
-
@Enum(MyEnum)
|
|
453
|
-
enum: MyEnum;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
@Model()
|
|
457
|
-
class Test6 {
|
|
458
|
-
@CollectionOf(Children)
|
|
459
|
-
tests: Children[];
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
// WHEN
|
|
463
|
-
const testSchema = getSchema(Test6);
|
|
464
|
-
const childrenSchema = getSchema(Children);
|
|
465
|
-
|
|
466
|
-
// THEN
|
|
467
|
-
expect(testSchema.obj).toEqual({
|
|
468
|
-
tests: {
|
|
469
|
-
type: [childrenSchema],
|
|
470
|
-
required: false
|
|
471
|
-
}
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
expect(childrenSchema.obj).toEqual({
|
|
475
|
-
_id: {
|
|
476
|
-
required: false,
|
|
477
|
-
type: String
|
|
478
|
-
},
|
|
479
|
-
enum: {
|
|
480
|
-
enum: ["v1", "v2"],
|
|
481
|
-
required: false,
|
|
482
|
-
type: String
|
|
483
|
-
},
|
|
484
|
-
name: {
|
|
485
|
-
default: "defaultValue",
|
|
486
|
-
match: /pattern/,
|
|
487
|
-
maxlength: 100,
|
|
488
|
-
minlength: 0,
|
|
489
|
-
required: false,
|
|
490
|
-
type: String
|
|
491
|
-
},
|
|
492
|
-
test: {
|
|
493
|
-
max: 10,
|
|
494
|
-
min: 0,
|
|
495
|
-
required: false,
|
|
496
|
-
type: Number
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
});
|
|
500
|
-
it("should create schema with collection (Array of subdocument and default value undefined)", () => {
|
|
501
|
-
// GIVEN
|
|
502
|
-
enum MyEnum {
|
|
503
|
-
V1 = "v1",
|
|
504
|
-
V2 = "v2"
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
@Schema()
|
|
508
|
-
class Children {
|
|
509
|
-
@Name("id")
|
|
510
|
-
_id: string;
|
|
511
|
-
|
|
512
|
-
@Enum(MyEnum)
|
|
513
|
-
@Default(undefined)
|
|
514
|
-
enum?: MyEnum[];
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
@Model()
|
|
518
|
-
class Test6 {
|
|
519
|
-
@CollectionOf(Children)
|
|
520
|
-
@Default(undefined)
|
|
521
|
-
tests?: Children[];
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
// WHEN
|
|
525
|
-
const testSchema = getSchema(Test6);
|
|
526
|
-
const childrenSchema = getSchema(Children);
|
|
527
|
-
|
|
528
|
-
// THEN
|
|
529
|
-
expect(testSchema.obj).toEqual({
|
|
530
|
-
tests: {
|
|
531
|
-
type: [childrenSchema],
|
|
532
|
-
required: false
|
|
533
|
-
}
|
|
534
|
-
});
|
|
535
|
-
|
|
536
|
-
expect(childrenSchema.obj).toEqual({
|
|
537
|
-
_id: {
|
|
538
|
-
required: false,
|
|
539
|
-
type: String
|
|
540
|
-
},
|
|
541
|
-
enum: {
|
|
542
|
-
enum: ["v1", "v2"],
|
|
543
|
-
required: false,
|
|
544
|
-
type: [String]
|
|
545
|
-
}
|
|
546
|
-
});
|
|
547
|
-
});
|
|
548
|
-
it("should create schema with collection (Array of ref)", () => {
|
|
549
|
-
// GIVEN
|
|
550
|
-
enum MyEnum {
|
|
551
|
-
V1 = "v1",
|
|
552
|
-
V2 = "v2"
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
@Model()
|
|
556
|
-
class Children3 {
|
|
557
|
-
@Name("id")
|
|
558
|
-
_id: string;
|
|
559
|
-
|
|
560
|
-
@Minimum(0)
|
|
561
|
-
@Maximum(10)
|
|
562
|
-
test: number;
|
|
563
|
-
|
|
564
|
-
@MinLength(0)
|
|
565
|
-
@MaxLength(100)
|
|
566
|
-
@Pattern("pattern")
|
|
567
|
-
@Default("defaultValue")
|
|
568
|
-
name: string = "defaultValue";
|
|
569
|
-
|
|
570
|
-
@Enum(MyEnum)
|
|
571
|
-
enum: MyEnum;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
@Model()
|
|
575
|
-
class Test7 {
|
|
576
|
-
@Ref(Children3)
|
|
577
|
-
tests: Ref<Children3>[];
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
// WHEN
|
|
581
|
-
const testSchema = getSchema(Test7);
|
|
582
|
-
|
|
583
|
-
// THEN
|
|
584
|
-
expect(testSchema.obj).toEqual({
|
|
585
|
-
tests: {
|
|
586
|
-
type: [SchemaMongoose.Types.ObjectId],
|
|
587
|
-
ref: "Children3",
|
|
588
|
-
required: false
|
|
589
|
-
}
|
|
590
|
-
});
|
|
591
|
-
});
|
|
592
|
-
it("should create schema with collection (Array of virtual ref", () => {
|
|
593
|
-
// GIVEN
|
|
594
|
-
enum MyEnum {
|
|
595
|
-
V1 = "v1",
|
|
596
|
-
V2 = "v2"
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
@Model()
|
|
600
|
-
class Children4 {
|
|
601
|
-
@Name("id")
|
|
602
|
-
_id: string;
|
|
603
|
-
|
|
604
|
-
@Minimum(0)
|
|
605
|
-
@Maximum(10)
|
|
606
|
-
test: number;
|
|
607
|
-
|
|
608
|
-
@MinLength(0)
|
|
609
|
-
@MaxLength(100)
|
|
610
|
-
@Pattern("pattern")
|
|
611
|
-
@Default("defaultValue")
|
|
612
|
-
name: string = "defaultValue";
|
|
613
|
-
|
|
614
|
-
@Enum(MyEnum)
|
|
615
|
-
enum: MyEnum;
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
@Model()
|
|
619
|
-
class Test8 {
|
|
620
|
-
@VirtualRef({ref: Children4, foreignField: "foo"})
|
|
621
|
-
tests: VirtualRef<Children4>[];
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
// WHEN
|
|
625
|
-
const testSchema = getSchema(Test8);
|
|
626
|
-
|
|
627
|
-
// THEN
|
|
628
|
-
expect(testSchema.obj).toEqual({});
|
|
629
|
-
// @ts-ignore
|
|
630
|
-
expect(testSchema.virtuals.tests.options).toMatchObject({
|
|
631
|
-
foreignField: "foo",
|
|
632
|
-
justOne: false,
|
|
633
|
-
localField: "_id",
|
|
634
|
-
options: undefined,
|
|
635
|
-
ref: "Children4"
|
|
636
|
-
});
|
|
637
|
-
});
|
|
638
|
-
it("should create schema with collection (Map of subdocument)", () => {
|
|
639
|
-
// GIVEN
|
|
640
|
-
enum MyEnum {
|
|
641
|
-
V1 = "v1",
|
|
642
|
-
V2 = "v2"
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
@Schema()
|
|
646
|
-
class Children {
|
|
647
|
-
@Name("id")
|
|
648
|
-
_id: string;
|
|
649
|
-
|
|
650
|
-
@Minimum(0)
|
|
651
|
-
@Maximum(10)
|
|
652
|
-
test: number;
|
|
653
|
-
|
|
654
|
-
@MinLength(0)
|
|
655
|
-
@MaxLength(100)
|
|
656
|
-
@Pattern("pattern")
|
|
657
|
-
@Default("defaultValue")
|
|
658
|
-
name: string = "defaultValue";
|
|
659
|
-
|
|
660
|
-
@Enum(MyEnum)
|
|
661
|
-
enum: MyEnum;
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
@Model()
|
|
665
|
-
class Test9 {
|
|
666
|
-
@CollectionOf(Children)
|
|
667
|
-
tests: Map<string, Children>;
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
// WHEN
|
|
671
|
-
const testSchema = getSchema(Test9);
|
|
672
|
-
const childrenSchema = getSchema(Children);
|
|
673
|
-
|
|
674
|
-
// THEN
|
|
675
|
-
expect(testSchema.obj).toEqual({
|
|
676
|
-
tests: {
|
|
677
|
-
type: Map,
|
|
678
|
-
of: {
|
|
679
|
-
type: childrenSchema
|
|
680
|
-
},
|
|
681
|
-
required: false
|
|
682
|
-
}
|
|
683
|
-
});
|
|
684
|
-
|
|
685
|
-
expect(childrenSchema.obj).toEqual({
|
|
686
|
-
_id: {
|
|
687
|
-
required: false,
|
|
688
|
-
type: String
|
|
689
|
-
},
|
|
690
|
-
enum: {
|
|
691
|
-
enum: ["v1", "v2"],
|
|
692
|
-
required: false,
|
|
693
|
-
type: String
|
|
694
|
-
},
|
|
695
|
-
name: {
|
|
696
|
-
default: "defaultValue",
|
|
697
|
-
match: /pattern/,
|
|
698
|
-
maxlength: 100,
|
|
699
|
-
minlength: 0,
|
|
700
|
-
required: false,
|
|
701
|
-
type: String
|
|
702
|
-
},
|
|
703
|
-
test: {
|
|
704
|
-
max: 10,
|
|
705
|
-
min: 0,
|
|
706
|
-
required: false,
|
|
707
|
-
type: Number
|
|
708
|
-
}
|
|
709
|
-
});
|
|
710
|
-
});
|
|
711
|
-
it("should throw error with collection (Set of subdocument)", () => {
|
|
712
|
-
// GIVEN
|
|
713
|
-
enum MyEnum {
|
|
714
|
-
V1 = "v1",
|
|
715
|
-
V2 = "v2"
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
let actualError: any;
|
|
719
|
-
try {
|
|
720
|
-
@Schema()
|
|
721
|
-
class Children {
|
|
722
|
-
@Name("id")
|
|
723
|
-
_id: string;
|
|
724
|
-
|
|
725
|
-
@Minimum(0)
|
|
726
|
-
@Maximum(10)
|
|
727
|
-
test: number;
|
|
728
|
-
|
|
729
|
-
@MinLength(0)
|
|
730
|
-
@MaxLength(100)
|
|
731
|
-
@Pattern("pattern")
|
|
732
|
-
@Default("defaultValue")
|
|
733
|
-
name: string = "defaultValue";
|
|
734
|
-
|
|
735
|
-
@Enum(MyEnum)
|
|
736
|
-
enum: MyEnum;
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
class TestWithSet9 {
|
|
740
|
-
@CollectionOf(Children)
|
|
741
|
-
tests: Set<Children>;
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
getSchema(TestWithSet9);
|
|
745
|
-
} catch (er) {
|
|
746
|
-
actualError = er;
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
expect(actualError).toBeInstanceOf(Error);
|
|
750
|
-
expect(actualError.message).toBe("Invalid collection type. Set is not supported.");
|
|
751
|
-
});
|
|
752
|
-
it("should not create schema property for ignored field", () => {
|
|
753
|
-
@Model()
|
|
754
|
-
class Test10 {
|
|
755
|
-
@Property()
|
|
756
|
-
field: string;
|
|
757
|
-
@Property()
|
|
758
|
-
@SchemaIgnore()
|
|
759
|
-
kind: string;
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
const testSchema = getSchema(Test10);
|
|
763
|
-
expect(testSchema.obj).toEqual({
|
|
764
|
-
field: {
|
|
765
|
-
required: false,
|
|
766
|
-
type: String
|
|
767
|
-
}
|
|
768
|
-
});
|
|
769
|
-
});
|
|
770
|
-
it("should create schema with discriminator key", () => {
|
|
771
|
-
@Model()
|
|
772
|
-
class Test11 {
|
|
773
|
-
@DiscriminatorKey()
|
|
774
|
-
kind: string;
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
const testSchema = getSchema(Test11);
|
|
778
|
-
// @ts-ignore
|
|
779
|
-
const options = testSchema.options;
|
|
780
|
-
|
|
781
|
-
expect(options.discriminatorKey).toBe("kind");
|
|
782
|
-
});
|
|
783
|
-
it("should create schema with version key", () => {
|
|
784
|
-
@Model()
|
|
785
|
-
class Test12 {
|
|
786
|
-
@VersionKey()
|
|
787
|
-
version: number;
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
const testSchema = getSchema(Test12);
|
|
791
|
-
// @ts-ignore
|
|
792
|
-
const options = testSchema.options;
|
|
793
|
-
|
|
794
|
-
expect(options.versionKey).toBe("version");
|
|
795
|
-
});
|
|
796
|
-
|
|
797
|
-
it("should create json schema with virtual ref and count", () => {
|
|
798
|
-
// GIVEN
|
|
799
|
-
@Model()
|
|
800
|
-
class Children5 {
|
|
801
|
-
@Name("id")
|
|
802
|
-
_id: string;
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
@Model()
|
|
806
|
-
class Test13 {
|
|
807
|
-
@VirtualRef({ref: Children5, foreignField: "test13", count: true})
|
|
808
|
-
childrenCount: number;
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
// WHEN
|
|
812
|
-
const testSchema: any = getJsonSchema(Test13);
|
|
813
|
-
|
|
814
|
-
// THEN
|
|
815
|
-
expect(testSchema.properties.childrenCount.type).toEqual("number");
|
|
816
|
-
});
|
|
817
|
-
});
|