@decaf-ts/decorator-validation 1.5.1 → 1.5.2

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 (170) hide show
  1. package/package.json +15 -9
  2. package/dist/decorator-validation.bundle.min.js +0 -2
  3. package/dist/decorator-validation.bundle.min.js.LICENSE.txt +0 -14
  4. package/dist/esm/decorator-validation.bundle.min.esm.js +0 -2
  5. package/dist/esm/decorator-validation.bundle.min.esm.js.LICENSE.txt +0 -14
  6. package/lib/esm/index.d.ts +0 -42
  7. package/lib/esm/index.js +0 -42
  8. package/lib/esm/model/Model.d.ts +0 -138
  9. package/lib/esm/model/Model.js +0 -296
  10. package/lib/esm/model/ModelErrorDefinition.d.ts +0 -22
  11. package/lib/esm/model/ModelErrorDefinition.js +0 -52
  12. package/lib/esm/model/Registry.d.ts +0 -59
  13. package/lib/esm/model/Registry.js +0 -73
  14. package/lib/esm/model/constants.d.ts +0 -56
  15. package/lib/esm/model/constants.js +0 -69
  16. package/lib/esm/model/construction.d.ts +0 -29
  17. package/lib/esm/model/construction.js +0 -63
  18. package/lib/esm/model/decorators.d.ts +0 -25
  19. package/lib/esm/model/decorators.js +0 -62
  20. package/lib/esm/model/index.d.ts +0 -9
  21. package/lib/esm/model/index.js +0 -9
  22. package/lib/esm/model/types.d.ts +0 -79
  23. package/lib/esm/model/types.js +0 -1
  24. package/lib/esm/model/utils.d.ts +0 -11
  25. package/lib/esm/model/utils.js +0 -25
  26. package/lib/esm/model/validation.d.ts +0 -14
  27. package/lib/esm/model/validation.js +0 -135
  28. package/lib/esm/utils/constants.d.ts +0 -26
  29. package/lib/esm/utils/constants.js +0 -27
  30. package/lib/esm/utils/dates.d.ts +0 -76
  31. package/lib/esm/utils/dates.js +0 -243
  32. package/lib/esm/utils/decorators.d.ts +0 -2
  33. package/lib/esm/utils/decorators.js +0 -18
  34. package/lib/esm/utils/hashing.d.ts +0 -52
  35. package/lib/esm/utils/hashing.js +0 -99
  36. package/lib/esm/utils/index.d.ts +0 -7
  37. package/lib/esm/utils/index.js +0 -7
  38. package/lib/esm/utils/registry.d.ts +0 -68
  39. package/lib/esm/utils/registry.js +0 -1
  40. package/lib/esm/utils/serialization.d.ts +0 -79
  41. package/lib/esm/utils/serialization.js +0 -88
  42. package/lib/esm/utils/strings.d.ts +0 -25
  43. package/lib/esm/utils/strings.js +0 -31
  44. package/lib/esm/validation/Validation.d.ts +0 -51
  45. package/lib/esm/validation/Validation.js +0 -71
  46. package/lib/esm/validation/Validators/DateValidator.d.ts +0 -30
  47. package/lib/esm/validation/Validators/DateValidator.js +0 -54
  48. package/lib/esm/validation/Validators/EmailValidator.d.ts +0 -27
  49. package/lib/esm/validation/Validators/EmailValidator.js +0 -50
  50. package/lib/esm/validation/Validators/ListValidator.d.ts +0 -30
  51. package/lib/esm/validation/Validators/ListValidator.js +0 -65
  52. package/lib/esm/validation/Validators/MaxLengthValidator.d.ts +0 -31
  53. package/lib/esm/validation/Validators/MaxLengthValidator.js +0 -52
  54. package/lib/esm/validation/Validators/MaxValidator.d.ts +0 -30
  55. package/lib/esm/validation/Validators/MaxValidator.js +0 -57
  56. package/lib/esm/validation/Validators/MinLengthValidator.d.ts +0 -31
  57. package/lib/esm/validation/Validators/MinLengthValidator.js +0 -52
  58. package/lib/esm/validation/Validators/MinValidator.d.ts +0 -30
  59. package/lib/esm/validation/Validators/MinValidator.js +0 -57
  60. package/lib/esm/validation/Validators/PasswordValidator.d.ts +0 -27
  61. package/lib/esm/validation/Validators/PasswordValidator.js +0 -50
  62. package/lib/esm/validation/Validators/PatternValidator.d.ts +0 -39
  63. package/lib/esm/validation/Validators/PatternValidator.js +0 -70
  64. package/lib/esm/validation/Validators/RequiredValidator.d.ts +0 -27
  65. package/lib/esm/validation/Validators/RequiredValidator.js +0 -57
  66. package/lib/esm/validation/Validators/StepValidator.d.ts +0 -31
  67. package/lib/esm/validation/Validators/StepValidator.js +0 -52
  68. package/lib/esm/validation/Validators/TypeValidator.d.ts +0 -29
  69. package/lib/esm/validation/Validators/TypeValidator.js +0 -60
  70. package/lib/esm/validation/Validators/URLValidator.d.ts +0 -32
  71. package/lib/esm/validation/Validators/URLValidator.js +0 -46
  72. package/lib/esm/validation/Validators/Validator.d.ts +0 -43
  73. package/lib/esm/validation/Validators/Validator.js +0 -47
  74. package/lib/esm/validation/Validators/ValidatorRegistry.d.ts +0 -47
  75. package/lib/esm/validation/Validators/ValidatorRegistry.js +0 -80
  76. package/lib/esm/validation/Validators/constants.d.ts +0 -96
  77. package/lib/esm/validation/Validators/constants.js +0 -134
  78. package/lib/esm/validation/Validators/decorators.d.ts +0 -12
  79. package/lib/esm/validation/Validators/decorators.js +0 -25
  80. package/lib/esm/validation/Validators/index.d.ts +0 -52
  81. package/lib/esm/validation/Validators/index.js +0 -52
  82. package/lib/esm/validation/Validators/types.d.ts +0 -51
  83. package/lib/esm/validation/Validators/types.js +0 -1
  84. package/lib/esm/validation/decorators.d.ts +0 -178
  85. package/lib/esm/validation/decorators.js +0 -288
  86. package/lib/esm/validation/index.d.ts +0 -4
  87. package/lib/esm/validation/index.js +0 -4
  88. package/lib/esm/validation/types.d.ts +0 -46
  89. package/lib/esm/validation/types.js +0 -1
  90. package/lib/index.cjs +0 -59
  91. package/lib/index.d.ts +0 -42
  92. package/lib/model/Model.cjs +0 -300
  93. package/lib/model/Model.d.ts +0 -138
  94. package/lib/model/ModelErrorDefinition.cjs +0 -56
  95. package/lib/model/ModelErrorDefinition.d.ts +0 -22
  96. package/lib/model/Registry.cjs +0 -78
  97. package/lib/model/Registry.d.ts +0 -59
  98. package/lib/model/constants.cjs +0 -72
  99. package/lib/model/constants.d.ts +0 -56
  100. package/lib/model/construction.cjs +0 -68
  101. package/lib/model/construction.d.ts +0 -29
  102. package/lib/model/decorators.cjs +0 -67
  103. package/lib/model/decorators.d.ts +0 -25
  104. package/lib/model/index.cjs +0 -25
  105. package/lib/model/index.d.ts +0 -9
  106. package/lib/model/types.cjs +0 -2
  107. package/lib/model/types.d.ts +0 -79
  108. package/lib/model/utils.cjs +0 -29
  109. package/lib/model/utils.d.ts +0 -11
  110. package/lib/model/validation.cjs +0 -138
  111. package/lib/model/validation.d.ts +0 -14
  112. package/lib/utils/constants.cjs +0 -30
  113. package/lib/utils/constants.d.ts +0 -26
  114. package/lib/utils/dates.cjs +0 -251
  115. package/lib/utils/dates.d.ts +0 -76
  116. package/lib/utils/decorators.cjs +0 -22
  117. package/lib/utils/decorators.d.ts +0 -2
  118. package/lib/utils/hashing.cjs +0 -106
  119. package/lib/utils/hashing.d.ts +0 -52
  120. package/lib/utils/index.cjs +0 -23
  121. package/lib/utils/index.d.ts +0 -7
  122. package/lib/utils/registry.cjs +0 -2
  123. package/lib/utils/registry.d.ts +0 -68
  124. package/lib/utils/serialization.cjs +0 -93
  125. package/lib/utils/serialization.d.ts +0 -79
  126. package/lib/utils/strings.cjs +0 -35
  127. package/lib/utils/strings.d.ts +0 -25
  128. package/lib/validation/Validation.cjs +0 -75
  129. package/lib/validation/Validation.d.ts +0 -51
  130. package/lib/validation/Validators/DateValidator.cjs +0 -57
  131. package/lib/validation/Validators/DateValidator.d.ts +0 -30
  132. package/lib/validation/Validators/EmailValidator.cjs +0 -53
  133. package/lib/validation/Validators/EmailValidator.d.ts +0 -27
  134. package/lib/validation/Validators/ListValidator.cjs +0 -68
  135. package/lib/validation/Validators/ListValidator.d.ts +0 -30
  136. package/lib/validation/Validators/MaxLengthValidator.cjs +0 -55
  137. package/lib/validation/Validators/MaxLengthValidator.d.ts +0 -31
  138. package/lib/validation/Validators/MaxValidator.cjs +0 -60
  139. package/lib/validation/Validators/MaxValidator.d.ts +0 -30
  140. package/lib/validation/Validators/MinLengthValidator.cjs +0 -55
  141. package/lib/validation/Validators/MinLengthValidator.d.ts +0 -31
  142. package/lib/validation/Validators/MinValidator.cjs +0 -60
  143. package/lib/validation/Validators/MinValidator.d.ts +0 -30
  144. package/lib/validation/Validators/PasswordValidator.cjs +0 -53
  145. package/lib/validation/Validators/PasswordValidator.d.ts +0 -27
  146. package/lib/validation/Validators/PatternValidator.cjs +0 -73
  147. package/lib/validation/Validators/PatternValidator.d.ts +0 -39
  148. package/lib/validation/Validators/RequiredValidator.cjs +0 -60
  149. package/lib/validation/Validators/RequiredValidator.d.ts +0 -27
  150. package/lib/validation/Validators/StepValidator.cjs +0 -55
  151. package/lib/validation/Validators/StepValidator.d.ts +0 -31
  152. package/lib/validation/Validators/TypeValidator.cjs +0 -63
  153. package/lib/validation/Validators/TypeValidator.d.ts +0 -29
  154. package/lib/validation/Validators/URLValidator.cjs +0 -49
  155. package/lib/validation/Validators/URLValidator.d.ts +0 -32
  156. package/lib/validation/Validators/Validator.cjs +0 -51
  157. package/lib/validation/Validators/Validator.d.ts +0 -43
  158. package/lib/validation/Validators/ValidatorRegistry.cjs +0 -85
  159. package/lib/validation/Validators/ValidatorRegistry.d.ts +0 -47
  160. package/lib/validation/Validators/constants.cjs +0 -137
  161. package/lib/validation/Validators/constants.d.ts +0 -96
  162. package/lib/validation/Validators/decorators.cjs +0 -28
  163. package/lib/validation/Validators/index.cjs +0 -69
  164. package/lib/validation/Validators/types.cjs +0 -2
  165. package/lib/validation/decorators.cjs +0 -304
  166. package/lib/validation/decorators.d.ts +0 -178
  167. package/lib/validation/index.cjs +0 -20
  168. package/lib/validation/index.d.ts +0 -4
  169. package/lib/validation/types.cjs +0 -2
  170. package/lib/validation/types.d.ts +0 -46
