topsyde-utils 1.0.201 → 1.0.203

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/index.d.ts CHANGED
@@ -28,6 +28,7 @@ export * from "./utils/Guards";
28
28
  export * from "./utils/BaseEntity";
29
29
  export * from "./utils/Console";
30
30
  export * from "./utils/BaseDto";
31
+ export * from "./utils/dto_validators/IsNumberOrRangeConstraint";
31
32
  export { default as Singleton } from "./singleton";
32
33
  export { default as Initializable } from "./initializable";
33
34
  export { default as Application } from "./application";
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ export * from "./utils/Guards.js";
31
31
  export * from "./utils/BaseEntity.js";
32
32
  export * from "./utils/Console.js";
33
33
  export * from "./utils/BaseDto.js";
34
+ export * from "./utils/dto_validators/IsNumberOrRangeConstraint.js";
34
35
  // Export default classes
35
36
  export { default as Singleton } from "./singleton.js";
36
37
  export { default as Initializable } from "./initializable.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,qBAAqB;AACrB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,uBAAuB,CAAC;AACtC,cAAc,8CAA8C,CAAC;AAC7D,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0CAA0C,CAAC;AACzD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAyC,CAAC;A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aAAa,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAErD,sDAAsD;AACtD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEtE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACrF,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAK/C,OAAO,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,MAAM,wCAAwC,CAAC;AAmB5G,OAAO,EAAE,aAAa,EAAiC,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\n// Export all modules\nexport * from \"./errors\";\nexport * from \"./singleton\";\nexport * from \"./initializable\";\nexport * from \"./application\";\nexport * from \"./consts\";\nexport * from \"./types\";\nexport * from \"./throwable\";\nexport * from \"./enums\";\nexport * from \"./client/rxjs/useRxjs\";\nexport * from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport * from \"./server/controller\";\nexport * from \"./server/service\";\nexport * from \"./server/base/base.database\";\nexport * from \"./server/bun/router/controller-discovery\";\nexport * from \"./server/bun/router/routes\";\nexport * from \"./server/bun/router/router\";\nexport * from \"./server/bun/router/router.internal\";\nexport * from \"./server/bun/websocket/Websocket\";\nexport * from \"./server/bun/websocket/websocket.guards\";\nexport * from \"./server/bun/websocket/Message\";\nexport * from \"./server/bun/websocket/Channel\";\nexport * from \"./server/bun/websocket/Client\";\nexport * from \"./server/bun/websocket/websocket.enums\";\nexport * from \"./server/bun/websocket/websocket.types\";\nexport * from \"./utils/Rxjs\";\nexport * from \"./utils/Lib\";\nexport * from \"./utils/Guards\";\nexport * from \"./utils/BaseEntity\";\nexport * from \"./utils/Console\";\nexport * from \"./utils/BaseDto\";\n\n// Export default classes\nexport { default as Singleton } from \"./singleton\";\nexport { default as Initializable } from \"./initializable\";\nexport { default as Application } from \"./application\";\nexport { default as Throwable } from \"./throwable\";\nexport { default as TopsydeUtilsVitePlugin } from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport { default as Controller } from \"./server/controller\";\nexport { default as Service } from \"./server/service\";\nexport { default as Database } from \"./server/base/base.database\";\nexport { default as Router } from \"./server/bun/router/router\";\nexport { default as Router_Internal } from \"./server/bun/router/router.internal\";\nexport { default as Websocket } from \"./server/bun/websocket/Websocket\";\nexport { default as Message } from \"./server/bun/websocket/Message\";\nexport { default as Channel } from \"./server/bun/websocket/Channel\";\nexport { default as Client } from \"./server/bun/websocket/Client\";\nexport { default as Lib } from \"./utils/Lib\";\nexport { default as Guards } from \"./utils/Guards\";\nexport { default as BaseEntity } from \"./utils/BaseEntity\";\nexport { default as Console } from \"./utils/Console\";\n\n// Re-export specific items for backward compatibility\nexport { ERROR_CODE, HTTP_ERROR_CODE, WS_ERROR_CODE } from \"./errors\";\nexport { InitializableOptions, InitializableEvent } from \"./initializable\";\nexport { RESPONSE_INIT, HEADERS_INIT, RESPONSE_METHOD_OPTIONS } from \"./application\";\nexport { DEFAULT_FALSE_RESPONSE, LOG_COLORS, LOG_ICONS } from \"./consts\";\nexport { ClassConstructor, NonNullableType, ObjectKeys, KVObj, I_ApplicationResponse } from \"./types\";\nexport { E_IS, E_ENVIRONMENTS } from \"./enums\";\nexport { RxjsDataType, NamespaceActions, MultiNamespaceActions } from \"./client/rxjs/useRxjs\";\nexport { ControllerResponse, ControllerAction, ControllerMap, ControllerOptions } from \"./server/controller\";\nexport { Routes } from \"./server/bun/router/routes\";\nexport { WebsocketConstructorOptions, I_WebsocketConstructor } from \"./server/bun/websocket/Websocket\";\nexport { E_WebsocketMessageType, E_WebsocketMessagePriority } from \"./server/bun/websocket/websocket.enums\";\nexport {\n\tBunWebsocketMessage,\n\tWebsocketChannel,\n\tWebsocketClients,\n\tWebsocketMessageOptions,\n\tWebsocketMessage,\n\tWebsocketStructuredMessage,\n\tWebsocketEntityId,\n\tWebsocketEntityName,\n\tWebsocketEntityData,\n\tI_WebsocketEntity,\n\tI_WebsocketClient,\n\tI_WebsocketChannelEntity,\n\tBroadcastOptions,\n\tI_WebsocketChannel,\n\tWebsocketInterfaceHandlers,\n\tI_WebsocketInterface,\n} from \"./server/bun/websocket/websocket.types\";\nexport { E_SUBJET_TYPE, I_RxjsPayload, RxjsNamespaces, AsyncSubject, BehaviorSubject, ReplaySubject, Subject, Subscription } from \"./utils/Rxjs\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,qBAAqB;AACrB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,uBAAuB,CAAC;AACtC,cAAc,8CAA8C,CAAC;AAC7D,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0CAA0C,CAAC;AACzD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAyC,CAAC;AACxD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc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AAI,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAErD,sDAAsD;AACtD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEtE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACrF,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAK/C,OAAO,EAAE,sBAAsB,EAAE,0BAA0B,EAAE,MAAM,wCAAwC,CAAC;AAmB5G,OAAO,EAAE,aAAa,EAAiC,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\n// Export all modules\nexport * from \"./errors\";\nexport * from \"./singleton\";\nexport * from \"./initializable\";\nexport * from \"./application\";\nexport * from \"./consts\";\nexport * from \"./types\";\nexport * from \"./throwable\";\nexport * from \"./enums\";\nexport * from \"./client/rxjs/useRxjs\";\nexport * from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport * from \"./server/controller\";\nexport * from \"./server/service\";\nexport * from \"./server/base/base.database\";\nexport * from \"./server/bun/router/controller-discovery\";\nexport * from \"./server/bun/router/routes\";\nexport * from \"./server/bun/router/router\";\nexport * from \"./server/bun/router/router.internal\";\nexport * from \"./server/bun/websocket/Websocket\";\nexport * from \"./server/bun/websocket/websocket.guards\";\nexport * from \"./server/bun/websocket/Message\";\nexport * from \"./server/bun/websocket/Channel\";\nexport * from \"./server/bun/websocket/Client\";\nexport * from \"./server/bun/websocket/websocket.enums\";\nexport * from \"./server/bun/websocket/websocket.types\";\nexport * from \"./utils/Rxjs\";\nexport * from \"./utils/Lib\";\nexport * from \"./utils/Guards\";\nexport * from \"./utils/BaseEntity\";\nexport * from \"./utils/Console\";\nexport * from \"./utils/BaseDto\";\nexport * from \"./utils/dto_validators/IsNumberOrRangeConstraint\";\n\n// Export default classes\nexport { default as Singleton } from \"./singleton\";\nexport { default as Initializable } from \"./initializable\";\nexport { default as Application } from \"./application\";\nexport { default as Throwable } from \"./throwable\";\nexport { default as TopsydeUtilsVitePlugin } from \"./client/vite/plugins/topsydeUtilsVitePlugin\";\nexport { default as Controller } from \"./server/controller\";\nexport { default as Service } from \"./server/service\";\nexport { default as Database } from \"./server/base/base.database\";\nexport { default as Router } from \"./server/bun/router/router\";\nexport { default as Router_Internal } from \"./server/bun/router/router.internal\";\nexport { default as Websocket } from \"./server/bun/websocket/Websocket\";\nexport { default as Message } from \"./server/bun/websocket/Message\";\nexport { default as Channel } from \"./server/bun/websocket/Channel\";\nexport { default as Client } from \"./server/bun/websocket/Client\";\nexport { default as Lib } from \"./utils/Lib\";\nexport { default as Guards } from \"./utils/Guards\";\nexport { default as BaseEntity } from \"./utils/BaseEntity\";\nexport { default as Console } from \"./utils/Console\";\n\n// Re-export specific items for backward compatibility\nexport { ERROR_CODE, HTTP_ERROR_CODE, WS_ERROR_CODE } from \"./errors\";\nexport { InitializableOptions, InitializableEvent } from \"./initializable\";\nexport { RESPONSE_INIT, HEADERS_INIT, RESPONSE_METHOD_OPTIONS } from \"./application\";\nexport { DEFAULT_FALSE_RESPONSE, LOG_COLORS, LOG_ICONS } from \"./consts\";\nexport { ClassConstructor, NonNullableType, ObjectKeys, KVObj, I_ApplicationResponse } from \"./types\";\nexport { E_IS, E_ENVIRONMENTS } from \"./enums\";\nexport { RxjsDataType, NamespaceActions, MultiNamespaceActions } from \"./client/rxjs/useRxjs\";\nexport { ControllerResponse, ControllerAction, ControllerMap, ControllerOptions } from \"./server/controller\";\nexport { Routes } from \"./server/bun/router/routes\";\nexport { WebsocketConstructorOptions, I_WebsocketConstructor } from \"./server/bun/websocket/Websocket\";\nexport { E_WebsocketMessageType, E_WebsocketMessagePriority } from \"./server/bun/websocket/websocket.enums\";\nexport {\n\tBunWebsocketMessage,\n\tWebsocketChannel,\n\tWebsocketClients,\n\tWebsocketMessageOptions,\n\tWebsocketMessage,\n\tWebsocketStructuredMessage,\n\tWebsocketEntityId,\n\tWebsocketEntityName,\n\tWebsocketEntityData,\n\tI_WebsocketEntity,\n\tI_WebsocketClient,\n\tI_WebsocketChannelEntity,\n\tBroadcastOptions,\n\tI_WebsocketChannel,\n\tWebsocketInterfaceHandlers,\n\tI_WebsocketInterface,\n} from \"./server/bun/websocket/websocket.types\";\nexport { E_SUBJET_TYPE, I_RxjsPayload, RxjsNamespaces, AsyncSubject, BehaviorSubject, ReplaySubject, Subject, Subscription } from \"./utils/Rxjs\";\n"]}
