badmfck-api-server 3.9.6 → 3.9.8
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,9 +1,3 @@
|
|
1
|
-
type SyncOptions = {
|
2
|
-
fillOnlyWhenNull?: boolean;
|
3
|
-
resetOnTypeMismatch?: boolean;
|
4
|
-
repairArrayItems?: "coerce";
|
5
|
-
coerceArrayFromObject?: "values" | "numericKeys" | "wrap" | "off";
|
6
|
-
};
|
7
1
|
export type TValidatorType = "string" | "number" | "boolean" | "date" | "array" | "object" | "email" | "phone";
|
8
2
|
export interface IStructureOptions {
|
9
3
|
optional?: boolean;
|
@@ -76,7 +70,7 @@ export declare class Validator {
|
|
76
70
|
static filterStructure(structure: any, object: any): void;
|
77
71
|
static isPlainObject(v: any): boolean;
|
78
72
|
static clone<T>(v: T): T;
|
79
|
-
static syncStructure(structure: any,
|
73
|
+
static syncStructure(structure: any, node: any): any;
|
80
74
|
static convertToType<T>(value: any, type: "string" | "number" | "boolean" | "date"): T | null;
|
81
75
|
static validateValue(value: string | number | boolean, opt?: IValidatorOptions): ValidationReport;
|
82
76
|
}
|
@@ -301,114 +301,84 @@ class Validator {
|
|
301
301
|
return structuredClone(v);
|
302
302
|
return JSON.parse(JSON.stringify(v));
|
303
303
|
}
|
304
|
-
static syncStructure(structure,
|
305
|
-
fillOnlyWhenNull: true,
|
306
|
-
resetOnTypeMismatch: true,
|
307
|
-
repairArrayItems: "coerce",
|
308
|
-
coerceArrayFromObject: "values"
|
309
|
-
}) {
|
304
|
+
static syncStructure(structure, node) {
|
310
305
|
if (structure === null || typeof structure !== "object")
|
311
|
-
return
|
306
|
+
return node;
|
312
307
|
if (Array.isArray(structure)) {
|
313
308
|
const tmpl = structure[0];
|
314
|
-
if (!Array.isArray(
|
315
|
-
const
|
316
|
-
|
317
|
-
|
318
|
-
|
309
|
+
if (!Array.isArray(node) && this.isPlainObject(node)) {
|
310
|
+
const numericKeys = Object.keys(node)
|
311
|
+
.filter((k) => String(+k) === k)
|
312
|
+
.map((k) => +k)
|
313
|
+
.sort((a, b) => a - b);
|
314
|
+
const items = numericKeys.length > 0
|
315
|
+
? numericKeys.map((i) => node[String(i)])
|
316
|
+
: Object.values(node);
|
317
|
+
return items.map((it) => this.syncStructure(tmpl, it));
|
318
|
+
}
|
319
|
+
if (!Array.isArray(node))
|
320
|
+
return node;
|
321
|
+
if (tmpl === undefined)
|
322
|
+
return node;
|
323
|
+
const out = new Array(node.length);
|
324
|
+
for (let i = 0; i < node.length; i++) {
|
325
|
+
const item = node[i];
|
326
|
+
if (item === null || item === undefined) {
|
327
|
+
out[i] = this.syncStructure(tmpl, this.clone(tmpl));
|
319
328
|
}
|
320
|
-
else if (
|
321
|
-
|
329
|
+
else if (this.isPlainObject(tmpl) && this.isPlainObject(item)) {
|
330
|
+
out[i] = this.syncStructure(tmpl, item);
|
322
331
|
}
|
323
|
-
else if (
|
324
|
-
|
332
|
+
else if (Array.isArray(tmpl) && Array.isArray(item)) {
|
333
|
+
out[i] = this.syncStructure(tmpl, item);
|
325
334
|
}
|
326
335
|
else {
|
327
|
-
|
328
|
-
}
|
329
|
-
}
|
330
|
-
if (!Array.isArray(object))
|
331
|
-
return object;
|
332
|
-
if (tmpl !== undefined) {
|
333
|
-
for (let i = 0; i < object.length; i++) {
|
334
|
-
let item = object[i];
|
335
|
-
if (item === null || item === undefined) {
|
336
|
-
object[i] = this.clone(tmpl);
|
337
|
-
this.syncStructure(tmpl, object[i], opts);
|
338
|
-
continue;
|
339
|
-
}
|
340
|
-
if (this.isPlainObject(tmpl) && this.isPlainObject(item)) {
|
341
|
-
this.syncStructure(tmpl, item, opts);
|
342
|
-
continue;
|
343
|
-
}
|
344
|
-
if (Array.isArray(tmpl) && Array.isArray(item)) {
|
345
|
-
this.syncStructure(tmpl, item, opts);
|
346
|
-
continue;
|
347
|
-
}
|
348
|
-
if (opts.repairArrayItems === "coerce") {
|
349
|
-
object[i] = this.clone(tmpl);
|
350
|
-
this.syncStructure(tmpl, object[i], opts);
|
351
|
-
}
|
336
|
+
out[i] = this.syncStructure(tmpl, this.clone(tmpl));
|
352
337
|
}
|
353
338
|
}
|
354
|
-
return
|
355
|
-
}
|
356
|
-
if (!this.isPlainObject(object))
|
357
|
-
return object;
|
358
|
-
for (const key in object) {
|
359
|
-
if (!(key in structure))
|
360
|
-
delete object[key];
|
339
|
+
return out;
|
361
340
|
}
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
const
|
366
|
-
|
367
|
-
|
368
|
-
object[key] = this.clone(structureValue);
|
369
|
-
continue;
|
370
|
-
}
|
371
|
-
if (key.startsWith("$__")) {
|
372
|
-
object[key] = this.clone(structureValue);
|
341
|
+
const out = {};
|
342
|
+
const src = this.isPlainObject(node) ? node : {};
|
343
|
+
for (const k of Object.keys(structure)) {
|
344
|
+
const sv = structure[k];
|
345
|
+
if (k.startsWith("$__")) {
|
346
|
+
out[k] = this.clone(sv);
|
373
347
|
continue;
|
374
348
|
}
|
375
|
-
|
376
|
-
|
377
|
-
|
349
|
+
const has = Object.prototype.hasOwnProperty.call(src, k);
|
350
|
+
const ov = has ? src[k] : undefined;
|
351
|
+
const { optional, skip_validation, default: def } = this.parseStructureOptions(k, structure);
|
352
|
+
if (!has) {
|
353
|
+
if (sv && typeof sv === "object") {
|
354
|
+
out[k] = this.syncStructure(sv, this.clone(sv));
|
355
|
+
}
|
356
|
+
else {
|
357
|
+
out[k] = this.clone(sv);
|
378
358
|
}
|
379
359
|
continue;
|
380
360
|
}
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
if (this.isPlainObject(objectValue)) {
|
390
|
-
for (const k in objectValue)
|
391
|
-
base[k] = objectValue[k];
|
392
|
-
this.syncStructure(structureValue, base, opts);
|
393
|
-
object[key] = base;
|
394
|
-
}
|
395
|
-
else {
|
396
|
-
object[key] = base;
|
397
|
-
}
|
398
|
-
}
|
361
|
+
if (ov === null || ov === undefined) {
|
362
|
+
if (def !== undefined && !optional) {
|
363
|
+
out[k] = this.clone(def);
|
364
|
+
}
|
365
|
+
else if (!optional || skip_validation) {
|
366
|
+
out[k] = sv && typeof sv === "object"
|
367
|
+
? this.syncStructure(sv, this.clone(sv))
|
368
|
+
: this.clone(sv);
|
399
369
|
}
|
400
370
|
else {
|
401
|
-
|
371
|
+
out[k] = ov;
|
402
372
|
}
|
403
373
|
continue;
|
404
374
|
}
|
405
|
-
if (typeof
|
406
|
-
|
407
|
-
|
408
|
-
}
|
375
|
+
if (sv && typeof sv === "object") {
|
376
|
+
out[k] = this.syncStructure(sv, ov);
|
377
|
+
continue;
|
409
378
|
}
|
379
|
+
out[k] = ov;
|
410
380
|
}
|
411
|
-
return
|
381
|
+
return out;
|
412
382
|
}
|
413
383
|
static convertToType(value, type) {
|
414
384
|
if (value === null || value === undefined)
|