@decaf-ts/decorator-validation 1.20.0 → 1.21.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.
Files changed (175) hide show
  1. package/README.md +1 -1
  2. package/dist/decorator-validation.cjs +1 -1
  3. package/dist/decorator-validation.cjs.map +1 -1
  4. package/dist/decorator-validation.js +1 -1
  5. package/dist/decorator-validation.js.map +1 -1
  6. package/lib/cjs/index.cjs +2 -1
  7. package/lib/cjs/index.cjs.map +1 -1
  8. package/lib/esm/index.js +2 -1
  9. package/lib/esm/index.js.map +1 -1
  10. package/lib/types/constants/index.d.cts +2 -2
  11. package/lib/types/constants/index.d.mts +2 -2
  12. package/lib/types/index.d.cts +8 -7
  13. package/lib/types/index.d.mts +8 -7
  14. package/lib/types/model/Builder.d.cts +3 -3
  15. package/lib/types/model/Builder.d.mts +3 -3
  16. package/lib/types/model/Model.d.cts +4 -4
  17. package/lib/types/model/Model.d.mts +4 -4
  18. package/lib/types/model/ModelErrorDefinition.d.cts +1 -1
  19. package/lib/types/model/ModelErrorDefinition.d.mts +1 -1
  20. package/lib/types/model/ModelRegistry.d.cts +3 -3
  21. package/lib/types/model/ModelRegistry.d.mts +3 -3
  22. package/lib/types/model/construction.d.cts +1 -1
  23. package/lib/types/model/construction.d.mts +1 -1
  24. package/lib/types/model/index.d.cts +10 -10
  25. package/lib/types/model/index.d.mts +10 -10
  26. package/lib/types/model/types.d.cts +3 -3
  27. package/lib/types/model/types.d.mts +3 -3
  28. package/lib/types/model/utils.d.cts +1 -1
  29. package/lib/types/model/utils.d.mts +1 -1
  30. package/lib/types/model/validation.d.cts +3 -3
  31. package/lib/types/model/validation.d.mts +3 -3
  32. package/lib/types/overrides/Metadata.d.cts +3 -3
  33. package/lib/types/overrides/Metadata.d.mts +3 -3
  34. package/lib/types/overrides/index.d.cts +3 -3
  35. package/lib/types/overrides/index.d.mts +3 -3
  36. package/lib/types/overrides/types.d.cts +1 -1
  37. package/lib/types/overrides/types.d.mts +1 -1
  38. package/lib/types/types/index.d.cts +1 -1
  39. package/lib/types/types/index.d.mts +1 -1
  40. package/lib/types/types/validation.d.cts +2 -2
  41. package/lib/types/types/validation.d.mts +2 -2
  42. package/lib/types/utils/index.d.cts +11 -11
  43. package/lib/types/utils/index.d.mts +11 -11
  44. package/lib/types/utils/serialization.d.cts +1 -1
  45. package/lib/types/utils/serialization.d.mts +1 -1
  46. package/lib/types/utils/serializers.d.cts +2 -2
  47. package/lib/types/utils/serializers.d.mts +2 -2
  48. package/lib/types/utils/types.d.cts +1 -1
  49. package/lib/types/utils/types.d.mts +1 -1
  50. package/lib/types/validation/Validation.d.cts +2 -2
  51. package/lib/types/validation/Validation.d.mts +2 -2
  52. package/lib/types/validation/Validators/AsyncValidator.d.cts +3 -3
  53. package/lib/types/validation/Validators/AsyncValidator.d.mts +3 -3
  54. package/lib/types/validation/Validators/BaseValidator.d.cts +3 -3
  55. package/lib/types/validation/Validators/BaseValidator.d.mts +3 -3
  56. package/lib/types/validation/Validators/DateValidator.d.cts +2 -2
  57. package/lib/types/validation/Validators/DateValidator.d.mts +2 -2
  58. package/lib/types/validation/Validators/DiffValidator.d.cts +3 -3
  59. package/lib/types/validation/Validators/DiffValidator.d.mts +3 -3
  60. package/lib/types/validation/Validators/EmailValidator.d.cts +2 -2
  61. package/lib/types/validation/Validators/EmailValidator.d.mts +2 -2
  62. package/lib/types/validation/Validators/EqualsValidator.d.cts +3 -3
  63. package/lib/types/validation/Validators/EqualsValidator.d.mts +3 -3
  64. package/lib/types/validation/Validators/GreaterThanOrEqualValidator.d.cts +3 -3
  65. package/lib/types/validation/Validators/GreaterThanOrEqualValidator.d.mts +3 -3
  66. package/lib/types/validation/Validators/GreaterThanValidator.d.cts +3 -3
  67. package/lib/types/validation/Validators/GreaterThanValidator.d.mts +3 -3
  68. package/lib/types/validation/Validators/LessThanOrEqualValidator.d.cts +3 -3
  69. package/lib/types/validation/Validators/LessThanOrEqualValidator.d.mts +3 -3
  70. package/lib/types/validation/Validators/LessThanValidator.d.cts +3 -3
  71. package/lib/types/validation/Validators/LessThanValidator.d.mts +3 -3
  72. package/lib/types/validation/Validators/ListValidator.d.cts +2 -2
  73. package/lib/types/validation/Validators/ListValidator.d.mts +2 -2
  74. package/lib/types/validation/Validators/MaxLengthValidator.d.cts +2 -2
  75. package/lib/types/validation/Validators/MaxLengthValidator.d.mts +2 -2
  76. package/lib/types/validation/Validators/MaxValidator.d.cts +2 -2
  77. package/lib/types/validation/Validators/MaxValidator.d.mts +2 -2
  78. package/lib/types/validation/Validators/MinLengthValidator.d.cts +2 -2
  79. package/lib/types/validation/Validators/MinLengthValidator.d.mts +2 -2
  80. package/lib/types/validation/Validators/MinValidator.d.cts +2 -2
  81. package/lib/types/validation/Validators/MinValidator.d.mts +2 -2
  82. package/lib/types/validation/Validators/OptionValidator.d.cts +2 -2
  83. package/lib/types/validation/Validators/OptionValidator.d.mts +2 -2
  84. package/lib/types/validation/Validators/PasswordValidator.d.cts +2 -2
  85. package/lib/types/validation/Validators/PasswordValidator.d.mts +2 -2
  86. package/lib/types/validation/Validators/PatternValidator.d.cts +2 -2
  87. package/lib/types/validation/Validators/PatternValidator.d.mts +2 -2
  88. package/lib/types/validation/Validators/RequiredValidator.d.cts +2 -2
  89. package/lib/types/validation/Validators/RequiredValidator.d.mts +2 -2
  90. package/lib/types/validation/Validators/StepValidator.d.cts +2 -2
  91. package/lib/types/validation/Validators/StepValidator.d.mts +2 -2
  92. package/lib/types/validation/Validators/TypeValidator.d.cts +2 -2
  93. package/lib/types/validation/Validators/TypeValidator.d.mts +2 -2
  94. package/lib/types/validation/Validators/URLValidator.d.cts +2 -2
  95. package/lib/types/validation/Validators/URLValidator.d.mts +2 -2
  96. package/lib/types/validation/Validators/Validator.d.cts +3 -3
  97. package/lib/types/validation/Validators/Validator.d.mts +3 -3
  98. package/lib/types/validation/Validators/ValidatorRegistry.d.cts +3 -3
  99. package/lib/types/validation/Validators/ValidatorRegistry.d.mts +3 -3
  100. package/lib/types/validation/Validators/decorators.d.cts +1 -1
  101. package/lib/types/validation/Validators/decorators.d.mts +1 -1
  102. package/lib/types/validation/Validators/index.d.cts +26 -26
  103. package/lib/types/validation/Validators/index.d.mts +26 -26
  104. package/lib/types/validation/decorators.d.cts +2 -2
  105. package/lib/types/validation/decorators.d.mts +2 -2
  106. package/lib/types/validation/index.d.cts +4 -4
  107. package/lib/types/validation/index.d.mts +4 -4
  108. package/lib/types/validation/types.d.cts +2 -2
  109. package/lib/types/validation/types.d.mts +2 -2
  110. package/package.json +9 -1
  111. package/lib/types/constants/errors.d.ts +0 -17
  112. package/lib/types/constants/index.d.ts +0 -2
  113. package/lib/types/constants/validation.d.ts +0 -16
  114. package/lib/types/index.d.ts +0 -20
  115. package/lib/types/model/Builder.d.ts +0 -86
  116. package/lib/types/model/Model.d.ts +0 -286
  117. package/lib/types/model/ModelErrorDefinition.d.ts +0 -22
  118. package/lib/types/model/ModelRegistry.d.ts +0 -181
  119. package/lib/types/model/constants.d.ts +0 -65
  120. package/lib/types/model/construction.d.ts +0 -46
  121. package/lib/types/model/decorators.d.ts +0 -47
  122. package/lib/types/model/index.d.ts +0 -10
  123. package/lib/types/model/types.d.ts +0 -145
  124. package/lib/types/model/utils.d.ts +0 -13
  125. package/lib/types/model/validation.d.ts +0 -92
  126. package/lib/types/overrides/Metadata.d.ts +0 -132
  127. package/lib/types/overrides/index.d.ts +0 -4
  128. package/lib/types/overrides/overrides.d.ts +0 -1
  129. package/lib/types/overrides/types.d.ts +0 -17
  130. package/lib/types/types/index.d.ts +0 -1
  131. package/lib/types/types/validation.d.ts +0 -28
  132. package/lib/types/utils/DateBuilder.d.ts +0 -121
  133. package/lib/types/utils/PathProxy.d.ts +0 -43
  134. package/lib/types/utils/constants.d.ts +0 -32
  135. package/lib/types/utils/dates.d.ts +0 -86
  136. package/lib/types/utils/equality.d.ts +0 -56
  137. package/lib/types/utils/hashing.d.ts +0 -82
  138. package/lib/types/utils/index.d.ts +0 -11
  139. package/lib/types/utils/registry.d.ts +0 -68
  140. package/lib/types/utils/serialization.d.ts +0 -12
  141. package/lib/types/utils/serializers.d.ts +0 -41
  142. package/lib/types/utils/strings.d.ts +0 -25
  143. package/lib/types/utils/types.d.ts +0 -35
  144. package/lib/types/validation/Validation.d.ts +0 -53
  145. package/lib/types/validation/Validators/AsyncValidator.d.ts +0 -72
  146. package/lib/types/validation/Validators/BaseValidator.d.ts +0 -118
  147. package/lib/types/validation/Validators/DateValidator.d.ts +0 -60
  148. package/lib/types/validation/Validators/DiffValidator.d.ts +0 -29
  149. package/lib/types/validation/Validators/EmailValidator.d.ts +0 -60
  150. package/lib/types/validation/Validators/EqualsValidator.d.ts +0 -29
  151. package/lib/types/validation/Validators/GreaterThanOrEqualValidator.d.ts +0 -29
  152. package/lib/types/validation/Validators/GreaterThanValidator.d.ts +0 -29
  153. package/lib/types/validation/Validators/LessThanOrEqualValidator.d.ts +0 -29
  154. package/lib/types/validation/Validators/LessThanValidator.d.ts +0 -29
  155. package/lib/types/validation/Validators/ListValidator.d.ts +0 -66
  156. package/lib/types/validation/Validators/MaxLengthValidator.d.ts +0 -29
  157. package/lib/types/validation/Validators/MaxValidator.d.ts +0 -74
  158. package/lib/types/validation/Validators/MinLengthValidator.d.ts +0 -29
  159. package/lib/types/validation/Validators/MinValidator.d.ts +0 -74
  160. package/lib/types/validation/Validators/OptionValidator.d.ts +0 -29
  161. package/lib/types/validation/Validators/PasswordValidator.d.ts +0 -28
  162. package/lib/types/validation/Validators/PatternValidator.d.ts +0 -103
  163. package/lib/types/validation/Validators/RequiredValidator.d.ts +0 -74
  164. package/lib/types/validation/Validators/StepValidator.d.ts +0 -29
  165. package/lib/types/validation/Validators/TypeValidator.d.ts +0 -79
  166. package/lib/types/validation/Validators/URLValidator.d.ts +0 -61
  167. package/lib/types/validation/Validators/Validator.d.ts +0 -65
  168. package/lib/types/validation/Validators/ValidatorRegistry.d.ts +0 -41
  169. package/lib/types/validation/Validators/constants.d.ts +0 -157
  170. package/lib/types/validation/Validators/decorators.d.ts +0 -12
  171. package/lib/types/validation/Validators/index.d.ts +0 -26
  172. package/lib/types/validation/Validators/utils.d.ts +0 -75
  173. package/lib/types/validation/decorators.d.ts +0 -313
  174. package/lib/types/validation/index.d.ts +0 -9
  175. package/lib/types/validation/types.d.ts +0 -309
