@martel/calyx 1.12.0 → 1.13.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/CHANGELOG.md +15 -0
- package/README.md +5 -3
- package/benchmarks/graphql-benchmark.ts +4 -1
- package/benchmarks/serialization-benchmark.ts +46 -6
- package/benchmarks/techniques-benchmark.ts +12 -0
- package/benchmarks/validation-benchmark.ts +8 -1
- package/docs/controllers.md +3 -3
- package/docs/dependency-injection.md +3 -3
- package/docs/lifecycle.md +3 -3
- package/package.json +1 -1
- package/src/cli/index.ts +7 -1
- package/src/config/config.module.ts +16 -2
- package/src/config/config.service.ts +20 -6
- package/src/cookies/cookies.ts +45 -8
- package/src/core/container.ts +340 -154
- package/src/core/testing-module.ts +4 -0
- package/src/cqrs/cqrs.ts +93 -4
- package/src/database/sequelize.module.ts +239 -0
- package/src/event-emitter/decorators.ts +2 -2
- package/src/event-emitter/event-emitter.ts +3 -0
- package/src/graphql/graphql.module.ts +2 -4
- package/src/http/application.ts +140 -10
- package/src/http/decorators.ts +21 -1
- package/src/http/exceptions.ts +97 -0
- package/src/http/factory.ts +3 -0
- package/src/http/router.ts +27 -4
- package/src/index.ts +1 -0
- package/src/microservices/exceptions.ts +10 -0
- package/src/microservices/index.ts +1 -0
- package/src/queue/queue.module.ts +73 -5
- package/src/terminus/terminus.ts +75 -2
- package/src/validation/compiler.ts +137 -17
- package/src/validation/decorators.ts +164 -2
- package/src/websockets/exceptions.ts +10 -0
- package/src/websockets/index.ts +1 -0
- package/tests/circular-di.test.ts +151 -0
- package/tests/di.test.ts +10 -2
- package/tests/nestjs-parity.test.ts +255 -0
|
@@ -28,16 +28,139 @@ export class ValidationCompiler {
|
|
|
28
28
|
|
|
29
29
|
const propCode: string[] = [];
|
|
30
30
|
for (const rule of propRules) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
31
|
+
const args = rule.args || [];
|
|
32
|
+
const type = rule.type;
|
|
33
|
+
|
|
34
|
+
if (type === 'optional') continue;
|
|
35
|
+
|
|
36
|
+
switch (type) {
|
|
37
|
+
case 'isDefined':
|
|
38
|
+
propCode.push(`if (obj.${prop} === undefined || obj.${prop} === null) errors.push('${prop} must be defined');`);
|
|
39
|
+
break;
|
|
40
|
+
case 'equals':
|
|
41
|
+
propCode.push(`if (obj.${prop} !== ${JSON.stringify(args[0])}) errors.push('${prop} must be equal to ${args[0]}');`);
|
|
42
|
+
break;
|
|
43
|
+
case 'notEquals':
|
|
44
|
+
propCode.push(`if (obj.${prop} === ${JSON.stringify(args[0])}) errors.push('${prop} must not be equal to ${args[0]}');`);
|
|
45
|
+
break;
|
|
46
|
+
case 'isEmpty':
|
|
47
|
+
propCode.push(`if (obj.${prop} !== '' && obj.${prop} !== undefined && obj.${prop} !== null) errors.push('${prop} must be empty');`);
|
|
48
|
+
break;
|
|
49
|
+
case 'isNotEmpty':
|
|
50
|
+
propCode.push(`if (obj.${prop} === '' || obj.${prop} === undefined || obj.${prop} === null) errors.push('${prop} should not be empty');`);
|
|
51
|
+
break;
|
|
52
|
+
case 'isIn':
|
|
53
|
+
propCode.push(`if (!${JSON.stringify(args[0])}.includes(obj.${prop})) errors.push('${prop} must be one of the following values: ' + ${JSON.stringify(args[0])}.join(', '));`);
|
|
54
|
+
break;
|
|
55
|
+
case 'isNotIn':
|
|
56
|
+
propCode.push(`if (${JSON.stringify(args[0])}.includes(obj.${prop})) errors.push('${prop} must not be one of the following values: ' + ${JSON.stringify(args[0])}.join(', '));`);
|
|
57
|
+
break;
|
|
58
|
+
case 'isBoolean':
|
|
59
|
+
propCode.push(`if (typeof obj.${prop} !== 'boolean') errors.push('${prop} must be a boolean');`);
|
|
60
|
+
break;
|
|
61
|
+
case 'isDate':
|
|
62
|
+
propCode.push(`if (!(obj.${prop} instanceof Date) || isNaN(obj.${prop}.getTime())) errors.push('${prop} must be a Date instance');`);
|
|
63
|
+
break;
|
|
64
|
+
case 'string':
|
|
65
|
+
propCode.push(`if (typeof obj.${prop} !== 'string') errors.push('${prop} must be a string');`);
|
|
66
|
+
break;
|
|
67
|
+
case 'number':
|
|
68
|
+
propCode.push(`if (typeof obj.${prop} !== 'number' || isNaN(obj.${prop})) errors.push('${prop} must be a number');`);
|
|
69
|
+
break;
|
|
70
|
+
case 'isInt':
|
|
71
|
+
propCode.push(`if (typeof obj.${prop} !== 'number' || !Number.isInteger(obj.${prop})) errors.push('${prop} must be an integer');`);
|
|
72
|
+
break;
|
|
73
|
+
case 'isArray':
|
|
74
|
+
propCode.push(`if (!Array.isArray(obj.${prop})) errors.push('${prop} must be an array');`);
|
|
75
|
+
break;
|
|
76
|
+
case 'isEnum':
|
|
77
|
+
const enumValues = Object.values(args[0]);
|
|
78
|
+
propCode.push(`if (!${JSON.stringify(enumValues)}.includes(obj.${prop})) errors.push('${prop} must be a valid enum value');`);
|
|
79
|
+
break;
|
|
80
|
+
case 'isObject':
|
|
81
|
+
propCode.push(`if (typeof obj.${prop} !== 'object' || obj.${prop} === null || Array.isArray(obj.${prop})) errors.push('${prop} must be an object');`);
|
|
82
|
+
break;
|
|
83
|
+
case 'isPositive':
|
|
84
|
+
propCode.push(`if (typeof obj.${prop} !== 'number' || obj.${prop} <= 0) errors.push('${prop} must be a positive number');`);
|
|
85
|
+
break;
|
|
86
|
+
case 'isNegative':
|
|
87
|
+
propCode.push(`if (typeof obj.${prop} !== 'number' || obj.${prop} >= 0) errors.push('${prop} must be a negative number');`);
|
|
88
|
+
break;
|
|
89
|
+
case 'min':
|
|
90
|
+
propCode.push(`if (typeof obj.${prop} !== 'number' || obj.${prop} < ${args[0]}) errors.push('${prop} must not be less than ${args[0]}');`);
|
|
91
|
+
break;
|
|
92
|
+
case 'max':
|
|
93
|
+
propCode.push(`if (typeof obj.${prop} !== 'number' || obj.${prop} > ${args[0]}) errors.push('${prop} must not be greater than ${args[0]}');`);
|
|
94
|
+
break;
|
|
95
|
+
case 'contains':
|
|
96
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || !obj.${prop}.includes(${JSON.stringify(args[0])})) errors.push('${prop} must contain a ${args[0]} string');`);
|
|
97
|
+
break;
|
|
98
|
+
case 'notContains':
|
|
99
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || obj.${prop}.includes(${JSON.stringify(args[0])})) errors.push('${prop} must not contain a ${args[0]} string');`);
|
|
100
|
+
break;
|
|
101
|
+
case 'isAlpha':
|
|
102
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || !/^[a-zA-Z]+$/.test(obj.${prop})) errors.push('${prop} must contain only letters');`);
|
|
103
|
+
break;
|
|
104
|
+
case 'isAlphanumeric':
|
|
105
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || !/^[a-zA-Z0-9]+$/.test(obj.${prop})) errors.push('${prop} must contain only letters and numbers');`);
|
|
106
|
+
break;
|
|
107
|
+
case 'isDecimal':
|
|
108
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' && typeof obj.${prop} !== 'number' || !/^\\d+\\.\\d+$/.test(String(obj.${prop}))) errors.push('${prop} must be a decimal number');`);
|
|
109
|
+
break;
|
|
110
|
+
case 'email':
|
|
111
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || !obj.${prop}.includes('@')) errors.push('${prop} must be a valid email');`);
|
|
112
|
+
break;
|
|
113
|
+
case 'isUrl':
|
|
114
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || !/^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$/.test(obj.${prop})) errors.push('${prop} must be a URL address');`);
|
|
115
|
+
break;
|
|
116
|
+
case 'isIP':
|
|
117
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || !/^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$/.test(obj.${prop})) errors.push('${prop} must be an IP address');`);
|
|
118
|
+
break;
|
|
119
|
+
case 'isPort':
|
|
120
|
+
propCode.push(`if (typeof obj.${prop} !== 'number' && typeof obj.${prop} !== 'string' || Number(obj.${prop}) < 0 || Number(obj.${prop}) > 65535) errors.push('${prop} must be a port');`);
|
|
121
|
+
break;
|
|
122
|
+
case 'isJSON':
|
|
123
|
+
propCode.push(`if (typeof obj.${prop} !== 'string') errors.push('${prop} must be a json string'); else { try { JSON.parse(obj.${prop}); } catch { errors.push('${prop} must be a json string'); } }`);
|
|
124
|
+
break;
|
|
125
|
+
case 'isLowercase':
|
|
126
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || obj.${prop} !== obj.${prop}.toLowerCase()) errors.push('${prop} must be a lowercase string');`);
|
|
127
|
+
break;
|
|
128
|
+
case 'isUppercase':
|
|
129
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || obj.${prop} !== obj.${prop}.toUpperCase()) errors.push('${prop} must be a uppercase string');`);
|
|
130
|
+
break;
|
|
131
|
+
case 'isNumberString':
|
|
132
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || isNaN(Number(obj.${prop}))) errors.push('${prop} must be a number string');`);
|
|
133
|
+
break;
|
|
134
|
+
case 'isUUID':
|
|
135
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(obj.${prop})) errors.push('${prop} must be a UUID');`);
|
|
136
|
+
break;
|
|
137
|
+
case 'isDateString':
|
|
138
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || isNaN(Date.parse(obj.${prop}))) errors.push('${prop} must be a valid ISO 8601 date string');`);
|
|
139
|
+
break;
|
|
140
|
+
case 'length':
|
|
141
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || obj.${prop}.length < ${args[0]} || obj.${prop}.length > ${args[1]}) errors.push('${prop} length must be between ${args[0]} and ${args[1]}');`);
|
|
142
|
+
break;
|
|
143
|
+
case 'minLength':
|
|
144
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || obj.${prop}.length < ${args[0]}) errors.push('${prop} length must be at least ${args[0]}');`);
|
|
145
|
+
break;
|
|
146
|
+
case 'maxLength':
|
|
147
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || obj.${prop}.length > ${args[0]}) errors.push('${prop} length must not exceed ${args[0]}');`);
|
|
148
|
+
break;
|
|
149
|
+
case 'matches':
|
|
150
|
+
propCode.push(`if (typeof obj.${prop} !== 'string' || !${args[0].toString()}.test(obj.${prop})) errors.push('${prop} must match regular expression');`);
|
|
151
|
+
break;
|
|
152
|
+
case 'arrayNotEmpty':
|
|
153
|
+
propCode.push(`if (!Array.isArray(obj.${prop}) || obj.${prop}.length === 0) errors.push('${prop} should not be empty');`);
|
|
154
|
+
break;
|
|
155
|
+
case 'arrayMinSize':
|
|
156
|
+
propCode.push(`if (!Array.isArray(obj.${prop}) || obj.${prop}.length < ${args[0]}) errors.push('${prop} must contain at least ${args[0]} elements');`);
|
|
157
|
+
break;
|
|
158
|
+
case 'arrayMaxSize':
|
|
159
|
+
propCode.push(`if (!Array.isArray(obj.${prop}) || obj.${prop}.length > ${args[0]}) errors.push('${prop} must contain not more than ${args[0]} elements');`);
|
|
160
|
+
break;
|
|
161
|
+
case 'arrayUnique':
|
|
162
|
+
propCode.push(`if (!Array.isArray(obj.${prop}) || new Set(obj.${prop}).size !== obj.${prop}.length) errors.push('${prop} must contain unique elements');`);
|
|
163
|
+
break;
|
|
41
164
|
}
|
|
42
165
|
}
|
|
43
166
|
|
|
@@ -92,15 +215,12 @@ export class SerializationCompiler {
|
|
|
92
215
|
const jsonParts: string[] = [];
|
|
93
216
|
for (const key of keys) {
|
|
94
217
|
const propRules = rules.filter((r) => r.propertyKey === key);
|
|
95
|
-
const
|
|
96
|
-
const
|
|
218
|
+
const designType = Reflect.getMetadata('design:type', dtoClass.prototype, key);
|
|
219
|
+
const isNumber = propRules.some((r) => r.type === 'number') || designType === Number;
|
|
220
|
+
const isBoolean = propRules.some((r) => r.type === 'isBoolean') || designType === Boolean;
|
|
97
221
|
|
|
98
|
-
if (isNumber) {
|
|
222
|
+
if (isNumber || isBoolean) {
|
|
99
223
|
jsonParts.push(`"${key}":\${obj.${key} === undefined || obj.${key} === null ? null : obj.${key}}`);
|
|
100
|
-
} else if (isString) {
|
|
101
|
-
jsonParts.push(
|
|
102
|
-
`"${key}":\${obj.${key} === undefined || obj.${key} === null ? null : JSON.stringify(obj.${key})}`
|
|
103
|
-
);
|
|
104
224
|
} else {
|
|
105
225
|
jsonParts.push(
|
|
106
226
|
`"${key}":\${obj.${key} === undefined || obj.${key} === null ? null : JSON.stringify(obj.${key})}`
|
|
@@ -12,6 +12,48 @@ function registerValidationRule(type: string, target: any, propertyKey: string,
|
|
|
12
12
|
Reflect.defineMetadata('calyx:validation_rules', existing, target.constructor);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
// 1. Conditional & Basic
|
|
16
|
+
export function IsOptional(): PropertyDecorator {
|
|
17
|
+
return (target, propertyKey) => registerValidationRule('optional', target, String(propertyKey));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function IsDefined(): PropertyDecorator {
|
|
21
|
+
return (target, propertyKey) => registerValidationRule('isDefined', target, String(propertyKey));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function Equals(value: any): PropertyDecorator {
|
|
25
|
+
return (target, propertyKey) => registerValidationRule('equals', target, String(propertyKey), [value]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function NotEquals(value: any): PropertyDecorator {
|
|
29
|
+
return (target, propertyKey) => registerValidationRule('notEquals', target, String(propertyKey), [value]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function IsEmpty(): PropertyDecorator {
|
|
33
|
+
return (target, propertyKey) => registerValidationRule('isEmpty', target, String(propertyKey));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function IsNotEmpty(): PropertyDecorator {
|
|
37
|
+
return (target, propertyKey) => registerValidationRule('isNotEmpty', target, String(propertyKey));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function IsIn(values: any[]): PropertyDecorator {
|
|
41
|
+
return (target, propertyKey) => registerValidationRule('isIn', target, String(propertyKey), [values]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function IsNotIn(values: any[]): PropertyDecorator {
|
|
45
|
+
return (target, propertyKey) => registerValidationRule('isNotIn', target, String(propertyKey), [values]);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 2. Core Types
|
|
49
|
+
export function IsBoolean(): PropertyDecorator {
|
|
50
|
+
return (target, propertyKey) => registerValidationRule('isBoolean', target, String(propertyKey));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function IsDate(): PropertyDecorator {
|
|
54
|
+
return (target, propertyKey) => registerValidationRule('isDate', target, String(propertyKey));
|
|
55
|
+
}
|
|
56
|
+
|
|
15
57
|
export function IsString(): PropertyDecorator {
|
|
16
58
|
return (target, propertyKey) => registerValidationRule('string', target, String(propertyKey));
|
|
17
59
|
}
|
|
@@ -20,14 +62,134 @@ export function IsNumber(): PropertyDecorator {
|
|
|
20
62
|
return (target, propertyKey) => registerValidationRule('number', target, String(propertyKey));
|
|
21
63
|
}
|
|
22
64
|
|
|
23
|
-
export function
|
|
24
|
-
return (target, propertyKey) => registerValidationRule('
|
|
65
|
+
export function IsInt(): PropertyDecorator {
|
|
66
|
+
return (target, propertyKey) => registerValidationRule('isInt', target, String(propertyKey));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function IsArray(): PropertyDecorator {
|
|
70
|
+
return (target, propertyKey) => registerValidationRule('isArray', target, String(propertyKey));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function IsEnum(entity: any): PropertyDecorator {
|
|
74
|
+
return (target, propertyKey) => registerValidationRule('isEnum', target, String(propertyKey), [entity]);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function IsObject(): PropertyDecorator {
|
|
78
|
+
return (target, propertyKey) => registerValidationRule('isObject', target, String(propertyKey));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 3. Number Constraints
|
|
82
|
+
export function IsPositive(): PropertyDecorator {
|
|
83
|
+
return (target, propertyKey) => registerValidationRule('isPositive', target, String(propertyKey));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function IsNegative(): PropertyDecorator {
|
|
87
|
+
return (target, propertyKey) => registerValidationRule('isNegative', target, String(propertyKey));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function Min(num: number): PropertyDecorator {
|
|
91
|
+
return (target, propertyKey) => registerValidationRule('min', target, String(propertyKey), [num]);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function Max(num: number): PropertyDecorator {
|
|
95
|
+
return (target, propertyKey) => registerValidationRule('max', target, String(propertyKey), [num]);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 4. String Constraints
|
|
99
|
+
export function Contains(str: string): PropertyDecorator {
|
|
100
|
+
return (target, propertyKey) => registerValidationRule('contains', target, String(propertyKey), [str]);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function NotContains(str: string): PropertyDecorator {
|
|
104
|
+
return (target, propertyKey) => registerValidationRule('notContains', target, String(propertyKey), [str]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function IsAlpha(): PropertyDecorator {
|
|
108
|
+
return (target, propertyKey) => registerValidationRule('isAlpha', target, String(propertyKey));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function IsAlphanumeric(): PropertyDecorator {
|
|
112
|
+
return (target, propertyKey) => registerValidationRule('isAlphanumeric', target, String(propertyKey));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function IsDecimal(): PropertyDecorator {
|
|
116
|
+
return (target, propertyKey) => registerValidationRule('isDecimal', target, String(propertyKey));
|
|
25
117
|
}
|
|
26
118
|
|
|
27
119
|
export function IsEmail(): PropertyDecorator {
|
|
28
120
|
return (target, propertyKey) => registerValidationRule('email', target, String(propertyKey));
|
|
29
121
|
}
|
|
30
122
|
|
|
123
|
+
export function IsUrl(): PropertyDecorator {
|
|
124
|
+
return (target, propertyKey) => registerValidationRule('isUrl', target, String(propertyKey));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function IsIP(): PropertyDecorator {
|
|
128
|
+
return (target, propertyKey) => registerValidationRule('isIP', target, String(propertyKey));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function IsPort(): PropertyDecorator {
|
|
132
|
+
return (target, propertyKey) => registerValidationRule('isPort', target, String(propertyKey));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function IsJSON(): PropertyDecorator {
|
|
136
|
+
return (target, propertyKey) => registerValidationRule('isJSON', target, String(propertyKey));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function IsLowercase(): PropertyDecorator {
|
|
140
|
+
return (target, propertyKey) => registerValidationRule('isLowercase', target, String(propertyKey));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function IsUppercase(): PropertyDecorator {
|
|
144
|
+
return (target, propertyKey) => registerValidationRule('isUppercase', target, String(propertyKey));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function IsNumberString(): PropertyDecorator {
|
|
148
|
+
return (target, propertyKey) => registerValidationRule('isNumberString', target, String(propertyKey));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function IsUUID(): PropertyDecorator {
|
|
152
|
+
return (target, propertyKey) => registerValidationRule('isUUID', target, String(propertyKey));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function IsDateString(): PropertyDecorator {
|
|
156
|
+
return (target, propertyKey) => registerValidationRule('isDateString', target, String(propertyKey));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function Length(min: number, max: number): PropertyDecorator {
|
|
160
|
+
return (target, propertyKey) => registerValidationRule('length', target, String(propertyKey), [min, max]);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function MinLength(min: number): PropertyDecorator {
|
|
164
|
+
return (target, propertyKey) => registerValidationRule('minLength', target, String(propertyKey), [min]);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function MaxLength(max: number): PropertyDecorator {
|
|
168
|
+
return (target, propertyKey) => registerValidationRule('maxLength', target, String(propertyKey), [max]);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function Matches(regex: RegExp): PropertyDecorator {
|
|
172
|
+
return (target, propertyKey) => registerValidationRule('matches', target, String(propertyKey), [regex]);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// 5. Array Constraints
|
|
176
|
+
export function ArrayNotEmpty(): PropertyDecorator {
|
|
177
|
+
return (target, propertyKey) => registerValidationRule('arrayNotEmpty', target, String(propertyKey));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function ArrayMinSize(num: number): PropertyDecorator {
|
|
181
|
+
return (target, propertyKey) => registerValidationRule('arrayMinSize', target, String(propertyKey), [num]);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function ArrayMaxSize(num: number): PropertyDecorator {
|
|
185
|
+
return (target, propertyKey) => registerValidationRule('arrayMaxSize', target, String(propertyKey), [num]);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function ArrayUnique(): PropertyDecorator {
|
|
189
|
+
return (target, propertyKey) => registerValidationRule('arrayUnique', target, String(propertyKey));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Class-Transformer Aliases/Overrides
|
|
31
193
|
export function Expose(): PropertyDecorator {
|
|
32
194
|
return (target, propertyKey) => {
|
|
33
195
|
const constructor = target.constructor;
|
package/src/websockets/index.ts
CHANGED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { describe, test, expect } from 'bun:test';
|
|
2
|
+
import {
|
|
3
|
+
Module,
|
|
4
|
+
Injectable,
|
|
5
|
+
Inject,
|
|
6
|
+
forwardRef,
|
|
7
|
+
CalyxContainer,
|
|
8
|
+
Scope,
|
|
9
|
+
REQUEST,
|
|
10
|
+
} from '../src/index.ts';
|
|
11
|
+
|
|
12
|
+
describe('Circular DI and Recursive Dynamic Modules', () => {
|
|
13
|
+
test('should resolve circular dependencies with property-based injection', () => {
|
|
14
|
+
@Injectable()
|
|
15
|
+
class ClassA {
|
|
16
|
+
@Inject(forwardRef(() => ClassB))
|
|
17
|
+
public b: any;
|
|
18
|
+
|
|
19
|
+
getName() {
|
|
20
|
+
return 'A';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@Injectable()
|
|
25
|
+
class ClassB {
|
|
26
|
+
@Inject(forwardRef(() => ClassA))
|
|
27
|
+
public a: any;
|
|
28
|
+
|
|
29
|
+
getName() {
|
|
30
|
+
return 'B';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@Module({
|
|
35
|
+
providers: [ClassA, ClassB],
|
|
36
|
+
})
|
|
37
|
+
class RootModule {}
|
|
38
|
+
|
|
39
|
+
const container = new CalyxContainer();
|
|
40
|
+
container.bootstrap(RootModule);
|
|
41
|
+
|
|
42
|
+
const a = container.getGlobalOrAnyInstance(ClassA);
|
|
43
|
+
const b = container.getGlobalOrAnyInstance(ClassB);
|
|
44
|
+
|
|
45
|
+
expect(a.b.getName()).toBe('B');
|
|
46
|
+
expect(b.a.getName()).toBe('A');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('should resolve recursive dynamic module imports', () => {
|
|
50
|
+
@Injectable()
|
|
51
|
+
class DatabaseConfig {
|
|
52
|
+
getUri() {
|
|
53
|
+
return 'mongodb://localhost';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@Module({
|
|
58
|
+
providers: [DatabaseConfig],
|
|
59
|
+
exports: [DatabaseConfig],
|
|
60
|
+
})
|
|
61
|
+
class ConfigModule {}
|
|
62
|
+
|
|
63
|
+
@Module({})
|
|
64
|
+
class DatabaseModule {
|
|
65
|
+
static forRoot() {
|
|
66
|
+
return {
|
|
67
|
+
module: DatabaseModule,
|
|
68
|
+
imports: [ConfigModule],
|
|
69
|
+
providers: [
|
|
70
|
+
{
|
|
71
|
+
provide: 'DATABASE_URI',
|
|
72
|
+
useFactory: (cfg: DatabaseConfig) => cfg.getUri(),
|
|
73
|
+
inject: [DatabaseConfig],
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
exports: ['DATABASE_URI'],
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
@Injectable()
|
|
82
|
+
class AppService {
|
|
83
|
+
constructor(@Inject('DATABASE_URI') public uri: string) {}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@Module({
|
|
87
|
+
imports: [DatabaseModule.forRoot()],
|
|
88
|
+
providers: [AppService],
|
|
89
|
+
})
|
|
90
|
+
class AppModule {}
|
|
91
|
+
|
|
92
|
+
const container = new CalyxContainer();
|
|
93
|
+
container.bootstrap(AppModule);
|
|
94
|
+
|
|
95
|
+
const appService = container.getGlobalOrAnyInstance(AppService);
|
|
96
|
+
expect(appService.uri).toBe('mongodb://localhost');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('should support request-scoped circular dependency JIT DI compilation', async () => {
|
|
100
|
+
@Injectable({ scope: Scope.REQUEST })
|
|
101
|
+
class ReqA {
|
|
102
|
+
constructor(
|
|
103
|
+
@Inject(REQUEST) public req: any,
|
|
104
|
+
@Inject(forwardRef(() => ReqB)) public b: any
|
|
105
|
+
) {}
|
|
106
|
+
|
|
107
|
+
sayHi() {
|
|
108
|
+
return 'A';
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@Injectable({ scope: Scope.REQUEST })
|
|
113
|
+
class ReqB {
|
|
114
|
+
constructor(
|
|
115
|
+
@Inject(REQUEST) public req: any,
|
|
116
|
+
@Inject(forwardRef(() => ReqA)) public a: any
|
|
117
|
+
) {}
|
|
118
|
+
|
|
119
|
+
sayHi() {
|
|
120
|
+
return 'B';
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// A controller is needed because JIT compiling factories runs for controllers
|
|
125
|
+
@Injectable()
|
|
126
|
+
class ReqController {
|
|
127
|
+
constructor(
|
|
128
|
+
public a: ReqA,
|
|
129
|
+
public b: ReqB
|
|
130
|
+
) {}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
@Module({
|
|
134
|
+
providers: [ReqA, ReqB],
|
|
135
|
+
controllers: [ReqController],
|
|
136
|
+
})
|
|
137
|
+
class RootModule {}
|
|
138
|
+
|
|
139
|
+
const container = new CalyxContainer();
|
|
140
|
+
container.bootstrap(RootModule);
|
|
141
|
+
|
|
142
|
+
// Get the JIT factory or resolve in a request context
|
|
143
|
+
const reqContext = new Map<any, any>();
|
|
144
|
+
reqContext.set(REQUEST, { url: '/test' });
|
|
145
|
+
|
|
146
|
+
const ctrl = container.resolveControllerInRequestContext(RootModule, ReqController, reqContext);
|
|
147
|
+
expect(ctrl.a.req).toEqual({ url: '/test' });
|
|
148
|
+
expect(ctrl.a.b.sayHi()).toBe('B');
|
|
149
|
+
expect(ctrl.b.a.sayHi()).toBe('A');
|
|
150
|
+
});
|
|
151
|
+
});
|
package/tests/di.test.ts
CHANGED
|
@@ -167,15 +167,17 @@ describe('Dependency Injection System', () => {
|
|
|
167
167
|
expect(() => container.bootstrap(RootModule)).toThrow(/Cannot resolve dependency/);
|
|
168
168
|
});
|
|
169
169
|
|
|
170
|
-
test('should
|
|
170
|
+
test('should resolve circular dependencies using lazy proxies', () => {
|
|
171
171
|
@Injectable()
|
|
172
172
|
class ServiceA {
|
|
173
173
|
constructor(@Inject(forwardRef(() => ServiceB)) public b: any) {}
|
|
174
|
+
getValue() { return 'A'; }
|
|
174
175
|
}
|
|
175
176
|
|
|
176
177
|
@Injectable()
|
|
177
178
|
class ServiceB {
|
|
178
179
|
constructor(@Inject(forwardRef(() => ServiceA)) public a: any) {}
|
|
180
|
+
getValue() { return 'B'; }
|
|
179
181
|
}
|
|
180
182
|
|
|
181
183
|
@Module({
|
|
@@ -184,7 +186,13 @@ describe('Dependency Injection System', () => {
|
|
|
184
186
|
class RootModule {}
|
|
185
187
|
|
|
186
188
|
const container = new CalyxContainer();
|
|
187
|
-
|
|
189
|
+
container.bootstrap(RootModule);
|
|
190
|
+
|
|
191
|
+
const a = container.getGlobalOrAnyInstance(ServiceA);
|
|
192
|
+
const b = container.getGlobalOrAnyInstance(ServiceB);
|
|
193
|
+
|
|
194
|
+
expect(a.b.getValue()).toBe('B');
|
|
195
|
+
expect(b.a.getValue()).toBe('A');
|
|
188
196
|
});
|
|
189
197
|
|
|
190
198
|
test('should isolate module scopes and resolve exported providers', () => {
|