@tsed/ajv 8.25.5 → 8.26.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/fn/validate.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { inject } from "@tsed/di";
|
|
2
2
|
import { AjvService } from "../services/AjvService.js";
|
|
3
3
|
/**
|
|
4
|
-
* Validate a value against a JSON schema or a type
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Validate a value against a JSON schema or a type using the configured `AjvService`.
|
|
5
|
+
*
|
|
6
|
+
* The return type is inferred from `value` by default and can be overridden with an explicit generic:
|
|
7
|
+
* `validate<MyType>(value, options)`.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam TReturn Explicit return type override. When omitted, `TValue` is returned.
|
|
10
|
+
* @typeParam TValue Input value type.
|
|
11
|
+
* @param value Value to validate.
|
|
12
|
+
* @param options Validation options, a JSON schema, or a target type descriptor.
|
|
13
|
+
* @returns The validated value. If `returnsCoercedValues` is enabled, it may contain coerced values.
|
|
14
|
+
* @throws {AjvValidationError} When validation fails.
|
|
7
15
|
*/
|
|
8
16
|
export function validate(value, options) {
|
|
9
17
|
return inject(AjvService).validate(value, options);
|
|
@@ -13,6 +13,21 @@ export class AjvService {
|
|
|
13
13
|
this.returnsCoercedValues = constant("ajv.returnsCoercedValues");
|
|
14
14
|
this.ajv = inject(Ajv);
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Validate a value against a JSON schema, a model type, or explicit AJV options.
|
|
18
|
+
*
|
|
19
|
+
* The return type is inferred from `value` by default and can be overridden with an explicit generic:
|
|
20
|
+
* `ajvService.validate<MyType>(value, options)`.
|
|
21
|
+
*
|
|
22
|
+
* If `returnsCoercedValues` is enabled globally or in `options`, coercions performed by AJV are returned.
|
|
23
|
+
*
|
|
24
|
+
* @typeParam TReturn Explicit return type override. When omitted, `TValue` is returned.
|
|
25
|
+
* @typeParam TValue Input value type.
|
|
26
|
+
* @param value Value to validate.
|
|
27
|
+
* @param options Validation options or `JsonSchema`.
|
|
28
|
+
* @returns The validated value (possibly coerced when configured).
|
|
29
|
+
* @throws {AjvValidationError} When validation fails.
|
|
30
|
+
*/
|
|
16
31
|
async validate(value, options) {
|
|
17
32
|
let { schema: defaultSchema, type, collectionType, returnsCoercedValues = this.returnsCoercedValues, ...additionalOptions } = this.mapOptions(options);
|
|
18
33
|
const schema = defaultSchema || getJsonSchema(type, { ...additionalOptions, customKeys: true });
|
|
@@ -66,7 +81,7 @@ export class AjvService {
|
|
|
66
81
|
error.modelName = nameOf(type);
|
|
67
82
|
error.message = this.mapClassError(error, type);
|
|
68
83
|
}
|
|
69
|
-
return this.errorFormatter
|
|
84
|
+
return this.errorFormatter(error);
|
|
70
85
|
})
|
|
71
86
|
.join("\n");
|
|
72
87
|
return new AjvValidationError(message, errors);
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { JsonSchema } from "@tsed/schema";
|
|
2
2
|
import { type AjvValidateOptions } from "../services/AjvService.js";
|
|
3
3
|
/**
|
|
4
|
-
* Validate a value against a JSON schema or a type
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Validate a value against a JSON schema or a type using the configured `AjvService`.
|
|
5
|
+
*
|
|
6
|
+
* The return type is inferred from `value` by default and can be overridden with an explicit generic:
|
|
7
|
+
* `validate<MyType>(value, options)`.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam TReturn Explicit return type override. When omitted, `TValue` is returned.
|
|
10
|
+
* @typeParam TValue Input value type.
|
|
11
|
+
* @param value Value to validate.
|
|
12
|
+
* @param options Validation options, a JSON schema, or a target type descriptor.
|
|
13
|
+
* @returns The validated value. If `returnsCoercedValues` is enabled, it may contain coerced values.
|
|
14
|
+
* @throws {AjvValidationError} When validation fails.
|
|
7
15
|
*/
|
|
8
|
-
export declare function validate(value:
|
|
16
|
+
export declare function validate<TReturn = never, TValue = unknown>(value: TValue, options: AjvValidateOptions | JsonSchema): Promise<[TReturn] extends [never] ? TValue : TReturn>;
|
|
@@ -15,7 +15,22 @@ export declare class AjvService {
|
|
|
15
15
|
protected errorFormatter: ErrorFormatter;
|
|
16
16
|
protected returnsCoercedValues: boolean | undefined;
|
|
17
17
|
protected ajv: Ajv;
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Validate a value against a JSON schema, a model type, or explicit AJV options.
|
|
20
|
+
*
|
|
21
|
+
* The return type is inferred from `value` by default and can be overridden with an explicit generic:
|
|
22
|
+
* `ajvService.validate<MyType>(value, options)`.
|
|
23
|
+
*
|
|
24
|
+
* If `returnsCoercedValues` is enabled globally or in `options`, coercions performed by AJV are returned.
|
|
25
|
+
*
|
|
26
|
+
* @typeParam TReturn Explicit return type override. When omitted, `TValue` is returned.
|
|
27
|
+
* @typeParam TValue Input value type.
|
|
28
|
+
* @param value Value to validate.
|
|
29
|
+
* @param options Validation options or `JsonSchema`.
|
|
30
|
+
* @returns The validated value (possibly coerced when configured).
|
|
31
|
+
* @throws {AjvValidationError} When validation fails.
|
|
32
|
+
*/
|
|
33
|
+
validate<TReturn = never, TValue = unknown>(value: TValue, options: AjvValidateOptions | JsonSchema): Promise<[TReturn] extends [never] ? TValue : TReturn>;
|
|
19
34
|
protected mapOptions(options: AjvValidateOptions | JsonSchema): AjvValidateOptions;
|
|
20
35
|
protected mapErrors(errors: ErrorObject[], options: any): AjvValidationError;
|
|
21
36
|
protected mapClassError(error: AjvErrorObject, targetType: Type<any>): string | undefined;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/ajv",
|
|
3
3
|
"description": "AJV package for Ts.ED framework",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.
|
|
5
|
+
"version": "8.26.1",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -26,19 +26,19 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"ajv-errors": "3.0.0",
|
|
28
28
|
"ajv-formats": "3.0.1",
|
|
29
|
-
"tslib": "2.7.0"
|
|
29
|
+
"tslib": ">=2.7.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@tsed/barrels": "8.
|
|
33
|
-
"@tsed/core": "8.
|
|
34
|
-
"@tsed/di": "8.
|
|
35
|
-
"@tsed/exceptions": "8.
|
|
36
|
-
"@tsed/schema": "8.
|
|
37
|
-
"@tsed/typescript": "8.
|
|
32
|
+
"@tsed/barrels": "8.26.1",
|
|
33
|
+
"@tsed/core": "8.26.1",
|
|
34
|
+
"@tsed/di": "8.26.1",
|
|
35
|
+
"@tsed/exceptions": "8.26.1",
|
|
36
|
+
"@tsed/schema": "8.26.1",
|
|
37
|
+
"@tsed/typescript": "8.26.1",
|
|
38
38
|
"ajv": "^8.17.1",
|
|
39
39
|
"eslint": "9.12.0",
|
|
40
|
-
"typescript": "
|
|
41
|
-
"vite": "7.
|
|
40
|
+
"typescript": "6.0.2",
|
|
41
|
+
"vite": "7.3.2",
|
|
42
42
|
"vitest": "3.2.4"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|