@decaf-ts/decorator-validation 1.5.8 → 1.5.10

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