@@ -1,286 +0,0 @@
1
- import { BuilderRegistry } from "../utils/registry";
2
- import { ModelErrorDefinition } from "./ModelErrorDefinition";
3
- import { Comparable, Comparison, Hashable, ModelArg, ModelBuilderFunction, ModelConstructor, Serializable, Validatable } from "./types";
4
- import { ConditionalAsync } from "../types";
5
- import { Constructor } from "@decaf-ts/decoration";
6
- /**
7
- * @summary Abstract class representing a Validatable Model object
8
- * @description Meant to be used as a base class for all Model classes
9
- *
10
- * Model objects must:
11
- * - Have all their required properties marked with '!';
12
- * - Have all their optional properties marked as '?':
13
- *
14
- * @param {ModelArg<Model>} model base object from which to populate properties from
15
- *
16
- * @class Model
17
- * @category Model
18
- * @abstract
19
- * @implements Validatable
20
- * @implements Serializable
21
- *
22
- * @example
23
- * class ClassName {
24
- * @required()
25
- * requiredPropertyName!: PropertyType;
26
- *
27
- * optionalPropertyName?: PropertyType;
28
- * }
29
- */
30
- export declare abstract class Model<Async extends boolean = false> implements Validatable<Async>, Serializable, Hashable, Comparable {
31
- protected constructor(arg?: ModelArg<Model> | undefined);
32
- isAsync(): boolean;
33
- /**
34
- * @description Validates the model object against its defined validation rules
35
- * @summary Validates the object according to its decorated properties, returning any validation errors
36
- *
37
- * @param {any[]} [exceptions] - Properties in the object to be ignored for the validation. Marked as 'any' to allow for extension but expects strings
38
- * @return {ModelErrorDefinition | undefined} - Returns a ModelErrorDefinition object if validation errors exist, otherwise undefined
39
- */
40
- hasErrors(...exceptions: any[]): ConditionalAsync<Async, ModelErrorDefinition | undefined>;
41
- /**
42
- * @description Determines if this model is equal to another object
43
- * @summary Compare object equality recursively, checking all properties unless excluded
44
- *
45
- * @param {any} obj - Object to compare to
46
- * @param {string[]} [exceptions] - Property names to be excluded from the comparison
47
- * @return {boolean} - True if objects are equal, false otherwise
48
- */
49
- equals(obj: any, ...exceptions: string[]): boolean;
50
- compare<M extends Model>(this: M, other: M, ...exceptions: (keyof M)[]): Comparison<M> | undefined;
51
- /**
52
- * @description Converts the model to a serialized string representation
53
- * @summary Returns the serialized model according to the currently defined {@link Serializer}
54
- *
55
- * @return {string} - The serialized string representation of the model
56
- */
57
- serialize(): string;
58
- /**
59
- * @description Provides a human-readable string representation of the model
60
- * @summary Override the implementation for js's 'toString()' to provide a more useful representation
61
- *
62
- * @return {string} - A string representation of the model including its class name and JSON representation
63
- * @override
64
- */
65
- toString(): string;
66
- /**
67
- * @description Generates a hash string for the model object
68
- * @summary Defines a default implementation for object hash, relying on a basic implementation based on Java's string hash
69
- *
70
- * @return {string} - A hash string representing the model
71
- */
72
- hash(): string;
73
- /**
74
- * @description Converts a serialized string back into a model instance
75
- * @summary Deserializes a Model from its string representation
76
- *
77
- * @param {string} str - The serialized string to convert back to a model
78
- * @return {any} - The deserialized model instance
79
- * @throws {Error} If it fails to parse the string, or if it fails to build the model
80
- */
81
- static deserialize(str: string): any;
82
- /**
83
- * @description Copies properties from a source object to a model instance
84
- * @summary Repopulates the Object properties with the ones from the new object
85
- *
86
- * @template T
87
- * @param {T} self - The target model instance to update
88
- * @param {T | Record<string, any>} [obj] - The source object containing properties to copy
89
- * @return {T} - The updated model instance
90
- */
91
- static fromObject<T extends Model<any>>(self: T, obj?: T | Record<string, any>): T;
92
- /**
93
- * @description Copies and rebuilds properties from a source object to a model instance, handling nested models
94
- * @summary Repopulates the instance with properties from the new Model Object, recursively rebuilding nested models
95
- *
96
- * @template T
97
- * @param {T} self - The target model instance to update
98
- * @param {T | Record<string, any>} [obj] - The source object containing properties to copy
99
- * @return {T} - The updated model instance with rebuilt nested models
100
- *
101
- * @mermaid
102
- * sequenceDiagram
103
- * participant C as Client
104
- * participant M as Model.fromModel
105
- * participant B as Model.build
106
- * participant R as Reflection
107
- *
108
- * C->>M: fromModel(self, obj)
109
- * M->>M: Get attributes from self
110
- * loop For each property
111
- * M->>M: Copy property from obj to self
112
- * alt Property is a model
113
- * M->>M: Check if property is a model
114
- * M->>B: build(property, modelType)
115
- * B-->>M: Return built model
116
- * else Property is a complex type
117
- * M->>R: Get property decorators
118
- * R-->>M: Return decorators
119
- * M->>M: Filter type decorators
120
- * alt Property is Array/Set with list decorator
121
- * M->>M: Process each item in collection
122
- * loop For each item
123
- * M->>B: build(item, itemModelType)
124
- * B-->>M: Return built model
125
- * end
126
- * else Property is another model type
127
- * M->>B: build(property, propertyType)
128
- * B-->>M: Return built model
129
- * end
130
- * end
131
- * end
132
- * M-->>C: Return updated self
133
- */
134
- static fromModel<T extends Model>(self: T, obj?: T | Record<string, any>): T;
135
- /**
136
- * @description Configures the global model builder function
137
- * @summary Sets the Global {@link ModelBuilderFunction} used for building model instances
138
- *
139
- * @param {ModelBuilderFunction} [builder] - The builder function to set as the global builder
140
- * @return {void}
141
- */
142
- static setBuilder(builder?: ModelBuilderFunction): void;
143
- /**
144
- * @description Retrieves the currently configured global model builder function
145
- * @summary Returns the current global {@link ModelBuilderFunction} used for building model instances
146
- *
147
- * @return {ModelBuilderFunction | undefined} - The current global builder function or undefined if not set
148
- */
149
- static getBuilder(): ModelBuilderFunction | undefined;
150
- /**
151
- * @description Provides access to the current model registry
152
- * @summary Returns the current {@link ModelRegistryManager} instance, creating one if it doesn't exist
153
- *
154
- * @return {ModelRegistry<any>} - The current model registry, defaults to a new {@link ModelRegistryManager} if not set
155
- * @private
156
- */
157
- private static getRegistry;
158
- /**
159
- * @description Configures the model registry to be used by the Model system
160
- * @summary Sets the current model registry to a custom implementation
161
- *
162
- * @param {BuilderRegistry<any>} modelRegistry - The new implementation of Registry to use
163
- * @return {void}
164
- */
165
- static setRegistry(modelRegistry: BuilderRegistry<any>): void;
166
- /**
167
- * @description Registers a model constructor with the model registry
168
- * @summary Registers new model classes to make them available for serialization and deserialization
169
- *
170
- * @template T
171
- * @param {ModelConstructor<T>} constructor - The model constructor to register
172
- * @param {string} [name] - Optional name to register the constructor under, defaults to constructor.name
173
- * @return {void}
174
- *
175
- * @see ModelRegistry
176
- */
177
- static register<T extends Model>(constructor: ModelConstructor<T>, name?: string): void;
178
- /**
179
- * @description Retrieves a registered model constructor by name
180
- * @summary Gets a registered Model {@link ModelConstructor} from the model registry
181
- *
182
- * @template T
183
- * @param {string} name - The name of the model constructor to retrieve
184
- * @return {ModelConstructor<T> | undefined} - The model constructor if found, undefined otherwise
185
- *
186
- * @see ModelRegistry
187
- */
188
- static get<T extends Model>(name: string): ModelConstructor<T> | undefined;
189
- /**
190
- * @description Creates a model instance from a plain object
191
- * @summary Builds a model instance using the model registry, optionally specifying the model class
192
- *
193
- * @template T
194
- * @param {Record<string, any>} obj - The source object to build the model from
195
- * @param {string} [clazz] - When provided, it will attempt to find the matching constructor by name
196
- * @return {T} - The built model instance
197
- * @throws {Error} If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
198
- *
199
- * @see ModelRegistry
200
- */
201
- static build<T extends Model>(obj?: Record<string, any>, clazz?: string): T;
202
- /**
203
- * @description Retrieves all attribute names from a model class or instance
204
- * @summary Gets all attributes defined in a model, traversing the prototype chain to include inherited attributes
205
- *
206
- * @template V
207
- * @param {Constructor<V> | V} model - The model class or instance to get attributes from
208
- * @return {string[]} - Array of attribute names defined in the model
209
- */
210
- static getAttributes<V extends Model>(model: Constructor<V> | V): string[];
211
- /**
212
- * @description Compares two model instances for equality
213
- * @summary Determines if two model instances are equal by comparing their properties
214
- *
215
- * @template M
216
- * @param {M} obj1 - First model instance to compare
217
- * @param {M} obj2 - Second model instance to compare
218
- * @param {any[]} [exceptions] - Property names to exclude from comparison
219
- * @return {boolean} - True if the models are equal, false otherwise
220
- */
221
- static equals<M extends Model>(obj1: M, obj2: M, ...exceptions: any[]): boolean;
222
- /**
223
- * @description Validates a model instance against its validation rules
224
- * @summary Checks if a model has validation errors, optionally ignoring specified properties
225
- *
226
- * @template M
227
- * @param {M} model - The model instance to validate
228
- * @param {boolean} async - A flag indicating whether validation should be asynchronous.
229
- * @param {string[]} [propsToIgnore] - Properties to exclude from validation
230
- * @return {ModelErrorDefinition | undefined} - Returns validation errors if any, otherwise undefined
231
- */
232
- static hasErrors<M extends Model, Async extends boolean = false>(model: M, async: Async, ...propsToIgnore: string[]): ConditionalAsync<Async, ModelErrorDefinition | undefined>;
233
- /**
234
- * @description Converts a model instance to a serialized string
235
- * @summary Serializes a model instance using the configured serializer or the default one
236
- *
237
- * @template M
238
- * @param {M} model - The model instance to serialize
239
- * @return {string} - The serialized string representation of the model
240
- */
241
- static serialize<M extends Model<boolean>>(model: M): any;
242
- /**
243
- * @description Generates a hash string for a model instance
244
- * @summary Creates a hash representation of a model using the configured algorithm or the default one
245
- *
246
- * @template M
247
- * @param {M} model - The model instance to hash
248
- * @return {string} - The hash string representing the model
249
- */
250
- static hash<M extends Model<boolean>>(model: M): any;
251
- /**
252
- * @description Determines if an object is a model instance or has model metadata
253
- * @summary Checks whether a given object is either an instance of the Model class or
254
- * has model metadata attached to it. This function is essential for serialization and
255
- * deserialization processes, as it helps identify model objects that need special handling.
256
- * It safely handles potential errors during metadata retrieval.
257
- *
258
- * @param {Record<string, any>} target - The object to check
259
- * @return {boolean} True if the object is a model instance or has model metadata, false otherwise
260
- *
261
- * @example
262
- * ```typescript
263
- * // Check if an object is a model
264
- * const user = new User({ name: "John" });
265
- * const isUserModel = isModel(user); // true
266
- *
267
- * // Check a plain object
268
- * const plainObject = { name: "John" };
269
- * const isPlainObjectModel = isModel(plainObject); // false
270
- * ```
271
- */
272
- static isModel(target: Record<string, any>): boolean;
273
- /**
274
- * @description Checks if a property of a model is itself a model or has a model type
275
- * @summary Determines whether a specific property of a model instance is either a model instance
276
- * or has a type that is registered as a model
277
- *
278
- * @template M
279
- * @param {M} target - The model instance to check
280
- * @param {string} attribute - The property name to check
281
- * @return {boolean | string | undefined} - Returns true if the property is a model instance,
282
- * the model name if the property has a model type, or undefined if not a model
283
- */
284
- static isPropertyModel<M extends Model>(target: M, attribute: string): boolean | string | undefined;
285
- static describe<M extends Model>(model: Constructor<M>, key?: keyof M): string | undefined;
286
- }
@@ -1,22 +0,0 @@
1
- import { ModelErrors } from "../validation/types";
2
- /**
3
- * @summary Helper Class to hold the error results
4
- * @description holds error results in an 'indexable' manner
5
- * while still providing the same result on toString
6
- *
7
- * @param {ModelErrors} errors
8
- *
9
- * @class ModelErrorDefinition
10
- *
11
- * @category Model
12
- */
13
- export declare class ModelErrorDefinition {
14
- [indexer: string]: Record<string, string | undefined> | (() => string | undefined);
15
- constructor(errors: ModelErrors);
16
- /**
17
- * @summary Outputs the class to a nice readable string
18
- *
19
- * @override
20
- */
21
- toString(): string;
22
- }
@@ -1,181 +0,0 @@
1
- import { Constructor } from "@decaf-ts/decoration";
2
- import { ModelBuilderFunction, ModelConstructor } from "./types";
3
- import { type Model } from "./Model";
4
- import { BuilderRegistry } from "../utils/registry";
5
- /**
6
- * @description Registry type for storing and retrieving model constructors
7
- * @summary The ModelRegistry type defines a registry for model constructors that extends
8
- * the BuilderRegistry interface. It provides a standardized way to register, retrieve,
9
- * and build model instances, enabling the model system to work with different types of models.
10
- *
11
- * @interface ModelRegistry
12
- * @template T Type of model that can be registered, must extend Model
13
- * @extends BuilderRegistry<T>
14
- * @memberOf module:decorator-validation
15
- * @category Model
16
- */
17
- export type ModelRegistry<T extends Model> = BuilderRegistry<T>;
18
- /**
19
- * @description Registry manager for model constructors that enables serialization and rebuilding
20
- * @summary The ModelRegistryManager implements the ModelRegistry interface and provides
21
- * functionality for registering, retrieving, and building model instances. It maintains
22
- * a cache of model constructors indexed by name, allowing for efficient lookup and instantiation.
23
- * This class is essential for the serialization and deserialization of model objects.
24
- *
25
- * @param {function(Record<string, any>): boolean} [testFunction] - Function to test if an object is a model, defaults to {@link Model#isModel}
26
- *
27
- * @class ModelRegistryManager
28
- * @template M Type of model that can be registered, must extend Model
29
- * @implements ModelRegistry<M>
30
- * @category Model
31
- *
32
- * @example
33
- * ```typescript
34
- * // Create a model registry
35
- * const registry = new ModelRegistryManager();
36
- *
37
- * // Register a model class
38
- * registry.register(User);
39
- *
40
- * // Retrieve a model constructor by name
41
- * const UserClass = registry.get("User");
42
- *
43
- * // Build a model instance from a plain object
44
- * const userData = { name: "John", age: 30 };
45
- * const user = registry.build(userData, "User");
46
- * ```
47
- *
48
- * @mermaid
49
- * sequenceDiagram
50
- * participant C as Client
51
- * participant R as ModelRegistryManager
52
- * participant M as Model Class
53
- *
54
- * C->>R: new ModelRegistryManager(testFunction)
55
- * C->>R: register(ModelClass)
56
- * R->>R: Store in cache
57
- * C->>R: get("ModelName")
58
- * R-->>C: ModelClass constructor
59
- * C->>R: build(data, "ModelName")
60
- * R->>R: Get constructor from cache
61
- * R->>M: new ModelClass(data)
62
- * M-->>R: Model instance
63
- * R-->>C: Model instance
64
- */
65
- export declare class ModelRegistryManager<M extends Model<true | false>> implements ModelRegistry<M> {
66
- private cache;
67
- private readonly testFunction;
68
- constructor(testFunction?: (obj: Record<string, any>) => boolean);
69
- /**
70
- * @description Registers a model constructor with the registry
71
- * @summary Adds a model constructor to the registry cache, making it available for
72
- * later retrieval and instantiation. If no name is provided, the constructor's name
73
- * property is used as the key in the registry.
74
- *
75
- * @param {ModelConstructor<M>} constructor - The model constructor to register
76
- * @param {string} [name] - Optional name to register the constructor under, defaults to constructor.name
77
- * @return {void}
78
- * @throws {Error} If the constructor is not a function
79
- */
80
- register(constructor: ModelConstructor<M>, name?: string): void;
81
- /**
82
- * @summary Gets a registered Model {@link ModelConstructor}
83
- * @param {string} name
84
- */
85
- get(name: string): ModelConstructor<M> | undefined;
86
- /**
87
- * @param {Record<string, any>} obj
88
- * @param {string} [clazz] when provided, it will attempt to find the matching constructor
89
- *
90
- * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
91
- */
92
- build(obj?: Record<string, any>, clazz?: string): M;
93
- /**
94
- * @description Configures the global model builder function
95
- * @summary Sets the Global {@link ModelBuilderFunction} used for building model instances
96
- *
97
- * @param {ModelBuilderFunction} [builder] - The builder function to set as the global builder
98
- * @return {void}
99
- */
100
- static setBuilder(builder?: ModelBuilderFunction): void;
101
- /**
102
- * @description Retrieves the currently configured global model builder function
103
- * @summary Returns the current global {@link ModelBuilderFunction} used for building model instances
104
- *
105
- * @return {ModelBuilderFunction | undefined} - The current global builder function or undefined if not set
106
- */
107
- static getBuilder(): ModelBuilderFunction | undefined;
108
- /**
109
- * @description Copies and rebuilds properties from a source object to a model instance, handling nested models
110
- * @summary Repopulates the instance with properties from the new Model Object, recursively rebuilding nested models
111
- *
112
- * @template T
113
- * @param {T} self - The target model instance to update
114
- * @param {T | Record<string, any>} [obj] - The source object containing properties to copy
115
- * @return {T} - The updated model instance with rebuilt nested models
116
- *
117
- * @mermaid
118
- * sequenceDiagram
119
- * participant C as Client
120
- * participant M as Model.fromModel
121
- * participant B as Model.build
122
- * participant R as Reflection
123
- *
124
- * C->>M: fromModel(self, obj)
125
- * M->>M: Get attributes from self
126
- * loop For each property
127
- * M->>M: Copy property from obj to self
128
- * alt Property is a model
129
- * M->>M: Check if property is a model
130
- * M->>B: build(property, modelType)
131
- * B-->>M: Return built model
132
- * else Property is a complex type
133
- * M->>R: Get property decorators
134
- * R-->>M: Return decorators
135
- * M->>M: Filter type decorators
136
- * alt Property is Array/Set with list decorator
137
- * M->>M: Process each item in collection
138
- * loop For each item
139
- * M->>B: build(item, itemModelType)
140
- * B-->>M: Return built model
141
- * end
142
- * else Property is another model type
143
- * M->>B: build(property, propertyType)
144
- * B-->>M: Return built model
145
- * end
146
- * end
147
- * end
148
- * M-->>C: Return updated self
149
- */
150
- static fromModel<T extends Model>(self: T, obj?: T | Record<string, any>): T;
151
- /**
152
- * @description Provides access to the current model registry
153
- * @summary Returns the current {@link ModelRegistryManager} instance, creating one if it doesn't exist
154
- *
155
- * @return {ModelRegistry<any>} - The current model registry, defaults to a new {@link ModelRegistryManager} if not set
156
- * @private
157
- */
158
- static getRegistry(): BuilderRegistry<any>;
159
- /**
160
- * @description Configures the model registry to be used by the Model system
161
- * @summary Sets the current model registry to a custom implementation
162
- *
163
- * @param {BuilderRegistry<any>} modelRegistry - The new implementation of Registry to use
164
- * @return {void}
165
- */
166
- static setRegistry(modelRegistry: BuilderRegistry<any>): void;
167
- }
168
- /**
169
- * @summary Bulk Registers Models
170
- * @description Useful when using bundlers that might not evaluate all the code at once
171
- *
172
- * @template M extends Model
173
- * @param {Array<Constructor<M>> | Array<{name: string, constructor: Constructor<M>}>} [models]
174
- *
175
- * @memberOf module:decorator-validation
176
- * @category Model
177
- */
178
- export declare function bulkModelRegister<M extends Model>(...models: (Constructor<M> | {
179
- name: string;
180
- constructor: Constructor<M>;
181
- })[]): void;
@@ -1,65 +0,0 @@
1
- /**
2
- * @description Enumeration of JavaScript primitive type identifiers used by the model system.
3
- * @summary References the relevant JS primitives and standardizes their string representations across the library.
4
- * @property {string} STRING references the string primitive
5
- * @property {string} NUMBER references the number primitive
6
- * @property {string} BOOLEAN references the boolean primitive
7
- * @property {string} BIGINT references the bigint primitive
8
- * @enum Primitives
9
- * @readonly
10
- * @memberOf module:decorator-validation
11
- */
12
- export declare enum Primitives {
13
- /** references the string primitive */
14
- STRING = "string",
15
- /** references the number primitive */
16
- NUMBER = "number",
17
- /** references the boolean primitive */
18
- BOOLEAN = "boolean",
19
- /** references the bigint primitive */
20
- BIGINT = "bigint"
21
- }
22
- /**
23
- * @description Reserved model names which are excluded from model rebuilding.
24
- * @summary References the Reserved model names to ignore during Model rebuilding to avoid interfering with native types and special cases.
25
- * @property {string} STRING
26
- * @property {string} OBJECT
27
- * @property {string} NUMBER
28
- * @property {string} BOOLEAN
29
- * @property {string} BIGINT
30
- * @property {string} DATE
31
- * @enum ReservedModels
32
- * @readonly
33
- * @memberOf module:decorator-validation
34
- */
35
- export declare const ReservedModels: {
36
- /** reserved name for string */
37
- STRING: StringConstructor;
38
- /** reserved name for object */
39
- OBJECT: ObjectConstructor;
40
- /** reserved name for number */
41
- NUMBER: NumberConstructor;
42
- /** reserved name for boolean */
43
- BOOLEAN: BooleanConstructor;
44
- /** reserved name for bigint */
45
- BIGINT: BigIntConstructor;
46
- /** reserved name for Date */
47
- DATE: DateConstructor;
48
- };
49
- /**
50
- * @description Basic supported JavaScript types used by the validation system.
51
- * @summary References the basic supported JS types as strings that can be used for type checking and metadata.
52
- * @typedef {Object} JsTypes
53
- * @property {"string"} string String primitive identifier
54
- * @property {"array"} array Array type identifier
55
- * @property {"number"} number Number primitive identifier
56
- * @property {"boolean"} boolean Boolean primitive identifier
57
- * @property {"symbol"} symbol Symbol primitive identifier
58
- * @property {"function"} function Function type identifier
59
- * @property {"object"} object Object type identifier
60
- * @property {"undefined"} undefined Undefined type identifier
61
- * @property {"null"} null Null value identifier
62
- * @property {"bigint"} BIGINT BigInt primitive identifier
63
- * @memberOf module:decorator-validation
64
- */
65
- export declare const jsTypes: string[];
@@ -1,46 +0,0 @@
1
- import { Model } from "./Model";
2
- /**
3
- * @description Helper function to create an instance by invoking a constructor with dynamic arguments.
4
- * @summary Overrides standard construction patterns by wrapping the given constructor to allow spread argument invocation while preserving the prototype chain.
5
- * @template M the model instance type
6
- * @param {any} constructor The constructor function to invoke.
7
- * @param {...any[]} args Optional arguments to pass to the constructor.
8
- * @return {M} The newly constructed instance.
9
- * @function construct
10
- * @memberOf module:decorator-validation
11
- */
12
- export declare function construct<M extends Model>(constructor: any, ...args: any[]): M;
13
- /**
14
- * @description Recursively finds the last prototype in the chain before reaching Object.prototype.
15
- * @summary Walks up the prototype chain to locate the most derived prototype that still precedes the base Object prototype.
16
- * @param {object} obj The object whose prototype chain will be inspected.
17
- * @return {object} The last prototype before Object.prototype, or the input object if its prototype is Object.prototype.
18
- * @function findLastProtoBeforeObject
19
- * @memberOf module:decorator-validation
20
- */
21
- export declare function findLastProtoBeforeObject(obj: object): object;
22
- /**
23
- * @description Binds the Model class as the root prototype of the provided instance when not already a Model.
24
- * @summary Ensures objects created outside of the Model inheritance chain gain Model as their ultimate prototype to access model utilities.
25
- * @param {unknown} obj The object to bind to the Model prototype chain.
26
- * @return {void}
27
- * @function bindModelPrototype
28
- * @mermaid
29
- * sequenceDiagram
30
- * participant Caller
31
- * participant Fn as bindModelPrototype
32
- * participant M as Model.prototype
33
- * Caller->>Fn: obj
34
- * alt obj instanceof Model
35
- * Fn-->>Caller: return
36
- * else obj chain ends at Object.prototype
37
- * Fn->>Fn: setPrototypeOf(obj, M)
38
- * Fn-->>Caller: return
39
- * else deep prototype chain
40
- * Fn->>Fn: walk prototypes
41
- * Fn->>Fn: setPrototypeOf(last, M)
42
- * Fn-->>Caller: return
43
- * end
44
- * @memberOf module:decorator-validation
45
- */
46
- export declare function bindModelPrototype(obj: unknown): void;
@@ -1,47 +0,0 @@
1
- /**
2
- * @description Base decorator for model classes.
3
- * @summary This decorator wraps the original constructor to bind the Model prototype, run a builder function, and register the model.
4
- * @param {any} original The original constructor of the class.
5
- * @return {any} The new constructor with added model functionality.
6
- * @function modelBaseDecorator
7
- * @memberOf module:decorator-validation
8
- */
9
- export declare function modelBaseDecorator(original: any): any;
10
- /**
11
- * @summary Defines a class as a Model class
12
- * @description
13
- *
14
- * - Registers the class under the model registry so it can be easily rebuilt;
15
- * - Overrides the class constructor;
16
- * - Runs the global {@link ModelBuilderFunction} if defined;
17
- *
18
- * @function model
19
- *
20
- * @category Class Decorators
21
- */
22
- export declare function model(): (target: any, propertyKey?: any, descriptor?: TypedPropertyDescriptor<any>) => any;
23
- /**
24
- * @summary Defines the hashing algorithm to use on the model
25
- * @description
26
- *
27
- * - Registers the class under the model registry so it can be easily rebuilt;
28
- * - Overrides the class constructor;
29
- * - Runs the global {@link ModelBuilderFunction} if defined;
30
- *
31
- * @param {string} algorithm the algorithm to use
32
- *
33
- * @function hashedBy
34
- *
35
- * @category Class Decorators
36
- */
37
- export declare function hashedBy(algorithm: string, ...args: any[]): (model: any, prop?: any, descriptor?: PropertyDescriptor | number) => void;
38
- /**
39
- * @summary Defines the serialization algorithm to use on the model
40
- *
41
- * @param {string} serializer the algorithm to use
42
- *
43
- * @function serializedBy
44
- *
45
- * @category Class Decorators
46
- */
47
- export declare function serializedBy(serializer: string, ...args: any[]): (model: any, prop?: any, descriptor?: PropertyDescriptor | number) => void;
@@ -1,10 +0,0 @@
1
- export * from "./constants";
2
- export * from "./construction";
3
- export * from "./decorators";
4
- export * from "./ModelRegistry";
5
- export * from "./Model";
6
- export * from "./ModelErrorDefinition";
7
- export * from "./Builder";
8
- export * from "./types";
9
- export * from "./validation";
10
- export * from "./utils";