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