@@ -1,13 +1,5 @@
1
1
  import type { ClassConstructor, ClassTransformOptions } from "class-transformer";
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
- }
2
+ import { ValidationError } from "class-validator";
11
3
  /**
12
4
  * Base typesafe class for Data Transfer Objects (DTOs)
13
5
  */
@@ -28,20 +20,15 @@ export declare abstract class Dto {
28
20
  */
29
21
  toJSON<T = Record<string, unknown>>(include_undefined?: boolean, options?: ClassTransformOptions): T;
30
22
  /**
31
- * Creates a new instance of the DTO with validation
32
- * @param cls - The class constructor to create an instance from
23
+ * Creates a new instance of the DTO with validation (infers class from `this`)
33
24
  * @param data - Data to create the DTO from
34
- * @param options - Class transformer options for controlling exposure and transformation
25
+ * @param options - Class transformer options
35
26
  * @returns New instance of the DTO
36
- * @throws ValidationError[] if validation fails and validate is true
37
27
  */
38
- static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options?: ClassTransformOptions): T;
28
+ static Create<T extends Dto>(this: ClassConstructor<T>, // 🔑 Key change: use 'this' parameter
29
+ data: Record<string, unknown>, options?: ClassTransformOptions): T;
39
30
  /**
40
31
  * Creates an array of DTOs from an array of plain objects
41
- * @param cls - The class constructor to create instances from
42
- * @param dataArray - Array of data to create DTOs from
43
- * @param options - Class transformer options for controlling exposure and transformation
44
- * @returns Array of DTO instances
45
32
  */
