@avleon/core 0.0.26 → 0.0.28
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/README.md +601 -561
- package/package.json +38 -6
- package/src/application.ts +104 -125
- package/src/authentication.ts +16 -16
- package/src/cache.ts +91 -91
- package/src/collection.test.ts +71 -0
- package/src/collection.ts +344 -254
- package/src/config.test.ts +35 -0
- package/src/config.ts +85 -42
- package/src/constants.ts +1 -1
- package/src/container.ts +54 -54
- package/src/controller.ts +125 -127
- package/src/decorators.ts +27 -27
- package/src/environment-variables.ts +53 -46
- package/src/exceptions/http-exceptions.ts +86 -86
- package/src/exceptions/index.ts +1 -1
- package/src/exceptions/system-exception.ts +35 -34
- package/src/file-storage.ts +206 -206
- package/src/helpers.ts +324 -328
- package/src/icore.ts +66 -90
- package/src/index.ts +30 -30
- package/src/interfaces/avleon-application.ts +32 -40
- package/src/logger.ts +72 -72
- package/src/map-types.ts +159 -159
- package/src/middleware.ts +119 -98
- package/src/multipart.ts +116 -116
- package/src/openapi.ts +372 -372
- package/src/params.ts +111 -111
- package/src/queue.ts +126 -126
- package/src/response.ts +74 -74
- package/src/results.ts +30 -30
- package/src/route-methods.ts +186 -186
- package/src/swagger-schema.ts +213 -213
- package/src/testing.ts +220 -220
- package/src/types/app-builder.interface.ts +18 -19
- package/src/types/application.interface.ts +7 -9
- package/src/utils/hash.ts +8 -5
- package/src/utils/index.ts +2 -2
- package/src/utils/optional-require.ts +50 -50
- package/src/validation.ts +160 -156
- package/src/validator-extend.ts +25 -25
package/src/validation.ts
CHANGED
|
@@ -1,156 +1,160 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { BadRequestException } from "./exceptions";
|
|
9
|
-
|
|
10
|
-
class PValidationRule<T> {
|
|
11
|
-
name: string;
|
|
12
|
-
type: T;
|
|
13
|
-
message?: string;
|
|
14
|
-
|
|
15
|
-
constructor(name: string, type: T, message?: string) {
|
|
16
|
-
this.name = name;
|
|
17
|
-
this.type = type;
|
|
18
|
-
this.message = message;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
type BaseRule = {
|
|
23
|
-
required?: boolean;
|
|
24
|
-
optional?: boolean;
|
|
25
|
-
message?: string;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
type StringRule = BaseRule & {
|
|
29
|
-
type: "string";
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
type NumberRule = BaseRule & {
|
|
33
|
-
type: "number";
|
|
34
|
-
min?: number;
|
|
35
|
-
max?: number;
|
|
36
|
-
exact?: number;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
type BooleanRule = BaseRule & {
|
|
40
|
-
type: "boolean";
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export type ValidationRule = StringRule | NumberRule | BooleanRule;
|
|
44
|
-
|
|
45
|
-
export type ValidationProps = {
|
|
46
|
-
[key: string]: ValidationRule;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export type ValidateOptons = {
|
|
50
|
-
location?:
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
class Validator {
|
|
54
|
-
private rules: PValidationRule<any>[] = [];
|
|
55
|
-
private options:ValidateOptons = {}
|
|
56
|
-
|
|
57
|
-
constructor(obj: ValidationProps, options?:ValidateOptons) {
|
|
58
|
-
this.init(obj);
|
|
59
|
-
if (options) {
|
|
60
|
-
this.options = options;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
private init(obj: ValidationProps) {
|
|
65
|
-
Object.keys(obj).forEach((key) => {
|
|
66
|
-
const rule = obj[key];
|
|
67
|
-
this.rules.push(
|
|
68
|
-
new PValidationRule<typeof rule.type>(key, rule.type, rule.message),
|
|
69
|
-
);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
validate(obj: any | Array<any>, options?:ValidateOptons) {
|
|
74
|
-
const erors: any[] = [];
|
|
75
|
-
|
|
76
|
-
this.rules.forEach((k) => {
|
|
77
|
-
const r = Object.keys(obj).find((key) => key == k.name);
|
|
78
|
-
let messages: any = [];
|
|
79
|
-
if (!r || obj[r] == undefined || obj[r] == "") {
|
|
80
|
-
messages.push({
|
|
81
|
-
constraint: "required",
|
|
82
|
-
message: k.name + " is required",
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (k.type == "string" && typeof obj[k.name] != "string") {
|
|
87
|
-
messages.push({
|
|
88
|
-
constraint: "type",
|
|
89
|
-
message: `${k.name} must be type ${k.type}`,
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
if (k.type == "number" && !parseInt(obj[k.name])) {
|
|
93
|
-
messages.push({
|
|
94
|
-
constraint: "type",
|
|
95
|
-
message: `${k.name} must be type ${k.type}`,
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (k.type == "number") {
|
|
100
|
-
obj[k.name] = parseInt(obj[k.name]);
|
|
101
|
-
}
|
|
102
|
-
if (k.type == "boolean" && !isBool(obj[k.name])) {
|
|
103
|
-
messages.push({
|
|
104
|
-
constraint: "type",
|
|
105
|
-
message: `${k.name} must be type ${k.type}`,
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
if (k.type == "boolean") {
|
|
109
|
-
obj[k.name] = parseBoolean(obj[k.name]);
|
|
110
|
-
}
|
|
111
|
-
if (messages.length > 0) {
|
|
112
|
-
erors.push({
|
|
113
|
-
path: k.name,
|
|
114
|
-
...(this.options.location ? { location: this.options.location } :{}),
|
|
115
|
-
constraints: messages,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
return [erors, obj];
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const isBool = (val: any) => {
|
|
125
|
-
if (typeof val == "boolean") return true;
|
|
126
|
-
if (parseInt(val) == 0 || parseInt(val) == 1) return true;
|
|
127
|
-
if (val == "true" || val == "false") return true;
|
|
128
|
-
|
|
129
|
-
return false;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
const parseBoolean = (val: any): boolean => {
|
|
133
|
-
if (typeof val === "boolean") return val;
|
|
134
|
-
|
|
135
|
-
// if (typeof val === "number") {
|
|
136
|
-
// return val !== 0; // Common convention: 0 → false, any other number → true
|
|
137
|
-
// }
|
|
138
|
-
|
|
139
|
-
if (parseInt(val) == 1) return true;
|
|
140
|
-
if (typeof val === "string") {
|
|
141
|
-
const normalized = val.trim().toLowerCase();
|
|
142
|
-
return normalized === "true";
|
|
143
|
-
}
|
|
144
|
-
return false; // Default for unsupported types (null, undefined, objects, etc.)
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
export function validateOrThrow<T extends {}>(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { BadRequestException } from "./exceptions";
|
|
9
|
+
|
|
10
|
+
class PValidationRule<T> {
|
|
11
|
+
name: string;
|
|
12
|
+
type: T;
|
|
13
|
+
message?: string;
|
|
14
|
+
|
|
15
|
+
constructor(name: string, type: T, message?: string) {
|
|
16
|
+
this.name = name;
|
|
17
|
+
this.type = type;
|
|
18
|
+
this.message = message;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type BaseRule = {
|
|
23
|
+
required?: boolean;
|
|
24
|
+
optional?: boolean;
|
|
25
|
+
message?: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type StringRule = BaseRule & {
|
|
29
|
+
type: "string";
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type NumberRule = BaseRule & {
|
|
33
|
+
type: "number";
|
|
34
|
+
min?: number;
|
|
35
|
+
max?: number;
|
|
36
|
+
exact?: number;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type BooleanRule = BaseRule & {
|
|
40
|
+
type: "boolean";
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type ValidationRule = StringRule | NumberRule | BooleanRule;
|
|
44
|
+
|
|
45
|
+
export type ValidationProps = {
|
|
46
|
+
[key: string]: ValidationRule;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type ValidateOptons = {
|
|
50
|
+
location?: "header" | "queryparam" | "param" | "body" | "custom";
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
class Validator {
|
|
54
|
+
private rules: PValidationRule<any>[] = [];
|
|
55
|
+
private options: ValidateOptons = {};
|
|
56
|
+
|
|
57
|
+
constructor(obj: ValidationProps, options?: ValidateOptons) {
|
|
58
|
+
this.init(obj);
|
|
59
|
+
if (options) {
|
|
60
|
+
this.options = options;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private init(obj: ValidationProps) {
|
|
65
|
+
Object.keys(obj).forEach((key) => {
|
|
66
|
+
const rule = obj[key];
|
|
67
|
+
this.rules.push(
|
|
68
|
+
new PValidationRule<typeof rule.type>(key, rule.type, rule.message),
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
validate(obj: any | Array<any>, options?: ValidateOptons) {
|
|
74
|
+
const erors: any[] = [];
|
|
75
|
+
|
|
76
|
+
this.rules.forEach((k) => {
|
|
77
|
+
const r = Object.keys(obj).find((key) => key == k.name);
|
|
78
|
+
let messages: any = [];
|
|
79
|
+
if (!r || obj[r] == undefined || obj[r] == "") {
|
|
80
|
+
messages.push({
|
|
81
|
+
constraint: "required",
|
|
82
|
+
message: k.name + " is required",
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (k.type == "string" && typeof obj[k.name] != "string") {
|
|
87
|
+
messages.push({
|
|
88
|
+
constraint: "type",
|
|
89
|
+
message: `${k.name} must be type ${k.type}`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (k.type == "number" && !parseInt(obj[k.name])) {
|
|
93
|
+
messages.push({
|
|
94
|
+
constraint: "type",
|
|
95
|
+
message: `${k.name} must be type ${k.type}`,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (k.type == "number") {
|
|
100
|
+
obj[k.name] = parseInt(obj[k.name]);
|
|
101
|
+
}
|
|
102
|
+
if (k.type == "boolean" && !isBool(obj[k.name])) {
|
|
103
|
+
messages.push({
|
|
104
|
+
constraint: "type",
|
|
105
|
+
message: `${k.name} must be type ${k.type}`,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (k.type == "boolean") {
|
|
109
|
+
obj[k.name] = parseBoolean(obj[k.name]);
|
|
110
|
+
}
|
|
111
|
+
if (messages.length > 0) {
|
|
112
|
+
erors.push({
|
|
113
|
+
path: k.name,
|
|
114
|
+
...(this.options.location ? { location: this.options.location } : {}),
|
|
115
|
+
constraints: messages,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return [erors, obj];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const isBool = (val: any) => {
|
|
125
|
+
if (typeof val == "boolean") return true;
|
|
126
|
+
if (parseInt(val) == 0 || parseInt(val) == 1) return true;
|
|
127
|
+
if (val == "true" || val == "false") return true;
|
|
128
|
+
|
|
129
|
+
return false;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const parseBoolean = (val: any): boolean => {
|
|
133
|
+
if (typeof val === "boolean") return val;
|
|
134
|
+
|
|
135
|
+
// if (typeof val === "number") {
|
|
136
|
+
// return val !== 0; // Common convention: 0 → false, any other number → true
|
|
137
|
+
// }
|
|
138
|
+
|
|
139
|
+
if (parseInt(val) == 1) return true;
|
|
140
|
+
if (typeof val === "string") {
|
|
141
|
+
const normalized = val.trim().toLowerCase();
|
|
142
|
+
return normalized === "true";
|
|
143
|
+
}
|
|
144
|
+
return false; // Default for unsupported types (null, undefined, objects, etc.)
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export function validateOrThrow<T extends {}>(
|
|
148
|
+
obj: T,
|
|
149
|
+
rules: ValidationProps,
|
|
150
|
+
options?: ValidateOptons,
|
|
151
|
+
) {
|
|
152
|
+
const valid = new Validator(rules, options);
|
|
153
|
+
const errors = valid.validate(obj);
|
|
154
|
+
|
|
155
|
+
if (errors[0].length > 0) {
|
|
156
|
+
throw new BadRequestException(errors[0]);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return errors[1];
|
|
160
|
+
}
|
package/src/validator-extend.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
export function IsArrayNotEmpty(validationOptions?: any) {
|
|
8
|
-
const { registerDecorator, ValidationArguments } = require("class-validator");
|
|
9
|
-
return function (object: Object, propertyName: string) {
|
|
10
|
-
registerDecorator({
|
|
11
|
-
name: "isArrayWithAtLeastOneElement",
|
|
12
|
-
target: object.constructor,
|
|
13
|
-
propertyName: propertyName,
|
|
14
|
-
options: validationOptions,
|
|
15
|
-
validator: {
|
|
16
|
-
validate(value: any, args: any) {
|
|
17
|
-
return Array.isArray(value) && value.length > 0;
|
|
18
|
-
},
|
|
19
|
-
defaultMessage(args: any) {
|
|
20
|
-
return `${args.property} must contain at least one item.`;
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
};
|
|
25
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
export function IsArrayNotEmpty(validationOptions?: any) {
|
|
8
|
+
const { registerDecorator, ValidationArguments } = require("class-validator");
|
|
9
|
+
return function (object: Object, propertyName: string) {
|
|
10
|
+
registerDecorator({
|
|
11
|
+
name: "isArrayWithAtLeastOneElement",
|
|
12
|
+
target: object.constructor,
|
|
13
|
+
propertyName: propertyName,
|
|
14
|
+
options: validationOptions,
|
|
15
|
+
validator: {
|
|
16
|
+
validate(value: any, args: any) {
|
|
17
|
+
return Array.isArray(value) && value.length > 0;
|
|
18
|
+
},
|
|
19
|
+
defaultMessage(args: any) {
|
|
20
|
+
return `${args.property} must contain at least one item.`;
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
}
|