@tsed/config 8.16.0-rc.1 → 8.16.0-rc.3
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,9 +1,7 @@
|
|
|
1
|
-
import "@tsed/ajv";
|
|
2
1
|
import { configuration, constant, injectable, logger } from "@tsed/di";
|
|
3
2
|
import { $off, $on, $once } from "@tsed/hooks";
|
|
4
3
|
import { CONFIG_SOURCES } from "../constants/constants.js";
|
|
5
4
|
import { getConfigSources } from "../utils/getConfigSources.js";
|
|
6
|
-
import { validate } from "../utils/validate.js";
|
|
7
5
|
export async function afterResolveConfiguration() {
|
|
8
6
|
const sources = getConfigSources(constant("extends", []));
|
|
9
7
|
for (const source of sources.values()) {
|
|
@@ -17,7 +15,8 @@ export async function afterResolveConfiguration() {
|
|
|
17
15
|
const refresh = async () => {
|
|
18
16
|
let data = await getAll.apply(instance);
|
|
19
17
|
if (validationSchema) {
|
|
20
|
-
|
|
18
|
+
const { validate } = await import("../utils/validate.js");
|
|
19
|
+
data = validate(name, data, validationSchema);
|
|
21
20
|
}
|
|
22
21
|
configuration().set(data);
|
|
23
22
|
configuration().set(`configs.${name}`, data);
|
|
@@ -1,15 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { Ajv } from "ajv";
|
|
2
|
+
const ajv = new Ajv({
|
|
3
|
+
verbose: false,
|
|
4
|
+
coerceTypes: true,
|
|
5
|
+
strict: false,
|
|
6
|
+
discriminator: true,
|
|
7
|
+
allErrors: true
|
|
8
|
+
});
|
|
9
|
+
class ValidationError extends Error {
|
|
10
|
+
constructor(message, errors) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "VALIDATION_ERROR";
|
|
13
|
+
this.errors = errors;
|
|
10
14
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
}
|
|
16
|
+
export function validate(configName, config, validationSchema) {
|
|
17
|
+
const schema = "toJSON" in validationSchema ? validationSchema.toJSON() : validationSchema;
|
|
18
|
+
const validate = ajv.compile(schema);
|
|
19
|
+
const result = validate(config);
|
|
20
|
+
if (!result && validate.errors) {
|
|
21
|
+
const firstError = validate.errors[0];
|
|
22
|
+
const message = `extends[${configName}].${firstError?.instancePath?.substring(1) || ""} ${firstError?.message}.`;
|
|
23
|
+
throw new ValidationError(message, validate.errors);
|
|
14
24
|
}
|
|
25
|
+
return config;
|
|
15
26
|
}
|