@tsed/ajv 7.0.0-alpha.2 → 7.0.0-beta.11
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/cjs/decorators/formats.js +22 -0
- package/lib/cjs/decorators/formats.js.map +1 -0
- package/lib/cjs/decorators/keyword.js +21 -0
- package/lib/cjs/decorators/keyword.js.map +1 -0
- package/lib/cjs/errors/AjvValidationError.js +13 -0
- package/lib/cjs/errors/AjvValidationError.js.map +1 -0
- package/lib/cjs/index.js +18 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/interfaces/FormatsMethods.js +3 -0
- package/lib/cjs/interfaces/FormatsMethods.js.map +1 -0
- package/lib/cjs/interfaces/IAjvSettings.js +3 -0
- package/lib/cjs/interfaces/IAjvSettings.js.map +1 -0
- package/lib/cjs/interfaces/KeywordMethods.js +3 -0
- package/lib/cjs/interfaces/KeywordMethods.js.map +1 -0
- package/lib/cjs/interfaces/interfaces.js +3 -0
- package/lib/cjs/interfaces/interfaces.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/services/Ajv.js +69 -0
- package/lib/cjs/services/Ajv.js.map +1 -0
- package/lib/cjs/services/AjvService.js +93 -0
- package/lib/cjs/services/AjvService.js.map +1 -0
- package/lib/cjs/utils/defaultErrorFormatter.js +24 -0
- package/lib/cjs/utils/defaultErrorFormatter.js.map +1 -0
- package/lib/cjs/utils/getPath.js +9 -0
- package/lib/cjs/utils/getPath.js.map +1 -0
- package/lib/esm/decorators/formats.js +18 -0
- package/lib/esm/decorators/formats.js.map +1 -0
- package/lib/esm/decorators/keyword.js +17 -0
- package/lib/esm/decorators/keyword.js.map +1 -0
- package/lib/esm/errors/AjvValidationError.js +9 -0
- package/lib/esm/errors/AjvValidationError.js.map +1 -0
- package/lib/esm/index.js +15 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/FormatsMethods.js +2 -0
- package/lib/esm/interfaces/FormatsMethods.js.map +1 -0
- package/lib/esm/interfaces/IAjvSettings.js +2 -0
- package/lib/esm/interfaces/IAjvSettings.js.map +1 -0
- package/lib/esm/interfaces/KeywordMethods.js +2 -0
- package/lib/esm/interfaces/KeywordMethods.js.map +1 -0
- package/lib/esm/interfaces/interfaces.js +2 -0
- package/lib/esm/interfaces/interfaces.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/services/Ajv.js +66 -0
- package/lib/esm/services/Ajv.js.map +1 -0
- package/lib/esm/services/AjvService.js +90 -0
- package/lib/esm/services/AjvService.js.map +1 -0
- package/lib/esm/utils/defaultErrorFormatter.js +20 -0
- package/lib/esm/utils/defaultErrorFormatter.js.map +1 -0
- package/lib/esm/utils/getPath.js +5 -0
- package/lib/esm/utils/getPath.js.map +1 -0
- package/lib/{decorators → types/decorators}/formats.d.ts +0 -0
- package/lib/{decorators → types/decorators}/keyword.d.ts +0 -0
- package/lib/{errors → types/errors}/AjvValidationError.d.ts +0 -0
- package/lib/{index.d.ts → types/index.d.ts} +12 -13
- package/lib/{interfaces → types/interfaces}/FormatsMethods.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/IAjvSettings.d.ts +0 -0
- package/lib/{interfaces → types/interfaces}/KeywordMethods.d.ts +0 -0
- package/lib/types/interfaces/interfaces.d.ts +8 -0
- package/lib/{services → types/services}/Ajv.d.ts +0 -0
- package/lib/{services → types/services}/AjvService.d.ts +0 -0
- package/lib/{utils → types/utils}/defaultErrorFormatter.d.ts +0 -0
- package/lib/{utils → types/utils}/getPath.d.ts +0 -0
- package/package.json +42 -22
- package/readme.md +30 -40
- package/tsconfig.compile.esm.json +12 -0
- package/lib/index.js +0 -302
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -295
- package/lib/index.modern.js.map +0 -1
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { deepClone, getValue, nameOf, prototypeOf, setValue } from "@tsed/core";
|
|
3
|
+
import { Constant, Inject, Injectable } from "@tsed/di";
|
|
4
|
+
import { getJsonSchema, JsonEntityStore, JsonSchema } from "@tsed/schema";
|
|
5
|
+
import Ajv from "ajv";
|
|
6
|
+
import { AjvValidationError } from "../errors/AjvValidationError.js";
|
|
7
|
+
import { defaultErrorFormatter } from "../utils/defaultErrorFormatter.js";
|
|
8
|
+
import "./Ajv.js";
|
|
9
|
+
import { getPath } from "../utils/getPath.js";
|
|
10
|
+
let AjvService = class AjvService {
|
|
11
|
+
async validate(value, options) {
|
|
12
|
+
let { schema: defaultSchema, type, collectionType, ...additionalOptions } = this.mapOptions(options);
|
|
13
|
+
const schema = defaultSchema || getJsonSchema(type, { ...additionalOptions, customKeys: true });
|
|
14
|
+
if (schema) {
|
|
15
|
+
const localValue = deepClone(value);
|
|
16
|
+
const validate = this.ajv.compile(schema);
|
|
17
|
+
const valid = await validate(localValue);
|
|
18
|
+
const { errors } = validate;
|
|
19
|
+
if (!valid && errors) {
|
|
20
|
+
throw this.mapErrors(errors, {
|
|
21
|
+
type,
|
|
22
|
+
collectionType,
|
|
23
|
+
async: true,
|
|
24
|
+
value: localValue
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
mapOptions(options) {
|
|
31
|
+
if (options instanceof JsonSchema) {
|
|
32
|
+
return {
|
|
33
|
+
schema: options.toJSON({ customKeys: true })
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return options;
|
|
37
|
+
}
|
|
38
|
+
mapErrors(errors, options) {
|
|
39
|
+
const { type, collectionType, value } = options;
|
|
40
|
+
const message = errors
|
|
41
|
+
.map((error) => {
|
|
42
|
+
if (collectionType) {
|
|
43
|
+
error.collectionName = nameOf(collectionType);
|
|
44
|
+
}
|
|
45
|
+
const dataPath = getPath(error);
|
|
46
|
+
if (!error.data) {
|
|
47
|
+
if (dataPath) {
|
|
48
|
+
error.data = getValue(value, dataPath.replace(/^\./, ""));
|
|
49
|
+
}
|
|
50
|
+
else if (error.schemaPath !== "#/required") {
|
|
51
|
+
error.data = value;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (dataPath && dataPath.match(/pwd|password|mdp|secret/)) {
|
|
55
|
+
error.data = "[REDACTED]";
|
|
56
|
+
}
|
|
57
|
+
if (type) {
|
|
58
|
+
error.modelName = nameOf(type);
|
|
59
|
+
error.message = this.mapClassError(error, type);
|
|
60
|
+
}
|
|
61
|
+
return this.errorFormatter.call(this, error, {});
|
|
62
|
+
})
|
|
63
|
+
.join("\n");
|
|
64
|
+
return new AjvValidationError(message, errors);
|
|
65
|
+
}
|
|
66
|
+
mapClassError(error, targetType) {
|
|
67
|
+
const propertyKey = getValue(error, "params.missingProperty");
|
|
68
|
+
if (propertyKey) {
|
|
69
|
+
const store = JsonEntityStore.from(prototypeOf(targetType), propertyKey);
|
|
70
|
+
if (store) {
|
|
71
|
+
setValue(error, "params.missingProperty", store.name || propertyKey);
|
|
72
|
+
return error.message.replace(`'${propertyKey}'`, `'${store.name || propertyKey}'`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return error.message;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
__decorate([
|
|
79
|
+
Constant("ajv.errorFormatter", defaultErrorFormatter),
|
|
80
|
+
__metadata("design:type", Function)
|
|
81
|
+
], AjvService.prototype, "errorFormatter", void 0);
|
|
82
|
+
__decorate([
|
|
83
|
+
Inject(),
|
|
84
|
+
__metadata("design:type", Ajv)
|
|
85
|
+
], AjvService.prototype, "ajv", void 0);
|
|
86
|
+
AjvService = __decorate([
|
|
87
|
+
Injectable()
|
|
88
|
+
], AjvService);
|
|
89
|
+
export { AjvService };
|
|
90
|
+
//# sourceMappingURL=AjvService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AjvService.js","sourceRoot":"","sources":["../../../src/services/AjvService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAE,QAAQ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EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;QACnD,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;QAEd,OAAO,IAAI,kBAAkB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACjD,CAAC;IAES,aAAa,CAAC,KAAqB,EAAE,UAAqB;QAClE,MAAM,WAAW,GAAG,QAAQ,CAAC,KAAK,EAAE,wBAAwB,CAAC,CAAC;QAE9D,IAAI,WAAW,EAAE;YACf,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,CAAkB,WAAW,CAAC,UAAU,CAAC,EAAE,WAAW,CAAC,CAAC;YAE1F,IAAI,KAAK,EAAE;gBACT,QAAQ,CAAC,KAAK,EAAE,wBAAwB,EAAE,KAAK,CAAC,IAAI,IAAI,WAAW,CAAC,CAAC;gBAErE,OAAO,KAAK,CAAC,OAAQ,CAAC,OAAO,CAAC,IAAI,WAAW,GAAG,EAAE,IAAI,KAAK,CAAC,IAAI,IAAI,WAAW,GAAG,CAAC,CAAC;aACrF;SACF;QAED,OAAO,KAAK,CAAC,OAAO,CAAC;IACvB,CAAC;CACF,CAAA;AA1FC;IADC,QAAQ,CAAC,oBAAoB,EAAE,qBAAqB,CAAC;;kDACb;AAGzC;IADC,MAAM,EAAE;8BACM,GAAG;uCAAC;AALR,UAAU;IADtB,UAAU,EAAE;GACA,UAAU,CA4FtB;SA5FY,UAAU","sourcesContent":["import {deepClone, getValue, nameOf, prototypeOf, setValue, Type} from \"@tsed/core\";\nimport {Constant, Inject, Injectable} from \"@tsed/di\";\nimport {getJsonSchema, JsonEntityStore, JsonSchema, JsonSchemaObject} from \"@tsed/schema\";\nimport Ajv, {ErrorObject} from \"ajv\";\nimport {AjvValidationError} from \"../errors/AjvValidationError\";\nimport {AjvErrorObject, ErrorFormatter} from \"../interfaces/IAjvSettings\";\nimport {defaultErrorFormatter} from \"../utils/defaultErrorFormatter\";\nimport \"./Ajv\";\nimport {getPath} from \"../utils/getPath\";\n\nexport interface AjvValidateOptions extends Record<string, any> {\n schema?: JsonSchema | Partial<JsonSchemaObject>;\n type?: Type<any> | any;\n collectionType?: Type<any> | any;\n}\n\n@Injectable()\nexport class AjvService {\n @Constant(\"ajv.errorFormatter\", defaultErrorFormatter)\n protected errorFormatter: ErrorFormatter;\n\n @Inject()\n protected ajv: Ajv;\n\n async validate(value: any, options: AjvValidateOptions | JsonSchema): Promise<any> {\n let {schema: defaultSchema, type, collectionType, ...additionalOptions} = this.mapOptions(options);\n\n const schema = defaultSchema || getJsonSchema(type, {...additionalOptions, customKeys: true});\n\n if (schema) {\n const localValue = deepClone(value);\n const validate = this.ajv.compile(schema);\n\n const valid = await validate(localValue);\n const {errors} = validate;\n\n if (!valid && errors) {\n throw this.mapErrors(errors, {\n type,\n collectionType,\n async: true,\n value: localValue\n });\n }\n }\n\n return value;\n }\n\n protected mapOptions(options: AjvValidateOptions | JsonSchema): AjvValidateOptions {\n if (options instanceof JsonSchema) {\n return {\n schema: options.toJSON({customKeys: true})\n };\n }\n\n return options;\n }\n\n protected mapErrors(errors: ErrorObject[], options: any) {\n const {type, collectionType, value} = options;\n\n const message = errors\n .map((error: AjvErrorObject) => {\n if (collectionType) {\n error.collectionName = nameOf(collectionType);\n }\n\n const dataPath = getPath(error);\n\n if (!error.data) {\n if (dataPath) {\n error.data = getValue(value, dataPath.replace(/^\\./, \"\"));\n } else if (error.schemaPath !== \"#/required\") {\n error.data = value;\n }\n }\n\n if (dataPath && dataPath.match(/pwd|password|mdp|secret/)) {\n error.data = \"[REDACTED]\";\n }\n\n if (type) {\n error.modelName = nameOf(type);\n error.message = this.mapClassError(error, type);\n }\n\n return this.errorFormatter.call(this, error, {});\n })\n .join(\"\\n\");\n\n return new AjvValidationError(message, errors);\n }\n\n protected mapClassError(error: AjvErrorObject, targetType: Type<any>) {\n const propertyKey = getValue(error, \"params.missingProperty\");\n\n if (propertyKey) {\n const store = JsonEntityStore.from<JsonEntityStore>(prototypeOf(targetType), propertyKey);\n\n if (store) {\n setValue(error, \"params.missingProperty\", store.name || propertyKey);\n\n return error.message!.replace(`'${propertyKey}'`, `'${store.name || propertyKey}'`);\n }\n }\n\n return error.message;\n }\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { getPath } from "./getPath.js";
|
|
2
|
+
export function defaultErrorFormatter(error) {
|
|
3
|
+
const value = JSON.stringify(error.data === undefined ? "undefined" : error.data);
|
|
4
|
+
const join = (list) => list.filter(Boolean).join("").trim();
|
|
5
|
+
error.dataPath = getPath(error);
|
|
6
|
+
const [, indexPath, ...paths] = error.dataPath.split(".");
|
|
7
|
+
const deepPaths = paths.length ? "." + paths.join(".") : "";
|
|
8
|
+
if (error.collectionName) {
|
|
9
|
+
switch (error.collectionName) {
|
|
10
|
+
case "Array":
|
|
11
|
+
return join([`${error.modelName || ""}[${indexPath}]${deepPaths}`, ` ${error.message}. Given value: ${value}`]);
|
|
12
|
+
case "Map":
|
|
13
|
+
return join([`Map<${indexPath}, ${error.modelName || ""}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);
|
|
14
|
+
case "Set":
|
|
15
|
+
return join([`Set<${indexPath}, ${error.modelName || ""}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return join([!error.modelName && "Value", `${error.modelName || ""}`, error.dataPath, ` ${error.message}. Given value: ${value}`]);
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=defaultErrorFormatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaultErrorFormatter.js","sourceRoot":"","sources":["../../../src/utils/defaultErrorFormatter.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAElC,MAAM,UAAU,qBAAqB,CAAC,KAA0C;IAC9E,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,WAAW,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AC,SAAS,IAAI,EAAE,EAAE,EAAE,KAAK,CAAC,QAAQ,EAAE,IAAI,KAAK,CAAC,OAAO,kBAAkB,KAAK,EAAE,CAAC,CAAC,CAAC;AACrI,CAAC","sourcesContent":["import {AjvErrorObject} from \"../interfaces/IAjvSettings\";\nimport {getPath} from \"./getPath\";\n\nexport function defaultErrorFormatter(error: AjvErrorObject & {dataPath: string}) {\n const value = JSON.stringify(error.data === undefined ? \"undefined\" : error.data);\n const join = (list: any[]): string => list.filter(Boolean).join(\"\").trim();\n\n error.dataPath = getPath(error);\n\n const [, indexPath, ...paths] = error.dataPath.split(\".\");\n const deepPaths = paths.length ? \".\" + paths.join(\".\") : \"\";\n\n if (error.collectionName) {\n switch (error.collectionName) {\n case \"Array\":\n return join([`${error.modelName || \"\"}[${indexPath}]${deepPaths}`, ` ${error.message}. Given value: ${value}`]);\n case \"Map\":\n return join([`Map<${indexPath}, ${error.modelName || \"\"}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);\n case \"Set\":\n return join([`Set<${indexPath}, ${error.modelName || \"\"}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);\n }\n }\n\n return join([!error.modelName && \"Value\", `${error.modelName || \"\"}`, error.dataPath, ` ${error.message}. Given value: ${value}`]);\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPath.js","sourceRoot":"","sources":["../../../src/utils/getPath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AAEpC,MAAM,UAAU,OAAO,CAAC,KAAU;IAChC,OAAO,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,KAAK,EAAE,cAAc,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC/F,CAAC","sourcesContent":["import {getValue} from \"@tsed/core\";\n\nexport function getPath(error: any) {\n return getValue(error, \"dataPath\", getValue(error, \"instancePath\", \"\")).replace(/\\//gi, \".\");\n}\n"]}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export * from "./decorators/keyword";
|
|
5
|
-
export * from "./interfaces/KeywordMethods";
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by barrelsby.
|
|
3
|
+
*/
|
|
6
4
|
export * from "./decorators/formats";
|
|
5
|
+
export * from "./decorators/keyword";
|
|
6
|
+
export * from "./errors/AjvValidationError";
|
|
7
7
|
export * from "./interfaces/FormatsMethods";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
8
|
+
export * from "./interfaces/IAjvSettings";
|
|
9
|
+
export * from "./interfaces/KeywordMethods";
|
|
10
|
+
export * from "./interfaces/interfaces";
|
|
11
|
+
export * from "./services/Ajv";
|
|
12
|
+
export * from "./services/AjvService";
|
|
13
|
+
export * from "./utils/defaultErrorFormatter";
|
|
14
|
+
export * from "./utils/getPath";
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,40 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/ajv",
|
|
3
|
-
"version": "7.0.0-
|
|
3
|
+
"version": "7.0.0-beta.11",
|
|
4
4
|
"description": "AJV package for Ts.ED framework",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
|
-
"main": "./lib/index.js",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
6
|
+
"main": "./lib/cjs/index.js",
|
|
7
|
+
"module": "./lib/esm/index.js",
|
|
8
|
+
"typings": "./lib/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"import": "./lib/esm/index.js",
|
|
12
|
+
"require": "./lib/cjs/index.js",
|
|
13
|
+
"default": "./lib/esm/index.js"
|
|
14
|
+
},
|
|
9
15
|
"scripts": {
|
|
10
|
-
"build": "
|
|
16
|
+
"build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
|
|
17
|
+
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
18
|
+
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
19
|
+
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\""
|
|
11
20
|
},
|
|
12
21
|
"dependencies": {
|
|
13
|
-
"ajv-formats": "1.
|
|
14
|
-
"tslib": "2.
|
|
22
|
+
"ajv-formats": "2.1.1",
|
|
23
|
+
"tslib": "2.3.1"
|
|
15
24
|
},
|
|
16
25
|
"devDependencies": {
|
|
17
|
-
"@tsed/core": "7.0.0-
|
|
18
|
-
"@tsed/di": "7.0.0-
|
|
19
|
-
"@tsed/exceptions": "7.0.0-
|
|
20
|
-
"@tsed/schema": "7.0.0-
|
|
21
|
-
"ajv": "8.
|
|
26
|
+
"@tsed/core": "7.0.0-beta.11",
|
|
27
|
+
"@tsed/di": "7.0.0-beta.11",
|
|
28
|
+
"@tsed/exceptions": "7.0.0-beta.11",
|
|
29
|
+
"@tsed/schema": "7.0.0-beta.11",
|
|
30
|
+
"ajv": "8.11.0"
|
|
22
31
|
},
|
|
23
32
|
"peerDependencies": {
|
|
24
|
-
"@tsed/core": "^7.0.0-
|
|
25
|
-
"@tsed/di": "^7.0.0-
|
|
26
|
-
"@tsed/exceptions": "^7.0.0-
|
|
27
|
-
"@tsed/schema": "^7.0.0-
|
|
28
|
-
"ajv": "
|
|
33
|
+
"@tsed/core": "^7.0.0-beta.11",
|
|
34
|
+
"@tsed/di": "^7.0.0-beta.11",
|
|
35
|
+
"@tsed/exceptions": "^7.0.0-beta.11",
|
|
36
|
+
"@tsed/schema": "^7.0.0-beta.11",
|
|
37
|
+
"ajv": ">=8.9.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependenciesMeta": {
|
|
40
|
+
"@tsed/core": {
|
|
41
|
+
"optional": false
|
|
42
|
+
},
|
|
43
|
+
"@tsed/di": {
|
|
44
|
+
"optional": false
|
|
45
|
+
},
|
|
46
|
+
"@tsed/exceptions": {
|
|
47
|
+
"optional": false
|
|
48
|
+
},
|
|
49
|
+
"@tsed/schema": {
|
|
50
|
+
"optional": false
|
|
51
|
+
}
|
|
29
52
|
},
|
|
30
53
|
"repository": "https://github.com/tsedio/tsed",
|
|
31
54
|
"bugs": {
|
|
32
55
|
"url": "https://github.com/tsedio/tsed/issues"
|
|
33
56
|
},
|
|
34
|
-
"homepage": "https://github.com/tsedio/tsed/tree/
|
|
57
|
+
"homepage": "https://github.com/tsedio/tsed/tree/beta/packages/specs/ajv",
|
|
35
58
|
"author": "Romain Lenzotti",
|
|
36
|
-
"license": "MIT"
|
|
37
|
-
"publishConfig": {
|
|
38
|
-
"tag": "alpha"
|
|
39
|
-
}
|
|
59
|
+
"license": "MIT"
|
|
40
60
|
}
|
package/readme.md
CHANGED
|
@@ -32,12 +32,8 @@ Then import `@tsed/ajv` in your Server:
|
|
|
32
32
|
import {Configuration} from "@tsed/common";
|
|
33
33
|
import "@tsed/ajv"; // import ajv ts.ed module
|
|
34
34
|
|
|
35
|
-
@Configuration({
|
|
36
|
-
|
|
37
|
-
})
|
|
38
|
-
export class Server {
|
|
39
|
-
|
|
40
|
-
}
|
|
35
|
+
@Configuration({})
|
|
36
|
+
export class Server {}
|
|
41
37
|
```
|
|
42
38
|
|
|
43
39
|
### Decorators
|
|
@@ -53,39 +49,37 @@ be validated by Ajv.
|
|
|
53
49
|
import {Required, MaxLength, MinLength, Minimum, Maximum, Format, Enum, Pattern, Email} from "@tsed/common";
|
|
54
50
|
|
|
55
51
|
export class CalendarModel {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
customInput: "value1" | "value2";
|
|
52
|
+
@MaxLength(20)
|
|
53
|
+
@MinLength(3)
|
|
54
|
+
@Required()
|
|
55
|
+
title: string;
|
|
56
|
+
|
|
57
|
+
@Minimum(0)
|
|
58
|
+
@Maximum(10)
|
|
59
|
+
rating: number;
|
|
60
|
+
|
|
61
|
+
@Email()
|
|
62
|
+
email: string;
|
|
63
|
+
|
|
64
|
+
@Format("date") // or date-time, etc...
|
|
65
|
+
createDate: Date;
|
|
66
|
+
|
|
67
|
+
@Pattern(/hello/)
|
|
68
|
+
customInput: string;
|
|
69
|
+
|
|
70
|
+
@Enum("value1", "value2")
|
|
71
|
+
customInput: "value1" | "value2";
|
|
77
72
|
}
|
|
78
73
|
```
|
|
79
74
|
|
|
80
75
|
> All validation decorators are compatible with the Swagger documentation.
|
|
81
76
|
|
|
82
|
-
|
|
83
77
|
## Error configuration
|
|
84
78
|
|
|
85
79
|
The AJV module allows a few settings to be added through the ServerSettings (all are optional):
|
|
86
80
|
|
|
87
|
-
|
|
88
|
-
|
|
81
|
+
- _options_, are AJV specific options passed directly to the AJV constructor,
|
|
82
|
+
- _errorFormatter_, can be used to alter the output produced by the @tsed/ajv package.
|
|
89
83
|
|
|
90
84
|
The error message could be changed like:
|
|
91
85
|
|
|
@@ -94,24 +88,20 @@ import {Configuration} from "@tsed/common";
|
|
|
94
88
|
import "@tsed/ajv"; // import ajv ts.ed module
|
|
95
89
|
|
|
96
90
|
@Configuration({
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
},
|
|
91
|
+
ajv: {
|
|
92
|
+
errorFormatter: (error) => `At ${error.modelName}${error.dataPath}, value '${error.data}' ${error.message}`,
|
|
93
|
+
options: {verbose: true}
|
|
94
|
+
}
|
|
102
95
|
})
|
|
103
|
-
export class Server {
|
|
104
|
-
|
|
105
|
-
}
|
|
96
|
+
export class Server {}
|
|
106
97
|
```
|
|
107
98
|
|
|
108
|
-
|
|
109
99
|
## Contributors
|
|
100
|
+
|
|
110
101
|
Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html)
|
|
111
102
|
|
|
112
103
|
<a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
113
104
|
|
|
114
|
-
|
|
115
105
|
## Backers
|
|
116
106
|
|
|
117
107
|
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.compile.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"module": "ES2020",
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"outDir": "./lib/esm",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationDir": "./lib/types"
|
|
10
|
+
},
|
|
11
|
+
"exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
|
|
12
|
+
}
|