46
- static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options?: ClassTransformOptions): T[];
33
+ static CreateMany<T extends Dto>(this: ClassConstructor<T>, dataArray: Record<string, unknown>[], options?: ClassTransformOptions): T[];
47
34
  }
@@ -1,89 +1,6 @@
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
- };
39
1
  import { instanceToPlain, plainToInstance } from "class-transformer";
40
2
  import Guards from "./Guards.js";
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 };
3
+ import { validateSync } from "class-validator";
87
4
  /**
88
5
  * Base typesafe class for Data Transfer Objects (DTOs)
89
6
  */
@@ -118,15 +35,15 @@ export class Dto {
118
35
  return value;
119
36
  }
120
37
  /**
121
- * Creates a new instance of the DTO with validation
122
- * @param cls - The class constructor to create an instance from
38
+ * Creates a new instance of the DTO with validation (infers class from `this`)
123
39
  * @param data - Data to create the DTO from
124
- * @param options - Class transformer options for controlling exposure and transformation
40
+ * @param options - Class transformer options
125
41
  * @returns New instance of the DTO
126
- * @throws ValidationError[] if validation fails and validate is true
127
42
  */
128
- static Create(cls, data, options = {}) {
129
- const instance = plainToInstance(cls, data, {
43
+ static Create(// 🔑 Key change: use 'this' parameter
44
+ data, options = {}) {
45
+ const instance = plainToInstance(this, data, {
46
+ // 🔑 Use 'this' instead of 'cls'
130
47
  ...Dto.defaultTransformOptions,
131
48
  ...options,
132
49
  });
@@ -134,13 +51,12 @@ export class Dto {
134
51
  }
135
52
  /**
136
53
  * Creates an array of DTOs from an array of plain objects
137
- * @param cls - The class constructor to create instances from
138
- * @param dataArray - Array of data to create DTOs from
139
- * @param options - Class transformer options for controlling exposure and transformation
140
- * @returns Array of DTO instances
141
54
  */
142
- static CreateMany(cls, dataArray, options = {}) {
143
- return dataArray.map((data) => Dto.Create(cls, data, options));
55
+ static CreateMany(dataArray, options = {}) {
56
+ return plainToInstance(this, dataArray, {
57
+ ...Dto.defaultTransformOptions,
58
+ ...options,
59
+ });
144
60
  }
145
61
  }
146
62
  /**
@@ -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,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,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC;oBAAE,OAAO,KAAK,CAAC;gBACtC,OAAO,IAAI,CAAC;YACb,CAAC;YAED,OAAO,KAA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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"]}
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,EAAmB,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEhE;;GAEG;AACH,MAAM,OAAgB,GAAG;IAWxB;;;OAGG;IACI,QAAQ;QACd,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE;YACjC,eAAe,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE;YAClC,mBAAmB,EAAE,IAAI;SACzB,CAAC,CAAC;QACH,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACvB,MAAM,MAAM,CAAC;QACd,CAAC;QACD,OAAO,MAAM,CAAC;IACf,CAAC;IAED;;;;OAIG;IACI,MAAM,CAA8B,oBAA6B,IAAI,EAAE,OAA+B;QAC5G,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,EAAE;YACnC,GAAG,GAAG,CAAC,uBAAuB;YAC9B,GAAG,OAAO;SACV,CAAM,CAAC;QAER,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACxB,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAM,CAAC;QACvH,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CACQ,sCAAsC;IACjE,IAA6B,EAC7B,UAAiC,EAAE;QAEnC,MAAM,QAAQ,GAAG,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE;YAC5C,iCAAiC;YACjC,GAAG,GAAG,CAAC,uBAAuB;YAC9B,GAAG,OAAO;SACV,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC;IACjB,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,UAAU,CAA2C,SAAoC,EAAE,UAAiC,EAAE;QAC3I,OAAO,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE;YACvC,GAAG,GAAG,CAAC,uBAAuB;YAC9B,GAAG,OAAO;SACV,CAAQ,CAAC;IACX,CAAC;;AAvED;;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 { ValidationError, validateSync } from \"class-validator\";\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 (infers class from `this`)\n\t * @param data - Data to create the DTO from\n\t * @param options - Class transformer options\n\t * @returns New instance of the DTO\n\t */\n\tpublic static Create<T extends Dto>(\n\t\tthis: ClassConstructor<T>, // 🔑 Key change: use 'this' parameter\n\t\tdata: Record<string, unknown>,\n\t\toptions: ClassTransformOptions = {},\n\t): T {\n\t\tconst instance = plainToInstance(this, data, {\n\t\t\t// 🔑 Use 'this' instead of 'cls'\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 */\n\tpublic static CreateMany<T extends Dto>(this: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {\n\t\treturn plainToInstance(this, dataArray, {\n\t\t\t...Dto.defaultTransformOptions,\n\t\t\t...options,\n\t\t}) as T[];\n\t}\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import { ValidatorConstraintInterface, ValidationArguments } from "class-validator";
2
+ /**
3
+ * Custom validator for number | number[] type
4
+ * Validates that value is either a single number or a 2-element number array [min, max]
5
+ */
6
+ export declare class IsNumberOrRangeConstraint implements ValidatorConstraintInterface {
7
+ validate(value: any, args: ValidationArguments): boolean;
8
+ defaultMessage(args: ValidationArguments): string;
9
+ }
@@ -0,0 +1,85 @@
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
+ };
39
+ import { ValidatorConstraint } from "class-validator";
40
+ /**
41
+ * Custom validator for number | number[] type
42
+ * Validates that value is either a single number or a 2-element number array [min, max]
43
+ */
44
+ let IsNumberOrRangeConstraint = (() => {
45
+ let _classDecorators = [ValidatorConstraint({ name: "isNumberOrRange", async: false })];
46
+ let _classDescriptor;
47
+ let _classExtraInitializers = [];
48
+ let _classThis;
49
+ var IsNumberOrRangeConstraint = _classThis = class {
50
+ validate(value, args) {
51
+ // Allow single number
52
+ if (typeof value === "number" && !isNaN(value)) {
53
+ return true;
54
+ }
55
+ // Allow array of exactly 2 numbers (range)
56
+ if (Array.isArray(value)) {
57
+ if (value.length !== 2)
58
+ return false;
59
+ if (typeof value[0] !== "number" || isNaN(value[0]))
60
+ return false;
61
+ if (typeof value[1] !== "number" || isNaN(value[1]))
62
+ return false;
63
+ // Optional: Validate min <= max
64
+ if (value[0] > value[1])
65
+ return false;
66
+ return true;
67
+ }
68
+ return false;
69
+ }
70
+ defaultMessage(args) {
71
+ return "Value must be a number or a 2-element number array [min, max]";
72
+ }
73
+ };
74
+ __setFunctionName(_classThis, "IsNumberOrRangeConstraint");
75
+ (() => {
76
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
77
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
78
+ IsNumberOrRangeConstraint = _classThis = _classDescriptor.value;
79
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
80
+ __runInitializers(_classThis, _classExtraInitializers);
81
+ })();
82
+ return IsNumberOrRangeConstraint = _classThis;
83
+ })();
84
+ export { IsNumberOrRangeConstraint };
85
+ //# sourceMappingURL=IsNumberOrRangeConstraint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IsNumberOrRangeConstraint.js","sourceRoot":"","sources":["../../../src/utils/dto_validators/IsNumberOrRangeConstraint.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,mBAAmB,EAAqD,MAAM,iBAAiB,CAAC;AAEzG;;;GAGG;IAGU,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,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC;oBAAE,OAAO,KAAK,CAAC;gBACtC,OAAO,IAAI,CAAC;YACb,CAAC;YAED,OAAO,KAAK,CAAC;QACd,CAAC;QAED,cAAc,CAAC,IAAyB;YACvC,OAAO,+DAA+D,CAAC;QACxE,CAAC;;;;;QAtBF,6KAuBC;;;QAvBY,uDAAyB;;;;SAAzB,yBAAyB","sourcesContent":["import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } 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\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"]}
@@ -0,0 +1 @@
1
+ export * from './IsNumberOrRangeConstraint';
@@ -0,0 +1,4 @@
1
+ // This file is auto-generated by scripts/generate-indexes.ts
2
+ // Do not edit this file directly
3
+ export * from './IsNumberOrRangeConstraint.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/dto_validators/index.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,iCAAiC;AAEjC,cAAc,6BAA6B,CAAC","sourcesContent":["// This file is auto-generated by scripts/generate-indexes.ts\n// Do not edit this file directly\n\nexport * from './IsNumberOrRangeConstraint';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topsyde-utils",
3
- "version": "1.0.201",
3
+ "version": "1.0.203",
4
4
  "description": "A bundle of TypeScript utility classes and functions",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -52,9 +52,9 @@
52
52
  "types": "./dist/server/bun/websocket/index.d.ts",
53
53
  "default": "./dist/server/bun/websocket/index.js"
54
54
  },
55
- "./utils": {
56
- "types": "./dist/utils/index.d.ts",
57
- "default": "./dist/utils/index.js"
55
+ "./utils/dto_validators": {
56
+ "types": "./dist/utils/dto_validators/index.d.ts",
57
+ "default": "./dist/utils/dto_validators/index.js"
58
58
  }
59
59
  },
60
60
  "typesVersions": {
@@ -74,8 +74,8 @@
74
74
  "server/bun/websocket": [
75
75
  "./dist/server/bun/websocket/index.d.ts"
76
76
  ],
77
- "utils": [
78
- "./dist/utils/index.d.ts"
77
+ "utils/dto_validators": [
78
+ "./dist/utils/dto_validators/index.d.ts"
79
79
  ]
80
80
  }
81
81
  },
package/src/index.ts CHANGED
@@ -32,6 +32,7 @@ export * from "./utils/Guards";
32
32
  export * from "./utils/BaseEntity";
33
33
  export * from "./utils/Console";
34
34
  export * from "./utils/BaseDto";
35
+ export * from "./utils/dto_validators/IsNumberOrRangeConstraint";
35
36
 
36
37
  // Export default classes
37
38
  export { default as Singleton } from "./singleton";
@@ -1,37 +1,7 @@
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 { 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
- }
4
+ import { ValidationError, validateSync } from "class-validator";
35
5
 
36
6
  /**
37
7
  * Base typesafe class for Data Transfer Objects (DTOs)
@@ -81,15 +51,18 @@ export abstract class Dto {
81
51
  }
82
52
 
83
53
  /**
84
- * Creates a new instance of the DTO with validation
85
- * @param cls - The class constructor to create an instance from
54
+ * Creates a new instance of the DTO with validation (infers class from `this`)
86
55
  * @param data - Data to create the DTO from
87
- * @param options - Class transformer options for controlling exposure and transformation
56
+ * @param options - Class transformer options
88
57
  * @returns New instance of the DTO
89
- * @throws ValidationError[] if validation fails and validate is true
90
58
  */
91
- public static Create<T extends Dto>(cls: ClassConstructor<T>, data: Record<string, unknown>, options: ClassTransformOptions = {}): T {
92
- const instance = plainToInstance(cls, data, {
59
+ public static Create<T extends Dto>(
60
+ this: ClassConstructor<T>, // 🔑 Key change: use 'this' parameter
61
+ data: Record<string, unknown>,
62
+ options: ClassTransformOptions = {},
63
+ ): T {
64
+ const instance = plainToInstance(this, data, {
65
+ // 🔑 Use 'this' instead of 'cls'
93
66
  ...Dto.defaultTransformOptions,
94
67
  ...options,
95
68
  });
@@ -99,12 +72,11 @@ export abstract class Dto {
99
72
 
100
73
  /**
101
74
  * Creates an array of DTOs from an array of plain objects
102
- * @param cls - The class constructor to create instances from
103
- * @param dataArray - Array of data to create DTOs from
104
- * @param options - Class transformer options for controlling exposure and transformation
105
- * @returns Array of DTO instances
106
75
  */
107
- public static CreateMany<T extends Dto>(cls: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {
108
- return dataArray.map((data) => Dto.Create(cls, data, options));
76
+ public static CreateMany<T extends Dto>(this: ClassConstructor<T>, dataArray: Record<string, unknown>[], options: ClassTransformOptions = {}): T[] {
77
+ return plainToInstance(this, dataArray, {
78
+ ...Dto.defaultTransformOptions,
79
+ ...options,
80
+ }) as T[];
109
81
  }
110
82
  }
@@ -0,0 +1,32 @@
1
+ import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from "class-validator";
2
+
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
+
8
+ @ValidatorConstraint({ name: "isNumberOrRange", async: false })
9
+ export class IsNumberOrRangeConstraint implements ValidatorConstraintInterface {
10
+ validate(value: any, args: ValidationArguments) {
11
+ // Allow single number
12
+ if (typeof value === "number" && !isNaN(value)) {
13
+ return true;
14
+ }
15
+
16
+ // Allow array of exactly 2 numbers (range)
17
+ if (Array.isArray(value)) {
18
+ if (value.length !== 2) return false;
19
+ if (typeof value[0] !== "number" || isNaN(value[0])) return false;
20
+ if (typeof value[1] !== "number" || isNaN(value[1])) return false;
21
+ // Optional: Validate min <= max
22
+ if (value[0] > value[1]) return false;
23
+ return true;
24
+ }
25
+
26
+ return false;
27
+ }
28
+
29
+ defaultMessage(args: ValidationArguments) {
30
+ return "Value must be a number or a 2-element number array [min, max]";
31
+ }
32
+ }
@@ -0,0 +1,4 @@
1
+ // This file is auto-generated by scripts/generate-indexes.ts
2
+ // Do not edit this file directly
3
+
4
+ export * from './IsNumberOrRangeConstraint';