@tsed/config 8.16.0-rc.2 → 8.16.0-rc.4
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/hooks/afterResolveConfiguration.js +2 -3
- package/lib/esm/utils/validate.js +23 -12
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/hooks/afterResolveConfiguration.d.ts +0 -1
- package/lib/types/utils/validate.d.ts +1 -2
- package/package.json +6 -6
- package/src/hooks/afterResolveConfiguration.ts +3 -4
- package/src/utils/validate.ts +33 -16
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function validate(configName: string, config: Record<string, unknown>, validationSchema: JsonSchema): Promise<any>;
|
|
1
|
+
export declare function validate(configName: string, config: Record<string, unknown>, validationSchema: any): Record<string, unknown>;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/config",
|
|
3
3
|
"description": "Configuration management for Ts.ED",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.16.0-rc.
|
|
5
|
+
"version": "8.16.0-rc.4",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"Ts.ED",
|
|
8
8
|
"Configuration",
|
|
@@ -57,15 +57,15 @@
|
|
|
57
57
|
"test:ci": "vitest run --coverage.thresholds.autoUpdate=true"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
+
"ajv": "^8.17.1",
|
|
60
61
|
"change-case": "5.4.4",
|
|
61
62
|
"tslib": "2.7.0"
|
|
62
63
|
},
|
|
63
64
|
"devDependencies": {
|
|
64
|
-
"@tsed/
|
|
65
|
-
"@tsed/
|
|
66
|
-
"@tsed/
|
|
67
|
-
"@tsed/
|
|
68
|
-
"@tsed/typescript": "8.16.0-rc.2",
|
|
65
|
+
"@tsed/barrels": "8.16.0-rc.4",
|
|
66
|
+
"@tsed/core": "8.16.0-rc.4",
|
|
67
|
+
"@tsed/di": "8.16.0-rc.4",
|
|
68
|
+
"@tsed/typescript": "8.16.0-rc.4",
|
|
69
69
|
"@types/js-yaml": "4.0.9",
|
|
70
70
|
"dotenv": "^16.5.0",
|
|
71
71
|
"dotenv-expand": "^12.0.2",
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import "@tsed/
|
|
2
|
-
|
|
3
|
-
import {configuration, constant, inject, injectable, logger} from "@tsed/di";
|
|
1
|
+
import {configuration, constant, injectable, logger} from "@tsed/di";
|
|
4
2
|
import {$off, $on, $once} from "@tsed/hooks";
|
|
5
3
|
|
|
6
4
|
import {CONFIG_SOURCES} from "../constants/constants.js";
|
|
@@ -26,7 +24,8 @@ export async function afterResolveConfiguration() {
|
|
|
26
24
|
let data = await getAll.apply(instance);
|
|
27
25
|
|
|
28
26
|
if (validationSchema) {
|
|
29
|
-
|
|
27
|
+
const {validate} = await import("../utils/validate.js");
|
|
28
|
+
data = validate(name!, data, validationSchema);
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
configuration().set(data);
|
package/src/utils/validate.ts
CHANGED
|
@@ -1,18 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import {Ajv, type ErrorObject} from "ajv";
|
|
2
|
+
|
|
3
|
+
const ajv = new Ajv({
|
|
4
|
+
verbose: false,
|
|
5
|
+
coerceTypes: true,
|
|
6
|
+
strict: false,
|
|
7
|
+
discriminator: true,
|
|
8
|
+
allErrors: true
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
class ValidationError extends Error {
|
|
12
|
+
private errors: ErrorObject[];
|
|
13
|
+
|
|
14
|
+
constructor(message: string, errors: ErrorObject[]) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "VALIDATION_ERROR";
|
|
17
|
+
this.errors = errors;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function validate(configName: string, config: Record<string, unknown>, validationSchema: any) {
|
|
22
|
+
const schema = "toJSON" in validationSchema ? validationSchema.toJSON() : validationSchema;
|
|
23
|
+
|
|
24
|
+
const validate = ajv.compile(schema);
|
|
25
|
+
const result = validate(config);
|
|
26
|
+
|
|
27
|
+
if (!result && validate.errors) {
|
|
28
|
+
const firstError = validate.errors[0];
|
|
29
|
+
const message = `extends[${configName}].${firstError?.instancePath?.substring(1) || ""} ${firstError?.message}.`;
|
|
30
|
+
|
|
31
|
+
throw new ValidationError(message, validate.errors);
|
|
17
32
|
}
|
|
33
|
+
|
|
34
|
+
return config;
|
|
18
35
|
}
|