badmfck-api-server 3.8.9 → 3.9.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.
@@ -1,4 +1,14 @@
|
|
1
1
|
export type TValidatorType = "string" | "number" | "boolean" | "date" | "array" | "object" | "email" | "phone";
|
2
|
+
export interface IStructureOptions {
|
3
|
+
optional?: boolean;
|
4
|
+
skip_validation?: boolean;
|
5
|
+
default?: any;
|
6
|
+
values?: any;
|
7
|
+
regex?: string;
|
8
|
+
min?: number;
|
9
|
+
max?: number;
|
10
|
+
details?: string;
|
11
|
+
}
|
2
12
|
export interface IValidatorOptions {
|
3
13
|
min?: number;
|
4
14
|
max?: number;
|
@@ -47,6 +57,7 @@ export type ValidationModel<T> = DeepMutable<{
|
|
47
57
|
export declare class Validator {
|
48
58
|
static validateObject(fields: string[], object: any): boolean;
|
49
59
|
static documentStructure(structure: any): Record<string, any> | undefined;
|
60
|
+
static parseStructureOptions(key: string, structure: Record<string, any>): IStructureOptions;
|
50
61
|
static validateStructure(structure: any, object: any, level?: number, parentPath?: string): Promise<string[] | undefined>;
|
51
62
|
static filterStructure(structure: any, object: any): void;
|
52
63
|
static syncStructure(structure: any, object: any): any;
|
@@ -60,6 +60,33 @@ class Validator {
|
|
60
60
|
}
|
61
61
|
return params;
|
62
62
|
}
|
63
|
+
static parseStructureOptions(key, structure) {
|
64
|
+
let k = '$__' + key;
|
65
|
+
let s_options = structure[k];
|
66
|
+
if (!s_options)
|
67
|
+
s_options = {};
|
68
|
+
if (typeof s_options === "string")
|
69
|
+
s_options = { details: s_options };
|
70
|
+
k = k + "_";
|
71
|
+
if (s_options.min == undefined || isNaN(s_options.min)) {
|
72
|
+
if (structure[key + "min"] && !isNaN(structure[key + "min"]))
|
73
|
+
s_options.min = parseInt(structure[key + "min"]);
|
74
|
+
}
|
75
|
+
if (s_options.min && isNaN(s_options.min))
|
76
|
+
s_options.min = undefined;
|
77
|
+
if (s_options.max == undefined || isNaN(s_options.max)) {
|
78
|
+
if (structure[key + "max"] && !isNaN(structure[key + "max"]))
|
79
|
+
s_options.min = parseInt(structure[key + "max"]);
|
80
|
+
}
|
81
|
+
if (s_options.max && isNaN(s_options.max))
|
82
|
+
s_options.max = undefined;
|
83
|
+
return {
|
84
|
+
...s_options,
|
85
|
+
optional: s_options.optional === undefined ? structure[k + 'optional'] ?? false : s_options.optional,
|
86
|
+
default: s_options.default === undefined ? structure[k + 'default'] ?? undefined : s_options.default,
|
87
|
+
regex: s_options.regex === undefined ? structure[k + 'regex'] ?? undefined : s_options.regex,
|
88
|
+
};
|
89
|
+
}
|
63
90
|
static async validateStructure(structure, object, level = 0, parentPath = "") {
|
64
91
|
if (!structure)
|
65
92
|
return;
|
@@ -91,13 +118,21 @@ class Validator {
|
|
91
118
|
foundKeys.push(i);
|
92
119
|
continue;
|
93
120
|
}
|
94
|
-
let
|
95
|
-
if (
|
96
|
-
|
97
|
-
|
121
|
+
let structureOptions = Validator.parseStructureOptions(i, structure);
|
122
|
+
if (structureOptions.skip_validation) {
|
123
|
+
foundKeys.push(i);
|
124
|
+
continue;
|
125
|
+
}
|
126
|
+
if ((object[i] === null || object[i] === undefined || !(i in object)) && structure[i] !== null && structure[i] !== undefined) {
|
127
|
+
if (structureOptions.default) {
|
128
|
+
object[i] = structureOptions.default;
|
98
129
|
}
|
99
130
|
else {
|
100
|
-
if (
|
131
|
+
if (structureOptions.optional) {
|
132
|
+
foundKeys.push(i);
|
133
|
+
continue;
|
134
|
+
}
|
135
|
+
else {
|
101
136
|
if (Array.isArray(object) && !isNaN(parseInt(`${i}`))) {
|
102
137
|
const p = parentPath.length > 0 ? parentPath.slice(0, -1) : parentPath;
|
103
138
|
errors.push("no item for '" + p + "[" + i + "]'");
|
@@ -107,19 +142,17 @@ class Validator {
|
|
107
142
|
}
|
108
143
|
continue;
|
109
144
|
}
|
110
|
-
else {
|
111
|
-
foundKeys.push(i);
|
112
|
-
continue;
|
113
|
-
}
|
114
145
|
}
|
115
146
|
}
|
116
|
-
if (object[i] === null || object[i] === undefined) {
|
117
|
-
if (
|
118
|
-
errors.push("no value for field '" + parentPath + i + "'");
|
119
|
-
else {
|
147
|
+
if ((object[i] === null || object[i] === undefined)) {
|
148
|
+
if (structure[i] === null || structure[i] === undefined) {
|
120
149
|
foundKeys.push(i);
|
121
150
|
continue;
|
122
151
|
}
|
152
|
+
else {
|
153
|
+
errors.push("no field '" + parentPath + i + "'");
|
154
|
+
continue;
|
155
|
+
}
|
123
156
|
}
|
124
157
|
if (typeof structure[i] === "number" && typeof object[i] === "string") {
|
125
158
|
let num = parseInt(object[i]);
|
@@ -159,25 +192,23 @@ class Validator {
|
|
159
192
|
errors.push("wrong value for field '" + parentPath + i + "', expected " + expected.join(", ") + " got: " + value);
|
160
193
|
}
|
161
194
|
}
|
162
|
-
if (structure['$__' + i + "_values"])
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
}
|
195
|
+
if (structure['$__' + i + "_values"])
|
196
|
+
structureOptions.values = structure['$__' + i + "_values"];
|
197
|
+
if (Array.isArray(structureOptions.values) && structureOptions.values.length) {
|
198
|
+
if (!structureOptions.values.includes(object[i])) {
|
199
|
+
let caseValue = null;
|
200
|
+
for (let i of structureOptions.values) {
|
201
|
+
const v = i.toLowerCase();
|
202
|
+
if (v === (object[i] + "").toLowerCase()) {
|
203
|
+
caseValue = object[i];
|
204
|
+
break;
|
173
205
|
}
|
174
|
-
errors.push("wrong value for field '" + parentPath + i + "'" + (caseValue ? ", check case for: " + caseValue : ""));
|
175
206
|
}
|
207
|
+
errors.push("wrong value for field '" + parentPath + i + "'" + (caseValue ? ", check case for: " + caseValue : ""));
|
176
208
|
}
|
177
209
|
}
|
178
|
-
if (typeof structure[i] === "string" &&
|
179
|
-
const
|
180
|
-
const reg = new RegExp(regex);
|
210
|
+
if (typeof structure[i] === "string" && structureOptions.regex) {
|
211
|
+
const reg = new RegExp(structureOptions.regex);
|
181
212
|
if (reg.test(object[i]) === false)
|
182
213
|
errors.push("wrong value for field '" + parentPath + i + "', false mask for: >" + object[i] + "<");
|
183
214
|
}
|
@@ -215,25 +246,21 @@ class Validator {
|
|
215
246
|
}
|
216
247
|
}
|
217
248
|
}
|
218
|
-
if (typeof structure[i] === "number" &&
|
219
|
-
|
220
|
-
|
221
|
-
errors.push("value for field '" + parentPath + i + "' is too small, expected more than " + min + " got: " + object[i]);
|
249
|
+
if (typeof structure[i] === "number" && structureOptions.min && !isNaN(structureOptions.min)) {
|
250
|
+
if (object[i] < structureOptions.min)
|
251
|
+
errors.push("value for field '" + parentPath + i + "' is too small, expected more than " + structureOptions.min + " got: " + object[i]);
|
222
252
|
}
|
223
|
-
if (typeof structure[i] === "number" &&
|
224
|
-
|
225
|
-
|
226
|
-
errors.push("value for field '" + parentPath + i + "' is too big, expected less than " + max + " got: " + object[i]);
|
253
|
+
if (typeof structure[i] === "number" && structureOptions.max && !isNaN(structureOptions.max)) {
|
254
|
+
if (object[i] > structureOptions.max)
|
255
|
+
errors.push("value for field '" + parentPath + i + "' is too big, expected less than " + structureOptions.max + " got: " + object[i]);
|
227
256
|
}
|
228
|
-
if (typeof object[i] === "string" &&
|
229
|
-
|
230
|
-
|
231
|
-
errors.push("value length for field '" + parentPath + i + "' is too small, expected more than " + min + " got: " + object[i].length);
|
257
|
+
if (typeof object[i] === "string" && structureOptions.min && !isNaN(structureOptions.min)) {
|
258
|
+
if (object[i].length < structureOptions.min)
|
259
|
+
errors.push("value length for field '" + parentPath + i + "' is too small, expected more than " + structureOptions.min + " got: " + object[i].length);
|
232
260
|
}
|
233
|
-
if (typeof object[i] === "string" &&
|
234
|
-
|
235
|
-
|
236
|
-
errors.push("value length for field '" + parentPath + i + "' is too big, expected less than " + max + " got: " + object[i].length);
|
261
|
+
if (typeof object[i] === "string" && structureOptions.max && !isNaN(structureOptions.max)) {
|
262
|
+
if (object[i].length > structureOptions.max)
|
263
|
+
errors.push("value length for field '" + parentPath + i + "' is too big, expected less than " + structureOptions.max + " got: " + object[i].length);
|
237
264
|
}
|
238
265
|
foundKeys.push(i);
|
239
266
|
}
|
@@ -281,10 +308,16 @@ class Validator {
|
|
281
308
|
object[key] = structureValue;
|
282
309
|
continue;
|
283
310
|
}
|
311
|
+
const options = Validator.parseStructureOptions(key, structure);
|
284
312
|
if (objectValue === null && structureValue !== null) {
|
285
|
-
if (!
|
313
|
+
if (!options.optional) {
|
286
314
|
object[key] = structureValue;
|
287
|
-
|
315
|
+
continue;
|
316
|
+
}
|
317
|
+
if (!options.skip_validation) {
|
318
|
+
object[key] = structureValue;
|
319
|
+
continue;
|
320
|
+
}
|
288
321
|
}
|
289
322
|
if (key.startsWith("$__")) {
|
290
323
|
object[key] = structure[key];
|