topsyde-utils 1.0.200 → 1.0.201
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/utils/BaseDto.d.ts +9 -1
- package/dist/utils/BaseDto.js +84 -1
- package/dist/utils/BaseDto.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/BaseDto.ts +31 -1
package/dist/utils/BaseDto.d.ts
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
import type { ClassConstructor, ClassTransformOptions } from "class-transformer";
|
2
|
-
import { ValidationError } from "class-validator";
|
2
|
+
import { ValidationArguments, ValidationError, ValidatorConstraintInterface } from "class-validator";
|
3
|
+
/**
|
4
|
+
* Custom validator for number | number[] type
|
5
|
+
* Validates that value is either a single number or a 2-element number array [min, max]
|
6
|
+
*/
|
7
|
+
export declare class IsNumberOrRangeConstraint implements ValidatorConstraintInterface {
|
8
|
+
validate(value: any, args: ValidationArguments): boolean;
|
9
|
+
defaultMessage(args: ValidationArguments): string;
|
10
|
+
}
|
3
11
|
/**
|
4
12
|
* Base typesafe class for Data Transfer Objects (DTOs)
|
5
13
|
*/
|
package/dist/utils/BaseDto.js
CHANGED
@@ -1,6 +1,89 @@
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
6
|
+
var _, done = false;
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
8
|
+
var context = {};
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
13
|
+
if (kind === "accessor") {
|
14
|
+
if (result === void 0) continue;
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
19
|
+
}
|
20
|
+
else if (_ = accept(result)) {
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
22
|
+
else descriptor[key] = _;
|
23
|
+
}
|
24
|
+
}
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
26
|
+
done = true;
|
27
|
+
};
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
29
|
+
var useValue = arguments.length > 2;
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
32
|
+
}
|
33
|
+
return useValue ? value : void 0;
|
34
|
+
};
|
35
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
36
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
37
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
38
|
+
};
|
1
39
|
import { instanceToPlain, plainToInstance } from "class-transformer";
|
2
40
|
import Guards from "./Guards.js";
|
3
|
-
import { validateSync } from "class-validator";
|
41
|
+
import { ValidatorConstraint, validateSync } from "class-validator";
|
42
|
+
/**
|
43
|
+
* Custom validator for number | number[] type
|
44
|
+
* Validates that value is either a single number or a 2-element number array [min, max]
|
45
|
+
*/
|
46
|
+
let IsNumberOrRangeConstraint = (() => {
|
47
|
+
let _classDecorators = [ValidatorConstraint({ name: "isNumberOrRange", async: false })];
|
48
|
+
let _classDescriptor;
|
49
|
+
let _classExtraInitializers = [];
|
50
|
+
let _classThis;
|
51
|
+
var IsNumberOrRangeConstraint = _classThis = class {
|
52
|
+
validate(value, args) {
|
53
|
+
// Allow single number
|
54
|
+
if (typeof value === "number" && !isNaN(value)) {
|
55
|
+
return true;
|
56
|
+
}
|
57
|
+
// Allow array of exactly 2 numbers (range)
|
58
|
+
if (Array.isArray(value)) {
|
59
|
+
if (value.length !== 2)
|
60
|
+
return false;
|
61
|
+
if (typeof value[0] !== "number" || isNaN(value[0]))
|
62
|
+
return false;
|
63
|
+
if (typeof value[1] !== "number" || isNaN(value[1]))
|
64
|
+
return false;
|
65
|
+
// Optional: Validate min <= max
|
66
|
+
if (value[0] > value[1])
|
67
|
+
return false;
|
68
|
+
return true;
|
69
|
+
}
|
70
|
+
return false;
|
71
|
+
}
|
72
|
+
defaultMessage(args) {
|
73
|
+
return "Value must be a number or a 2-element number array [min, max]";
|
74
|
+
}
|
75
|
+
};
|
76
|
+
__setFunctionName(_classThis, "IsNumberOrRangeConstraint");
|
77
|
+
(() => {
|
78
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
79
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
80
|
+
IsNumberOrRangeConstraint = _classThis = _classDescriptor.value;
|
81
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
82
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
83
|
+
})();
|
84
|
+
return IsNumberOrRangeConstraint = _classThis;
|
85
|
+
})();
|
86
|
+
export { IsNumberOrRangeConstraint };
|
4
87
|
/**
|
5
88
|
* Base typesafe class for Data Transfer Objects (DTOs)
|
6
89
|
*/
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"BaseDto.js","sourceRoot":"","sources":["../../src/utils/BaseDto.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,MAAM,MAAM,UAAU,CAAC;AAC9B,OAAO,
|
1
|
+
{"version":3,"file":"BaseDto.js","sourceRoot":"","sources":["../../src/utils/BaseDto.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,MAAM,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAwC,mBAAmB,EAAgC,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAExI;;;GAGG;IAEU,yBAAyB;4BADrC,mBAAmB,CAAC,EAAE,IAAI,EAAE,iBAAiB,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;;;;;QAE9D,QAAQ,CAAC,KAAU,EAAE,IAAyB;YAC7C,sBAAsB;YACtB,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;gBAChD,OAAO,IAAI,CAAC;YACb,CAAC;YAED,2CAA2C;YAC3C,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC1B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;oBAAE,OAAO,KAAK,CAAC;gBACrC,IAAI,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;oBAAE,OAAO,KAAK,CAAC;gBAClE,IAAI,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;oBAAE,OAAO,KAAK,CAAC;gBAClE,gCAAgC;gBAChC,IAAI,KAAK,CAAC,CAAC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YAC9B,GAAG,OAAO;SACV,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,UAAU,CAAgB,GAAwB,EAAE,SAAoC,EAAE,UAAiC,EAAE;QAC1I,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IAChE,CAAC;;AArED;;GAEG;AACuB,2BAAuB,GAA0B;IAC1E,uBAAuB,EAAE,IAAI;IAC7B,mBAAmB,EAAE,IAAI;IACzB,mBAAmB,EAAE,IAAI;IACzB,wBAAwB,EAAE,KAAK,EAAE,uDAAuD;CACxF,CAAC","sourcesContent":["import type { ClassConstructor, ClassTransformOptions } from \"class-transformer\";\nimport { instanceToPlain, plainToInstance } from \"class-transformer\";\nimport Guards from \"./Guards\";\nimport { ValidationArguments, ValidationError, ValidatorConstraint, ValidatorConstraintInterface, validateSync } from \"class-validator\";\n\n/**\n * Custom validator for number | number[] type\n * Validates that value is either a single number or a 2-element number array [min, max]\n */\n@ValidatorConstraint({ name: \"isNumberOrRange\", async: false })\nexport class IsNumberOrRangeConstraint implements ValidatorConstraintInterface {\n\tvalidate(value: any, args: ValidationArguments) {\n\t\t// Allow single number\n\t\tif (typeof value === \"number\" && !isNaN(value)) {\n\t\t\treturn true;\n\t\t}\n\n\t\t// Allow array of exactly 2 numbers (range)\n\t\tif (Array.isArray(value)) {\n\t\t\tif (value.length !== 2) return false;\n\t\t\tif (typeof value[0] !== \"number\" || isNaN(value[0])) return false;\n\t\t\tif (typeof value[1] !== \"number\" || isNaN(value[1])) return false;\n\t\t\t// Optional: Validate min <= max\n\t\t\tif (value[0] > value[1]) return false;\n\t\t\treturn true;\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tdefaultMessage(args: ValidationArguments) {\n\t\treturn \"Value must be a number or a 2-element number array [min, max]\";\n\t}\n}\n\n/**\n * Base typesafe class for Data Transfer Objects (DTOs)\n */\nexport abstract class Dto {\n\t/**\n\t * Default options for class transformation\n\t */\n\tprotected static readonly defaultTransformOptions: ClassTransformOptions = {\n\t\texcludeExtraneousValues: true,\n\t\tenableCircularCheck: true,\n\t\texposeDefaultValues: true,\n\t\tenableImplicitConversion: false, // Safer default, especially when using class-validator\n\t};\n\n\t/**\n\t * Validates the DTO instance\n\t * @throws ValidationError[] if validation fails\n\t */\n\tpublic validate(): ValidationError[] {\n\t\tconst errors = validateSync(this, {\n\t\t\tvalidationError: { target: false },\n\t\t\tforbidUnknownValues: true,\n\t\t});\n\t\tif (errors.length > 0) {\n\t\t\tthrow errors;\n\t\t}\n\t\treturn errors;\n\t}\n\n\t/**\n\t * Converts the DTO to a plain object\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns Plain object representation of the DTO\n\t */\n\tpublic toJSON<T = Record<string, unknown>>(include_undefined: boolean = true, options?: ClassTransformOptions): T {\n\t\tconst value = instanceToPlain(this, {\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t}) as T;\n\n\t\tif (!include_undefined) {\n\t\t\treturn Object.fromEntries(Object.entries(value as Record<string, unknown>).filter(([_, v]) => !Guards.IsNil(v))) as T;\n\t\t}\n\n\t\treturn value;\n\t}\n\n\t/**\n\t * Creates a new instance of the DTO with validation\n\t * @param cls - The class constructor to create an instance from\n\t * @param data - Data to create the DTO from\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns New instance of the DTO\n\t * @throws ValidationError[] if validation fails and validate is true\n\t */\n\tpublic static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options: ClassTransformOptions = {}): T {\n\t\tconst instance = plainToInstance(cls, data, {\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t});\n\n\t\treturn instance;\n\t}\n\n\t/**\n\t * Creates an array of DTOs from an array of plain objects\n\t * @param cls - The class constructor to create instances from\n\t * @param dataArray - Array of data to create DTOs from\n\t * @param options - Class transformer options for controlling exposure and transformation\n\t * @returns Array of DTO instances\n\t */\n\tpublic static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {\n\t\treturn dataArray.map((data) => Dto.Create(cls, data, options));\n\t}\n}\n"]}
|
package/package.json
CHANGED
package/src/utils/BaseDto.ts
CHANGED
@@ -1,7 +1,37 @@
|
|
1
1
|
import type { ClassConstructor, ClassTransformOptions } from "class-transformer";
|
2
2
|
import { instanceToPlain, plainToInstance } from "class-transformer";
|
3
3
|
import Guards from "./Guards";
|
4
|
-
import { ValidationError, validateSync } from "class-validator";
|
4
|
+
import { ValidationArguments, ValidationError, ValidatorConstraint, ValidatorConstraintInterface, validateSync } from "class-validator";
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Custom validator for number | number[] type
|
8
|
+
* Validates that value is either a single number or a 2-element number array [min, max]
|
9
|
+
*/
|
10
|
+
@ValidatorConstraint({ name: "isNumberOrRange", async: false })
|
11
|
+
export class IsNumberOrRangeConstraint implements ValidatorConstraintInterface {
|
12
|
+
validate(value: any, args: ValidationArguments) {
|
13
|
+
// Allow single number
|
14
|
+
if (typeof value === "number" && !isNaN(value)) {
|
15
|
+
return true;
|
16
|
+
}
|
17
|
+
|
18
|
+
// Allow array of exactly 2 numbers (range)
|
19
|
+
if (Array.isArray(value)) {
|
20
|
+
if (value.length !== 2) return false;
|
21
|
+
if (typeof value[0] !== "number" || isNaN(value[0])) return false;
|
22
|
+
if (typeof value[1] !== "number" || isNaN(value[1])) return false;
|
23
|
+
// Optional: Validate min <= max
|
24
|
+
if (value[0] > value[1]) return false;
|
25
|
+
return true;
|
26
|
+
}
|
27
|
+
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
defaultMessage(args: ValidationArguments) {
|
32
|
+
return "Value must be a number or a 2-element number array [min, max]";
|
33
|
+
}
|
34
|
+
}
|
5
35
|
|
6
36
|
/**
|
7
37
|
* Base typesafe class for Data Transfer Objects (DTOs)
|