@@ -1,296 +0,0 @@
1
- import { Serialization } from "../utils/serialization";
2
- import { ModelRegistryManager } from "./Registry";
3
- import { isEqual, Reflection } from "@decaf-ts/reflection";
4
- import { validate } from "./validation";
5
- import { Hashing } from "../utils/hashing";
6
- import { isPropertyModel } from "./utils";
7
- import { ModelKeys } from "../utils/constants";
8
- import { ValidationKeys } from "../validation/Validators/constants";
9
- import { sf } from "../utils/strings";
10
- import { jsTypes, ReservedModels } from "./constants";
11
- let modelBuilderFunction;
12
- let actingModelRegistry;
13
- /**
14
- * @summary Abstract class representing a Validatable Model object
15
- * @description Meant to be used as a base class for all Model classes
16
- *
17
- * Model objects must:
18
- * - Have all their required properties marked with '!';
19
- * - Have all their optional properties marked as '?':
20
- *
21
- * @param {Model | {}} model base object from which to populate properties from
22
- *
23
- * @class Model
24
- * @abstract
25
- * @implements Validatable
26
- * @implements Serializable
27
- *
28
- * @example
29
- * class ClassName {
30
- * @required()
31
- * requiredPropertyName!: PropertyType;
32
- *
33
- * optionalPropertyName?: PropertyType;
34
- * }
35
- */
36
- export class Model {
37
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
38
- constructor(arg) { }
39
- /**
40
- * @summary Validates the object according to its decorated properties
41
- *
42
- * @param {any[]} [exceptions] properties in the object to be ignored for the validation. Marked as 'any' to allow for extension but expects strings
43
- */
44
- hasErrors(...exceptions) {
45
- return validate(this, ...exceptions);
46
- }
47
- /**
48
- * @summary Compare object equality recursively
49
- * @param {any} obj object to compare to
50
- * @param {string} [exceptions] property names to be excluded from the comparison
51
- */
52
- equals(obj, ...exceptions) {
53
- return isEqual(this, obj, ...exceptions);
54
- }
55
- /**
56
- * @summary Returns the serialized model according to the currently defined {@link Serializer}
57
- */
58
- serialize() {
59
- return Model.serialize(this);
60
- }
61
- /**
62
- * @summary Override the implementation for js's 'toString()' which sucks...
63
- * @override
64
- */
65
- toString() {
66
- return this.constructor.name + ": " + JSON.stringify(this, undefined, 2);
67
- }
68
- /**
69
- * @summary Defines a default implementation for object hash. Relies on a very basic implementation based on Java's string hash;
70
- */
71
- hash() {
72
- return Model.hash(this);
73
- }
74
- /**
75
- * @summary Deserializes a Model
76
- * @param {string} str
77
- *
78
- * @throws {Error} If it fails to parse the string, or if it fails to build the model
79
- */
80
- static deserialize(str) {
81
- const metadata = Reflect.getMetadata(Model.key(ModelKeys.SERIALIZATION), this.constructor);
82
- if (metadata && metadata.serializer)
83
- return Serialization.deserialize(str, metadata.serializer, ...(metadata.args || []));
84
- return Serialization.deserialize(str);
85
- }
86
- /**
87
- * @summary Repopulates the Object properties with the ones from the new object
88
- * @description Iterates all common properties of obj (if existing) and self, and copies them onto self
89
- *
90
- * @param {T} self
91
- * @param {T | Record<string, any>} [obj]
92
- *
93
- */
94
- static fromObject(self, obj) {
95
- if (!obj)
96
- obj = {};
97
- for (const prop of Model.getAttributes(self)) {
98
- self[prop] = obj[prop] || undefined;
99
- }
100
- return self;
101
- }
102
- /**
103
- * @summary Repopulates the instance with the ones from the new Model Object
104
- * @description Iterates all common properties of obj (if existing) and self, and copies them onto self.
105
- * Is aware of nested Model Objects and rebuilds them also.
106
- * When List properties are decorated with {@link list}, they list items will also be rebuilt
107
- *
108
- * @param {T} self
109
- * @param {T | Record<string, any>} [obj]
110
- *
111
- */
112
- static fromModel(self, obj) {
113
- if (!obj)
114
- obj = {};
115
- let decorators, dec;
116
- const props = Model.getAttributes(self);
117
- for (const prop of props) {
118
- self[prop] =
119
- obj[prop] || undefined;
120
- if (typeof self[prop] !== "object")
121
- continue;
122
- const propM = isPropertyModel(self, prop);
123
- if (propM) {
124
- try {
125
- self[prop] = Model.build(self[prop], typeof propM === "string" ? propM : undefined);
126
- }
127
- catch (e) {
128
- console.log(e);
129
- }
130
- continue;
131
- }
132
- const allDecorators = Reflection.getPropertyDecorators(ValidationKeys.REFLECT, self, prop).decorators;
133
- decorators = allDecorators.filter((d) => [ModelKeys.TYPE, ValidationKeys.TYPE].indexOf(d.key) !== -1);
134
- if (!decorators || !decorators.length)
135
- throw new Error(sf("failed to find decorators for property {0}", prop));
136
- dec = decorators.pop();
137
- const clazz = dec.props.name
138
- ? [dec.props.name]
139
- : Array.isArray(dec.props.customTypes)
140
- ? dec.props.customTypes
141
- : [dec.props.customTypes];
142
- const reserved = Object.values(ReservedModels).map((v) => v.toLowerCase());
143
- clazz.forEach((c) => {
144
- if (reserved.indexOf(c.toLowerCase()) === -1)
145
- try {
146
- switch (c) {
147
- case "Array":
148
- case "Set":
149
- if (allDecorators.length) {
150
- const listDec = allDecorators.find((d) => d.key === ValidationKeys.LIST);
151
- if (listDec) {
152
- const clazzName = listDec.props.clazz.find((t) => !jsTypes.includes(t.toLowerCase()));
153
- if (c === "Array")
154
- self[prop] = self[prop].map((el) => {
155
- return ["object", "function"].includes(typeof el) &&
156
- clazzName
157
- ? Model.build(el, clazzName)
158
- : el;
159
- });
160
- if (c === "Set") {
161
- const s = new Set();
162
- for (const v of self[prop]) {
163
- if (["object", "function"].includes(typeof v) &&
164
- clazzName) {
165
- s.add(Model.build(v, clazzName));
166
- }
167
- else {
168
- s.add(v);
169
- }
170
- }
171
- self[prop] = s;
172
- }
173
- }
174
- }
175
- break;
176
- default:
177
- if (self[prop])
178
- self[prop] = Model.build(self[prop], c);
179
- }
180
- }
181
- catch (e) {
182
- console.log(e);
183
- // do nothing. we have no registry of this class
184
- }
185
- });
186
- }
187
- return self;
188
- }
189
- /**
190
- * @summary Sets the Global {@link ModelBuilderFunction}
191
- * @param {ModelBuilderFunction} [builder]
192
- */
193
- static setBuilder(builder) {
194
- modelBuilderFunction = builder;
195
- }
196
- /**
197
- * @summary Retrieves the current global {@link ModelBuilderFunction}
198
- */
199
- static getBuilder() {
200
- return modelBuilderFunction;
201
- }
202
- /**
203
- * Returns the current {@link ModelRegistryManager}
204
- *
205
- * @return ModelRegistry, defaults to {@link ModelRegistryManager}
206
- */
207
- static getRegistry() {
208
- if (!actingModelRegistry)
209
- actingModelRegistry = new ModelRegistryManager();
210
- return actingModelRegistry;
211
- }
212
- /**
213
- * Returns the current actingModelRegistry
214
- *
215
- * @param {BuilderRegistry} modelRegistry the new implementation of Registry
216
- */
217
- static setRegistry(modelRegistry) {
218
- actingModelRegistry = modelRegistry;
219
- }
220
- /**
221
- * @summary register new Models
222
- * @param {any} constructor
223
- * @param {string} [name] when not defined, the name of the constructor will be used
224
- *
225
- * @see ModelRegistry
226
- */
227
- static register(constructor, name) {
228
- return Model.getRegistry().register(constructor, name);
229
- }
230
- /**
231
- * @summary Gets a registered Model {@link ModelConstructor}
232
- * @param {string} name
233
- *
234
- * @see ModelRegistry
235
- */
236
- static get(name) {
237
- return Model.getRegistry().get(name);
238
- }
239
- /**
240
- * @param {Record<string, any>} obj
241
- * @param {string} [clazz] when provided, it will attempt to find the matching constructor
242
- *
243
- * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
244
- *
245
- * @see ModelRegistry
246
- */
247
- static build(obj = {}, clazz) {
248
- return Model.getRegistry().build(obj, clazz);
249
- }
250
- static getMetadata(model) {
251
- const metadata = Reflect.getMetadata(Model.key(ModelKeys.MODEL), model.constructor);
252
- if (!metadata)
253
- throw new Error("could not find metadata for provided " + model.constructor.name);
254
- return metadata;
255
- }
256
- static getAttributes(model) {
257
- const result = [];
258
- let prototype = model instanceof Model
259
- ? Object.getPrototypeOf(model)
260
- : model.prototype;
261
- while (prototype != null) {
262
- const props = prototype[ModelKeys.ATTRIBUTE];
263
- if (props) {
264
- result.push(...props);
265
- }
266
- prototype = Object.getPrototypeOf(prototype);
267
- }
268
- return result;
269
- }
270
- static equals(obj1, obj2, ...exceptions) {
271
- return isEqual(obj1, obj2, ...exceptions);
272
- }
273
- static hasErrors(model, ...propsToIgnore) {
274
- return validate(model, ...propsToIgnore);
275
- }
276
- static serialize(model) {
277
- const metadata = Reflect.getMetadata(Model.key(ModelKeys.SERIALIZATION), model.constructor);
278
- if (metadata && metadata.serializer)
279
- return Serialization.serialize(this, metadata.serializer, ...(metadata.args || []));
280
- return Serialization.serialize(model);
281
- }
282
- static hash(model) {
283
- const metadata = Reflect.getMetadata(Model.key(ModelKeys.HASHING), model.constructor);
284
- if (metadata && metadata.algorithm)
285
- return Hashing.hash(model, metadata.algorithm, ...(metadata.args || []));
286
- return Hashing.hash(model);
287
- }
288
- /**
289
- * @summary Builds the key to store as Metadata under Reflections
290
- * @description concatenates {@link ModelKeys#REFLECT} with the provided key
291
- * @param {string} str
292
- */
293
- static key(str) {
294
- return ModelKeys.REFLECT + str;
295
- }
296
- }
@@ -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,52 +0,0 @@
1
- /**
2
- * @summary Helper Class to hold the error results
3
- * @description holds error results in an 'indexable' manner
4
- * while still providing the same result on toString
5
- *
6
- * @param {ModelErrors} errors
7
- *
8
- * @class ModelErrorDefinition
9
- *
10
- * @category Model
11
- */
12
- export class ModelErrorDefinition {
13
- constructor(errors) {
14
- for (const prop in errors) {
15
- if (Object.prototype.hasOwnProperty.call(errors, prop) && errors[prop])
16
- Object.defineProperty(this, prop, {
17
- enumerable: true,
18
- configurable: false,
19
- value: errors[prop],
20
- writable: false,
21
- });
22
- }
23
- }
24
- /**
25
- * @summary Outputs the class to a nice readable string
26
- *
27
- * @override
28
- */
29
- toString() {
30
- const self = this;
31
- return Object.keys(self)
32
- .filter((k) => Object.prototype.hasOwnProperty.call(self, k) &&
33
- typeof self[k] !== "function")
34
- .reduce((accum, prop) => {
35
- let propError = Object.keys(self[prop]).reduce((propAccum, key) => {
36
- if (!propAccum)
37
- propAccum = self[prop][key];
38
- else
39
- propAccum += `\n${self[prop][key]}`;
40
- return propAccum;
41
- }, undefined);
42
- if (propError) {
43
- propError = `${prop} - ${propError}`;
44
- if (!accum)
45
- accum = propError;
46
- else
47
- accum += `\n${propError}`;
48
- }
49
- return accum;
50
- }, "");
51
- }
52
- }
@@ -1,59 +0,0 @@
1
- import { Model } from "./Model";
2
- import { BuilderRegistry } from "../utils/registry";
3
- import { Constructor, ModelConstructor } from "./types";
4
- /**
5
- * @summary ModelRegistry Interface
6
- *
7
- * @interface ModelRegistry
8
- * @extends BuilderRegistry<Model>
9
- *
10
- * @category Model
11
- */
12
- export type ModelRegistry<T extends Model> = BuilderRegistry<T>;
13
- /**
14
- * @summary Util class to enable serialization and correct rebuilding
15
- *
16
- * @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
17
- * @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
18
- *
19
- * @class ModelRegistryManager
20
- * @implements ModelRegistry
21
- *
22
- * @category Model
23
- */
24
- export declare class ModelRegistryManager<T extends Model> implements ModelRegistry<T> {
25
- private cache;
26
- private readonly testFunction;
27
- constructor(testFunction?: (obj: Record<string, any>) => boolean);
28
- /**
29
- * @summary register new Models
30
- * @param {any} constructor
31
- * @param {string} [name] when not defined, the name of the constructor will be used
32
- */
33
- register(constructor: ModelConstructor<T>, name?: string): void;
34
- /**
35
- * @summary Gets a registered Model {@link ModelConstructor}
36
- * @param {string} name
37
- */
38
- get(name: string): ModelConstructor<T> | undefined;
39
- /**
40
- * @param {Record<string, any>} obj
41
- * @param {string} [clazz] when provided, it will attempt to find the matching constructor
42
- *
43
- * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
44
- */
45
- build(obj?: Record<string, any>, clazz?: string): T;
46
- }
47
- /**
48
- * @summary Bulk Registers Models
49
- * @description Useful when using bundlers that might not evaluate all the code at once
50
- *
51
- * @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
52
- *
53
- * @memberOf module:decorator-validation.Model
54
- * @category Model
55
- */
56
- export declare function bulkModelRegister<T extends Model>(...models: (Constructor<T> | {
57
- name: string;
58
- constructor: Constructor<T>;
59
- })[]): void;
@@ -1,73 +0,0 @@
1
- import { Model } from "./Model";
2
- import { sf } from "../utils/strings";
3
- import { isModel } from "./utils";
4
- /**
5
- * @summary Util class to enable serialization and correct rebuilding
6
- *
7
- * @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
8
- * @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
9
- *
10
- * @class ModelRegistryManager
11
- * @implements ModelRegistry
12
- *
13
- * @category Model
14
- */
15
- export class ModelRegistryManager {
16
- constructor(testFunction = isModel) {
17
- this.cache = {};
18
- this.testFunction = testFunction;
19
- }
20
- /**
21
- * @summary register new Models
22
- * @param {any} constructor
23
- * @param {string} [name] when not defined, the name of the constructor will be used
24
- */
25
- register(constructor, name) {
26
- if (typeof constructor !== "function")
27
- throw new Error("Model registering failed. Missing Class name or constructor");
28
- name = name || constructor.name;
29
- this.cache[name] = constructor;
30
- }
31
- /**
32
- * @summary Gets a registered Model {@link ModelConstructor}
33
- * @param {string} name
34
- */
35
- get(name) {
36
- try {
37
- return this.cache[name];
38
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
39
- }
40
- catch (e) {
41
- return undefined;
42
- }
43
- }
44
- /**
45
- * @param {Record<string, any>} obj
46
- * @param {string} [clazz] when provided, it will attempt to find the matching constructor
47
- *
48
- * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
49
- */
50
- build(obj = {}, clazz) {
51
- if (!clazz && !this.testFunction(obj))
52
- throw new Error("Provided obj is not a Model object");
53
- const name = clazz || Model.getMetadata(obj);
54
- if (!(name in this.cache))
55
- throw new Error(sf("Provided class {0} is not a registered Model object", name));
56
- return new this.cache[name](obj);
57
- }
58
- }
59
- /**
60
- * @summary Bulk Registers Models
61
- * @description Useful when using bundlers that might not evaluate all the code at once
62
- *
63
- * @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
64
- *
65
- * @memberOf module:decorator-validation.Model
66
- * @category Model
67
- */
68
- export function bulkModelRegister(...models) {
69
- models.forEach((m) => {
70
- const constructor = (m.constructor ? m.constructor : m);
71
- Model.register(constructor, m.name);
72
- });
73
- }
@@ -1,56 +0,0 @@
1
- /**
2
- * @summary References the relevant JS primitives
3
- *
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
- *
9
- * @constant Primitives
10
- * @memberOf module:decorator-validation.Model
11
- */
12
- export declare enum Primitives {
13
- STRING = "string",
14
- NUMBER = "number",
15
- BOOLEAN = "boolean",
16
- BIGINT = "bigint"
17
- }
18
- /**
19
- * @summary References the Reserved model names to ignore during Model rebuilding
20
- *
21
- * @property {string} STRING
22
- * @property {string} OBJECT
23
- * @property {string} NUMBER
24
- * @property {string} BOOLEAN
25
- * @property {string} BIGINT
26
- * @property {string} DATE
27
- *
28
- * @constant ReservedModels
29
- * @memberOf module:decorator-validation.Model
30
- */
31
- export declare enum ReservedModels {
32
- STRING = "string",
33
- OBJECT = "object",
34
- NUMBER = "number",
35
- BOOLEAN = "boolean",
36
- BIGINT = "bigint",
37
- DATE = "date"
38
- }
39
- /**
40
- * @summary References the basic supported js types
41
- *
42
- * @property {string} string
43
- * @property {string} array
44
- * @property {string} number
45
- * @property {string} boolean
46
- * @property {string} symbol
47
- * @property {string} function
48
- * @property {string} object
49
- * @property {string} undefined
50
- * @property {string} null
51
- * @property {string} BIGINT
52
- *
53
- * @constant jsTypes
54
- * @memberOf module:decorator-validation.Model
55
- */
56
- export declare const jsTypes: string[];
@@ -1,69 +0,0 @@
1
- /**
2
- * @summary References the relevant JS primitives
3
- *
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
- *
9
- * @constant Primitives
10
- * @memberOf module:decorator-validation.Model
11
- */
12
- export var Primitives;
13
- (function (Primitives) {
14
- Primitives["STRING"] = "string";
15
- Primitives["NUMBER"] = "number";
16
- Primitives["BOOLEAN"] = "boolean";
17
- Primitives["BIGINT"] = "bigint";
18
- })(Primitives || (Primitives = {}));
19
- /**
20
- * @summary References the Reserved model names to ignore during Model rebuilding
21
- *
22
- * @property {string} STRING
23
- * @property {string} OBJECT
24
- * @property {string} NUMBER
25
- * @property {string} BOOLEAN
26
- * @property {string} BIGINT
27
- * @property {string} DATE
28
- *
29
- * @constant ReservedModels
30
- * @memberOf module:decorator-validation.Model
31
- */
32
- export var ReservedModels;
33
- (function (ReservedModels) {
34
- ReservedModels["STRING"] = "string";
35
- ReservedModels["OBJECT"] = "object";
36
- ReservedModels["NUMBER"] = "number";
37
- ReservedModels["BOOLEAN"] = "boolean";
38
- ReservedModels["BIGINT"] = "bigint";
39
- ReservedModels["DATE"] = "date";
40
- })(ReservedModels || (ReservedModels = {}));
41
- /**
42
- * @summary References the basic supported js types
43
- *
44
- * @property {string} string
45
- * @property {string} array
46
- * @property {string} number
47
- * @property {string} boolean
48
- * @property {string} symbol
49
- * @property {string} function
50
- * @property {string} object
51
- * @property {string} undefined
52
- * @property {string} null
53
- * @property {string} BIGINT
54
- *
55
- * @constant jsTypes
56
- * @memberOf module:decorator-validation.Model
57
- */
58
- export const jsTypes = [
59
- "string",
60
- "array",
61
- "number",
62
- "boolean",
63
- "symbol",
64
- "function",
65
- "object",
66
- "undefined",
67
- "null",
68
- "bigint",
69
- ];
@@ -1,29 +0,0 @@
1
- import { Model } from "./Model";
2
- /**
3
- * @summary Helper Function to override constructors
4
- *
5
- * @param {Function} constructor
6
- * @param {any[]} [args]
7
- * @return {T} the new instance
8
- *
9
- * @function construct
10
- * @memberOf module:decorator-validation.Construction
11
- */
12
- export declare function construct<T extends Model>(constructor: any, ...args: any[]): T;
13
- /**
14
- * @summary Recursively finds the last prototype before Object
15
- * @param {object} obj
16
- *
17
- * @function findLastProtoBeforeObject
18
- * @memberOf module:decorator-validation.Construction
19
- */
20
- export declare function findLastProtoBeforeObject(obj: object): object;
21
- /**
22
- * @sumary binds the {@link Model} class as a root prototype of the provided instance
23
- *
24
- * @param {unknown} obj
25
- *
26
- * @function bindModelPrototype
27
- * @memberOf module:decorator-validation.Construction
28
- */
29
- export declare function bindModelPrototype(obj: unknown): void;