badmfck-api-server 3.0.4 → 3.0.6
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/dist/apiServer/APIService.js +1 -1
- package/dist/apiServer/BaseEndpoint.js +1 -0
- package/dist/apiServer/helper/Validator.d.ts +2 -1
- package/dist/apiServer/helper/Validator.js +17 -2
- package/dist/apiServer/structures/DefaultErrors.d.ts +9 -0
- package/dist/apiServer/structures/DefaultErrors.js +10 -1
- package/package.json +1 -1
@@ -118,6 +118,7 @@ class BaseEndpoint {
|
|
118
118
|
const report = await Validator_1.Validator.validateStructure(structure, req.data);
|
119
119
|
if (report)
|
120
120
|
throw { ...DefaultErrors_1.default.BAD_REQUEST, details: report };
|
121
|
+
Validator_1.Validator.filterStructure(structure, req.data);
|
121
122
|
}
|
122
123
|
createStream(req) {
|
123
124
|
const res = req.response;
|
@@ -21,7 +21,8 @@ export declare enum ValidationReport {
|
|
21
21
|
}
|
22
22
|
export declare class Validator {
|
23
23
|
static validateObject(fields: string[], object: any): boolean;
|
24
|
-
static validateStructure(structure: any, object: any): Promise<string[] | undefined>;
|
24
|
+
static validateStructure(structure: any, object: any, level?: number): Promise<string[] | undefined>;
|
25
|
+
static filterStructure(structure: any, object: any): void;
|
25
26
|
static convertToType<T>(value: any, type: "string" | "number" | "boolean" | "date"): T | null;
|
26
27
|
static validateValue(value: string | number | boolean, opt?: IValidatorOptions): ValidationReport;
|
27
28
|
}
|
@@ -24,7 +24,7 @@ class Validator {
|
|
24
24
|
}
|
25
25
|
return true;
|
26
26
|
}
|
27
|
-
static async validateStructure(structure, object) {
|
27
|
+
static async validateStructure(structure, object, level = 0) {
|
28
28
|
if (!structure)
|
29
29
|
return;
|
30
30
|
if (!object)
|
@@ -65,7 +65,7 @@ class Validator {
|
|
65
65
|
if (typeof object[i] !== typeof structure[i])
|
66
66
|
errors.push("wrong datatype for field '" + i + "' expected " + typeof structure[i] + " got " + typeof object[i]);
|
67
67
|
if (typeof structure[i] === "object" || typeof structure[i] === "function") {
|
68
|
-
const result = await this.validateStructure(structure[i], object[i]);
|
68
|
+
const result = await this.validateStructure(structure[i], object[i], level++);
|
69
69
|
if (result) {
|
70
70
|
for (let j of result)
|
71
71
|
errors.push(j);
|
@@ -121,6 +121,21 @@ class Validator {
|
|
121
121
|
return errors;
|
122
122
|
return;
|
123
123
|
}
|
124
|
+
static filterStructure(structure, object) {
|
125
|
+
if (!structure || !object)
|
126
|
+
return;
|
127
|
+
if (typeof structure !== "object" || typeof object !== "object")
|
128
|
+
return;
|
129
|
+
if (Array.isArray(structure))
|
130
|
+
return;
|
131
|
+
for (let key in object) {
|
132
|
+
if (!(key in structure)) {
|
133
|
+
delete object[key];
|
134
|
+
continue;
|
135
|
+
}
|
136
|
+
this.filterStructure(structure[key], object[key]);
|
137
|
+
}
|
138
|
+
}
|
124
139
|
static convertToType(value, type) {
|
125
140
|
if (value === null || value === undefined)
|
126
141
|
return null;
|
@@ -12,6 +12,15 @@ declare class DefaultErrors {
|
|
12
12
|
static TIMEOUT: IError;
|
13
13
|
static METHOD_NOT_ALLOWED: IError;
|
14
14
|
static DB_ERROR: IError;
|
15
|
+
static FORBIDDEN: IError;
|
16
|
+
static NOT_FOUND: IError;
|
17
|
+
static INTERNAL_SERVER_ERROR: IError;
|
18
|
+
static INVALID_TOKEN: IError;
|
19
|
+
static INVALID_CREDENTIALS: IError;
|
20
|
+
static INVALID_DATA: IError;
|
21
|
+
static INVALID_FILE: IError;
|
22
|
+
static WRONG_DATA_TYPE: IError;
|
23
|
+
static UPROCESSIABLE_CONTENT: IError;
|
15
24
|
}
|
16
25
|
export declare class ErrorUtils {
|
17
26
|
static isError(obj: any): obj is IError;
|
@@ -12,9 +12,18 @@ class DefaultErrors {
|
|
12
12
|
static UNAUTHORIZED = { code: 6, message: "Unauthorized" };
|
13
13
|
static BAD_REQUEST = { code: 7, message: "Bad request", httpStatus: 400 };
|
14
14
|
static SERVICE_NOT_WORKING = { code: 8, message: "Service not working" };
|
15
|
-
static TIMEOUT = { code: 9, message: "Timeout" };
|
15
|
+
static TIMEOUT = { code: 9, message: "Timeout", httpStatus: 408 };
|
16
16
|
static METHOD_NOT_ALLOWED = { code: 10, message: "Method not allowed", httpStatus: 405 };
|
17
17
|
static DB_ERROR = { code: 11, message: "DB error", httpStatus: 500 };
|
18
|
+
static FORBIDDEN = { code: 12, message: "Forbidden", httpStatus: 403 };
|
19
|
+
static NOT_FOUND = { code: 13, message: "Not found", httpStatus: 404 };
|
20
|
+
static INTERNAL_SERVER_ERROR = { code: 14, message: "Internal server error", httpStatus: 500 };
|
21
|
+
static INVALID_TOKEN = { code: 15, message: "Invalid token", httpStatus: 401 };
|
22
|
+
static INVALID_CREDENTIALS = { code: 16, message: "Invalid credentials", httpStatus: 401 };
|
23
|
+
static INVALID_DATA = { code: 17, message: "Invalid data", httpStatus: 422 };
|
24
|
+
static INVALID_FILE = { code: 18, message: "Invalid file", httpStatus: 422 };
|
25
|
+
static WRONG_DATA_TYPE = { code: 23, message: "Wrong data type", httpStatus: 422 };
|
26
|
+
static UPROCESSIABLE_CONTENT = { code: 29, message: "Unprocessable content", httpStatus: 422 };
|
18
27
|
}
|
19
28
|
class ErrorUtils {
|
20
29
|
static isError(obj) {
|