@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.
@@ -1,2 +1 @@
1
- import "@tsed/ajv";
2
1
  export declare function afterResolveConfiguration(): Promise<void>;
@@ -1,2 +1 @@
1
- import { JsonSchema } from "@tsed/schema";
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.2",
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/ajv": "8.16.0-rc.2",
65
- "@tsed/barrels": "8.16.0-rc.2",
66
- "@tsed/core": "8.16.0-rc.2",
67
- "@tsed/di": "8.16.0-rc.2",
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/ajv";
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
- data = await validate(name!, data, validationSchema);
27
+ const {validate} = await import("../utils/validate.js");
28
+ data = validate(name!, data, validationSchema);
30
29
  }
31
30
 
32
31
  configuration().set(data);
@@ -1,18 +1,35 @@
1
- import {AjvService} from "@tsed/ajv";
2
- import {inject} from "@tsed/di";
3
- import {JsonSchema} from "@tsed/schema";
4
-
5
- export async function validate(configName: string, config: Record<string, unknown>, validationSchema: JsonSchema) {
6
- const ajv = inject(AjvService);
7
-
8
- try {
9
- return await ajv.validate(config, {
10
- returnsCoercedValues: true,
11
- schema: validationSchema.toJSON()
12
- });
13
- } catch (er) {
14
- er.message = er.message.replace("Value.", `extends[${configName}].`);
15
-
16
- throw er;
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
  }