@decaf-ts/decorator-validation 1.0.7

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 (151) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +302 -0
  3. package/dist/decorator-validation.bundle.min.js +2 -0
  4. package/dist/decorator-validation.bundle.min.js.LICENSE.txt +14 -0
  5. package/dist/esm/decorator-validation.bundle.min.esm.js +2 -0
  6. package/dist/esm/decorator-validation.bundle.min.esm.js.LICENSE.txt +14 -0
  7. package/lib/esm/index.d.ts +61 -0
  8. package/lib/esm/index.js +1 -0
  9. package/lib/esm/model/Model.d.ts +160 -0
  10. package/lib/esm/model/Model.js +1 -0
  11. package/lib/esm/model/ModelErrorDefinition.d.ts +22 -0
  12. package/lib/esm/model/ModelErrorDefinition.js +3 -0
  13. package/lib/esm/model/Registry.d.ts +61 -0
  14. package/lib/esm/model/Registry.js +1 -0
  15. package/lib/esm/model/constants.d.ts +40 -0
  16. package/lib/esm/model/constants.js +1 -0
  17. package/lib/esm/model/construction.d.ts +45 -0
  18. package/lib/esm/model/construction.js +1 -0
  19. package/lib/esm/model/decorators.d.ts +33 -0
  20. package/lib/esm/model/decorators.js +1 -0
  21. package/lib/esm/model/index.d.ts +7 -0
  22. package/lib/esm/model/index.js +1 -0
  23. package/lib/esm/model/types.d.ts +67 -0
  24. package/lib/esm/model/types.js +0 -0
  25. package/lib/esm/utils/constants.d.ts +22 -0
  26. package/lib/esm/utils/constants.js +1 -0
  27. package/lib/esm/utils/general.d.ts +193 -0
  28. package/lib/esm/utils/general.js +1 -0
  29. package/lib/esm/utils/index.d.ts +4 -0
  30. package/lib/esm/utils/index.js +1 -0
  31. package/lib/esm/utils/registry.d.ts +68 -0
  32. package/lib/esm/utils/registry.js +0 -0
  33. package/lib/esm/utils/serialization.d.ts +65 -0
  34. package/lib/esm/utils/serialization.js +1 -0
  35. package/lib/esm/validation/Validation.d.ts +39 -0
  36. package/lib/esm/validation/Validation.js +1 -0
  37. package/lib/esm/validation/Validators/DateValidator.d.ts +29 -0
  38. package/lib/esm/validation/Validators/DateValidator.js +1 -0
  39. package/lib/esm/validation/Validators/EmailValidator.d.ts +29 -0
  40. package/lib/esm/validation/Validators/EmailValidator.js +1 -0
  41. package/lib/esm/validation/Validators/ListValidator.d.ts +29 -0
  42. package/lib/esm/validation/Validators/ListValidator.js +1 -0
  43. package/lib/esm/validation/Validators/MaxLengthValidator.d.ts +30 -0
  44. package/lib/esm/validation/Validators/MaxLengthValidator.js +1 -0
  45. package/lib/esm/validation/Validators/MaxValidator.d.ts +29 -0
  46. package/lib/esm/validation/Validators/MaxValidator.js +1 -0
  47. package/lib/esm/validation/Validators/MinLengthValidator.d.ts +30 -0
  48. package/lib/esm/validation/Validators/MinLengthValidator.js +1 -0
  49. package/lib/esm/validation/Validators/MinValidator.d.ts +29 -0
  50. package/lib/esm/validation/Validators/MinValidator.js +1 -0
  51. package/lib/esm/validation/Validators/PasswordValidator.d.ts +31 -0
  52. package/lib/esm/validation/Validators/PasswordValidator.js +1 -0
  53. package/lib/esm/validation/Validators/PatternValidator.d.ts +38 -0
  54. package/lib/esm/validation/Validators/PatternValidator.js +1 -0
  55. package/lib/esm/validation/Validators/RequiredValidator.d.ts +28 -0
  56. package/lib/esm/validation/Validators/RequiredValidator.js +1 -0
  57. package/lib/esm/validation/Validators/StepValidator.d.ts +29 -0
  58. package/lib/esm/validation/Validators/StepValidator.js +1 -0
  59. package/lib/esm/validation/Validators/TypeValidator.d.ts +28 -0
  60. package/lib/esm/validation/Validators/TypeValidator.js +1 -0
  61. package/lib/esm/validation/Validators/URLValidator.d.ts +28 -0
  62. package/lib/esm/validation/Validators/URLValidator.js +1 -0
  63. package/lib/esm/validation/Validators/Validator.d.ts +42 -0
  64. package/lib/esm/validation/Validators/Validator.js +1 -0
  65. package/lib/esm/validation/Validators/ValidatorRegistry.d.ts +47 -0
  66. package/lib/esm/validation/Validators/ValidatorRegistry.js +1 -0
  67. package/lib/esm/validation/Validators/constants.d.ts +91 -0
  68. package/lib/esm/validation/Validators/constants.js +1 -0
  69. package/lib/esm/validation/Validators/index.d.ts +50 -0
  70. package/lib/esm/validation/Validators/index.js +1 -0
  71. package/lib/esm/validation/Validators/types.d.ts +51 -0
  72. package/lib/esm/validation/Validators/types.js +0 -0
  73. package/lib/esm/validation/decorators.d.ts +263 -0
  74. package/lib/esm/validation/decorators.js +1 -0
  75. package/lib/esm/validation/index.d.ts +13 -0
  76. package/lib/esm/validation/index.js +1 -0
  77. package/lib/esm/validation/types.d.ts +53 -0
  78. package/lib/esm/validation/types.js +0 -0
  79. package/lib/index.cjs +1 -0
  80. package/lib/index.d.ts +61 -0
  81. package/lib/model/Model.cjs +1 -0
  82. package/lib/model/Model.d.ts +160 -0
  83. package/lib/model/ModelErrorDefinition.cjs +3 -0
  84. package/lib/model/ModelErrorDefinition.d.ts +22 -0
  85. package/lib/model/Registry.cjs +1 -0
  86. package/lib/model/Registry.d.ts +61 -0
  87. package/lib/model/constants.cjs +1 -0
  88. package/lib/model/constants.d.ts +40 -0
  89. package/lib/model/construction.cjs +1 -0
  90. package/lib/model/construction.d.ts +45 -0
  91. package/lib/model/decorators.cjs +1 -0
  92. package/lib/model/decorators.d.ts +33 -0
  93. package/lib/model/index.cjs +1 -0
  94. package/lib/model/index.d.ts +7 -0
  95. package/lib/model/types.cjs +1 -0
  96. package/lib/model/types.d.ts +67 -0
  97. package/lib/utils/constants.cjs +1 -0
  98. package/lib/utils/constants.d.ts +22 -0
  99. package/lib/utils/general.cjs +1 -0
  100. package/lib/utils/general.d.ts +193 -0
  101. package/lib/utils/index.cjs +1 -0
  102. package/lib/utils/index.d.ts +4 -0
  103. package/lib/utils/registry.cjs +1 -0
  104. package/lib/utils/registry.d.ts +68 -0
  105. package/lib/utils/serialization.cjs +1 -0
  106. package/lib/utils/serialization.d.ts +65 -0
  107. package/lib/validation/Validation.cjs +1 -0
  108. package/lib/validation/Validation.d.ts +39 -0
  109. package/lib/validation/Validators/DateValidator.cjs +1 -0
  110. package/lib/validation/Validators/DateValidator.d.ts +29 -0
  111. package/lib/validation/Validators/EmailValidator.cjs +1 -0
  112. package/lib/validation/Validators/EmailValidator.d.ts +29 -0
  113. package/lib/validation/Validators/ListValidator.cjs +1 -0
  114. package/lib/validation/Validators/ListValidator.d.ts +29 -0
  115. package/lib/validation/Validators/MaxLengthValidator.cjs +1 -0
  116. package/lib/validation/Validators/MaxLengthValidator.d.ts +30 -0
  117. package/lib/validation/Validators/MaxValidator.cjs +1 -0
  118. package/lib/validation/Validators/MaxValidator.d.ts +29 -0
  119. package/lib/validation/Validators/MinLengthValidator.cjs +1 -0
  120. package/lib/validation/Validators/MinLengthValidator.d.ts +30 -0
  121. package/lib/validation/Validators/MinValidator.cjs +1 -0
  122. package/lib/validation/Validators/MinValidator.d.ts +29 -0
  123. package/lib/validation/Validators/PasswordValidator.cjs +1 -0
  124. package/lib/validation/Validators/PasswordValidator.d.ts +31 -0
  125. package/lib/validation/Validators/PatternValidator.cjs +1 -0
  126. package/lib/validation/Validators/PatternValidator.d.ts +38 -0
  127. package/lib/validation/Validators/RequiredValidator.cjs +1 -0
  128. package/lib/validation/Validators/RequiredValidator.d.ts +28 -0
  129. package/lib/validation/Validators/StepValidator.cjs +1 -0
  130. package/lib/validation/Validators/StepValidator.d.ts +29 -0
  131. package/lib/validation/Validators/TypeValidator.cjs +1 -0
  132. package/lib/validation/Validators/TypeValidator.d.ts +28 -0
  133. package/lib/validation/Validators/URLValidator.cjs +1 -0
  134. package/lib/validation/Validators/URLValidator.d.ts +28 -0
  135. package/lib/validation/Validators/Validator.cjs +1 -0
  136. package/lib/validation/Validators/Validator.d.ts +42 -0
  137. package/lib/validation/Validators/ValidatorRegistry.cjs +1 -0
  138. package/lib/validation/Validators/ValidatorRegistry.d.ts +47 -0
  139. package/lib/validation/Validators/constants.cjs +1 -0
  140. package/lib/validation/Validators/constants.d.ts +91 -0
  141. package/lib/validation/Validators/index.cjs +1 -0
  142. package/lib/validation/Validators/index.d.ts +50 -0
  143. package/lib/validation/Validators/types.cjs +1 -0
  144. package/lib/validation/Validators/types.d.ts +51 -0
  145. package/lib/validation/decorators.cjs +1 -0
  146. package/lib/validation/decorators.d.ts +263 -0
  147. package/lib/validation/index.cjs +1 -0
  148. package/lib/validation/index.d.ts +13 -0
  149. package/lib/validation/types.cjs +1 -0
  150. package/lib/validation/types.d.ts +53 -0
  151. package/package.json +96 -0
@@ -0,0 +1,61 @@
1
+ import { Model } from "./Model";
2
+ import { BuilderRegistry } from "../utils";
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 interface ModelRegistry<T extends Model> extends BuilderRegistry<T> {
13
+ }
14
+ /**
15
+ * @summary Util class to enable serialization and correct rebuilding
16
+ *
17
+ * @param {string} anchorKey defaults to {@link ModelKeys.ANCHOR}. The property name where the registered class name is stored;
18
+ * @param {function(Record<string, any>): boolean} [testFunction] method to test if the provided object is a Model Object. defaults to {@link isModel}
19
+ *
20
+ * @class ModelRegistryManager
21
+ * @implements ModelRegistry
22
+ *
23
+ * @category Model
24
+ */
25
+ export declare class ModelRegistryManager<T extends Model> implements ModelRegistry<T> {
26
+ private cache;
27
+ private readonly testFunction;
28
+ private readonly anchorKey;
29
+ constructor(anchorKey?: string, testFunction?: (obj: Record<string, any>) => boolean);
30
+ /**
31
+ * @summary register new Models
32
+ * @param {any} constructor
33
+ * @param {string} [name] when not defined, the name of the constructor will be used
34
+ */
35
+ register(constructor: ModelConstructor<T>, name?: string): void;
36
+ /**
37
+ * @summary Gets a registered Model {@link ModelConstructor}
38
+ * @param {string} name
39
+ */
40
+ get(name: string): ModelConstructor<T> | undefined;
41
+ /**
42
+ * @param {Record<string, any>} obj
43
+ * @param {string} [clazz] when provided, it will attempt to find the matching constructor
44
+ *
45
+ * @throws Error If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
46
+ */
47
+ build(obj?: Record<string, any>, clazz?: string): T;
48
+ }
49
+ /**
50
+ * @summary Bulk Registers Models
51
+ * @description Useful when using bundlers that might not evaluate all of the code at once
52
+ *
53
+ * @param {Array<Constructor<T>> | Array<{name: string, constructor: Constructor<T>}>} [models]
54
+ *
55
+ * @memberOf module:decorator-validation.Model
56
+ * @category Model
57
+ */
58
+ export declare function bulkModelRegister<T extends Model>(...models: (Constructor<T> | {
59
+ name: string;
60
+ constructor: Constructor<T>;
61
+ })[]): void;
@@ -0,0 +1 @@
1
+ "use strict";var Primitives,ReservedModels;Object.defineProperty(exports,"__esModule",{value:!0}),exports.ReservedModels=exports.Primitives=void 0,function(e){e.STRING="string",e.NUMBER="number",e.BOOLEAN="boolean",e.BIGINT="bigint"}(Primitives||(exports.Primitives=Primitives={})),function(e){e.STRING="string",e.OBJECT="object",e.NUMBER="number",e.BOOLEAN="boolean",e.BIGINT="bigint",e.DATE="date"}(ReservedModels||(exports.ReservedModels=ReservedModels={}));
@@ -0,0 +1,40 @@
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.Construction
11
+ * @category Model
12
+ */
13
+ export declare enum Primitives {
14
+ STRING = "string",
15
+ NUMBER = "number",
16
+ BOOLEAN = "boolean",
17
+ BIGINT = "bigint"
18
+ }
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.Construction
31
+ * @category Construction
32
+ */
33
+ export declare enum ReservedModels {
34
+ STRING = "string",
35
+ OBJECT = "object",
36
+ NUMBER = "number",
37
+ BOOLEAN = "boolean",
38
+ BIGINT = "bigint",
39
+ DATE = "date"
40
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.construct=exports.constructFromModel=exports.constructFromObject=void 0;const Model_1=require("./Model/index.cjs"),utils_1=require("../utils/index.cjs"),validation_1=require("../validation/index.cjs"),constants_1=require("./constants/index.cjs"),constants_2=require("../utils/constants/index.cjs");function constructFromObject(o,t){if(t)for(const r in t)t.hasOwnProperty(r)&&(o.hasOwnProperty(r)||o.prototype&&o.prototype.hasOwnProperty(r))&&(o[r]=t[r]);return o}function constructFromModel(s,o){if(o)for(const n in o)if(o.hasOwnProperty(n)&&(s.hasOwnProperty(n)||s.prototype&&s.prototype.hasOwnProperty(n))){if(s[n]=o[n],"object"!=typeof s[n])continue;if((0,utils_1.isModel)(s[n])){try{s[n]=Model_1.Model.build(s[n])}catch(o){console.log(o)}continue}const c=(0,utils_1.getPropertyDecorators)(validation_1.ValidationKeys.REFLECT,s,n).decorators;if(!(t=c.filter(o=>-1!==[constants_2.ModelKeys.TYPE,validation_1.ValidationKeys.TYPE].indexOf(o.key)))||!t.length)throw new Error((0,utils_1.stringFormat)("failed to find decorators for property {0}",n));var t=(t=t.pop()).props.name?[t.props.name]:Array.isArray(t.props.customTypes)?t.props.customTypes:[t.props.customTypes];const i=Object.values(constants_1.ReservedModels).map(o=>o.toLowerCase());t.forEach(o=>{if(-1===i.indexOf(o.toLowerCase()))try{switch(o){case"Array":case"Set":if(c.length){const r=c.find(o=>o.key===validation_1.ValidationKeys.LIST);if(r&&("Array"===o&&(s[n]=s[n].map(o=>Model_1.Model.build(o,r.props.class))),"Set"===o)){var t=new Set;for(const e of s[n])t.add(Model_1.Model.build(e,r.props.class));s[n]=t}}break;default:s[n]&&(s[n]=Model_1.Model.build(s[n],o))}}catch(o){console.log(o)}})}return s}function construct(t,...o){var r=(...o)=>new t(...o);return r.prototype=t.prototype,r(...o)}exports.constructFromObject=constructFromObject,exports.constructFromModel=constructFromModel,exports.construct=construct;
@@ -0,0 +1,45 @@
1
+ import { Model } from "./Model";
2
+ /**
3
+ * @summary Repopulates the Object properties with the ones from the new object
4
+ * @description Iterates all common properties of obj (if existing) and self, and copies them onto self
5
+ *
6
+ * @param {T} self
7
+ * @param {T | Record<string, any>} [obj]
8
+ *
9
+ * @function constructFromObject
10
+ * @memberOf module:decorator-validation.Construction
11
+ * @category Construction
12
+ */
13
+ export declare function constructFromObject<T extends Model>(self: T, obj?: T | Record<string, any>): T;
14
+ /**
15
+ * @summary Repopulates the instance with the ones from the new Model Object
16
+ * @description Iterates all common properties of obj (if existing) and self, and copies them onto self.
17
+ * Is aware of nested Model Objects and rebuilds them also.
18
+ * When List properties are decorated with {@link list}, they list items will also be rebuilt
19
+ *
20
+ * @param {T} self
21
+ * @param {T | Record<string, any>} [obj]
22
+ *
23
+ * @function constructFromModel
24
+ * @memberOf module:decorator-validation.Construction
25
+ * @category Construction
26
+ */
27
+ export declare function constructFromModel<T extends Model>(self: T, obj?: T | Record<string, any>): T;
28
+ /**
29
+ * @summary Helper Function to override constructors
30
+ *
31
+ * @param {Function} constructor
32
+ * @param {any[]} [args]
33
+ * @return {T} the new instance
34
+ *
35
+ * @function construct
36
+ * @memberOf module:decorator-validation.Construction
37
+ * @category Construction
38
+ */
39
+ export declare function construct<T extends Model>(constructor: any, ...args: any[]): T;
40
+ /**
41
+ * @summary Typo of a Model builder function
42
+ * @memberOf module:decorator-validation.Construction
43
+ * @category Construction
44
+ */
45
+ export type ModelBuilderFunction = <T extends Model>(self: T, obj?: T | Record<string, any>) => T;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.model=exports.getModelKey=void 0;const construction_1=require("./construction/index.cjs"),constants_1=require("../utils/constants/index.cjs"),Model_1=require("./Model/index.cjs"),getModelKey=e=>constants_1.ModelKeys.REFLECT+e;function model(n,s){return r=>{function e(...e){var t=(0,construction_1.construct)(r,...e),o=((o=Model_1.Model.getBuilder())&&o(t,e.length?e[0]:void 0),Object.assign({},{class:r.name}));return Object.defineProperty(t,constants_1.ModelKeys.ANCHOR,{writable:!1,enumerable:!1,configurable:!1,value:o}),Reflect.defineMetadata((0,exports.getModelKey)(constants_1.ModelKeys.MODEL),Object.assign(o,n||{}),t.constructor),s&&s(t,...e),t}return e.prototype=r.prototype,Object.defineProperty(e,"name",{writable:!1,enumerable:!0,configurable:!1,value:r.prototype.constructor.name}),Model_1.Model.register(e),e}}exports.getModelKey=getModelKey,exports.model=model;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * @summary Builds the key to store as Metadata under Reflections
3
+ * @description concatenates {@link ModelKeys#REFLECT} with the provided key
4
+ * @param {string} str
5
+ *
6
+ * @function getModelKey
7
+ * @memberOf module:decorator-validation.Model
8
+ * @category Utilities
9
+ */
10
+ export declare const getModelKey: (str: string) => string;
11
+ /**
12
+ * @summary defines the tpe os an InstanceCallback function
13
+ * @memberOf module:decorator-validation.Construction
14
+ * @category Construction
15
+ */
16
+ export type InstanceCallback = (instance: any, ...args: any[]) => void;
17
+ /**
18
+ * @summary Defines a class as a Model class
19
+ * @description
20
+ * - Registers the class under the model registry so it can be easily rebuilt;
21
+ * - Overrides the class constructor;
22
+ * - Runs the global {@link ModelBuilderFunction} if defined;
23
+ * - Runs the optional {@link InstanceCallback} if provided;
24
+ * - Defines an {@link ModelKeys#ANCHOR} property for serialization and model rebuilding purposes;
25
+ *
26
+ * @prop {Record<string, any>} [props] additional properties to store as metadata
27
+ * @prop {InstanceCallback} [instanceCallback] optional callback that will be call with the instance upon instantiation. defaults to undefined
28
+ *
29
+ * @function model
30
+ * @memberOf module:decorator-validation.Decorators.Model
31
+ * @category Decorators
32
+ */
33
+ export declare function model(props?: Record<string, any>, instanceCallback?: InstanceCallback): (original: any) => any;
@@ -0,0 +1 @@
1
+ "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,r,t,o){void 0===o&&(o=t);var i=Object.getOwnPropertyDescriptor(r,t);i&&("get"in i?r.__esModule:!i.writable&&!i.configurable)||(i={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,o,i)}:function(e,r,t,o){e[o=void 0===o?t:o]=r[t]}),__exportStar=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||Object.prototype.hasOwnProperty.call(r,t)||__createBinding(r,e,t)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./construction/index.cjs"),exports),__exportStar(require("./Registry/index.cjs"),exports),__exportStar(require("./decorators/index.cjs"),exports),__exportStar(require("./ModelErrorDefinition/index.cjs"),exports),__exportStar(require("./types/index.cjs"),exports),__exportStar(require("./Model/index.cjs"),exports),__exportStar(require("./constants/index.cjs"),exports);
@@ -0,0 +1,7 @@
1
+ export * from "./construction";
2
+ export * from "./Registry";
3
+ export * from "./decorators";
4
+ export * from "./ModelErrorDefinition";
5
+ export * from "./types";
6
+ export * from "./Model";
7
+ export * from "./constants";
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});
@@ -0,0 +1,67 @@
1
+ import { ModelErrorDefinition } from "./ModelErrorDefinition";
2
+ import { Model } from "./Model";
3
+ /**
4
+ * @summary Definition of a Model Constructor Argument
5
+ *
6
+ * @memberOf module:decorator-validation.Model
7
+ * @category Model
8
+ *
9
+ * @see ModelConstructor
10
+ */
11
+ export type ModelArg<T> = T | Record<string, any>;
12
+ /**
13
+ * @summary Definition of a Class Constructor
14
+ * @description Generic type for Constructor functions
15
+ *
16
+ * @typedef Constructor
17
+ *
18
+ * @param {any[]} [args]
19
+ * @memberOf module:decorator-validation.Model
20
+ * @category Model
21
+ */
22
+ export type Constructor<T> = {
23
+ new (...args: any[]): T;
24
+ };
25
+ /**
26
+ * @summary Definition of a Model Constructor
27
+ * @description Generic type for all Model Constructor functions
28
+ *
29
+ * @typedef ModelConstructor
30
+ *
31
+ * @param {ModelArg<T>} [model]
32
+ * @param {any[]} [args]
33
+ * @memberOf module:decorator-validation.Model
34
+ * @category Construction
35
+ */
36
+ export type ModelConstructor<T extends Model> = {
37
+ new (model?: ModelArg<T>, ...args: any[]): T;
38
+ };
39
+ /**
40
+ * @summary Defines the Validation API for validatable models
41
+ * @interface Validatable
42
+ *
43
+ * @category Validation
44
+ */
45
+ export interface Validatable {
46
+ /**
47
+ * @summary Validates the model and returns the {@link ModelErrorDefinition} if any
48
+ * @param {any} [args]
49
+ *
50
+ * @method
51
+ */
52
+ hasErrors(...args: any[]): ModelErrorDefinition | undefined;
53
+ }
54
+ /**
55
+ * @summary Serializable interface
56
+ *
57
+ * @interface Serializable
58
+ *
59
+ * @category Serialization
60
+ */
61
+ export interface Serializable {
62
+ /**
63
+ * @summary serializes the model
64
+ * @method
65
+ */
66
+ serialize(): string;
67
+ }
@@ -0,0 +1 @@
1
+ "use strict";var ModelKeys;Object.defineProperty(exports,"__esModule",{value:!0}),exports.ModelKeys=void 0,function(e){e.REFLECT="model.definition.",e.TYPE="design:type",e.PARAMS="design:paramtypes",e.RETURN="design:returntype",e.MODEL="model",e.ANCHOR="__modelDefinition"}(ModelKeys||(exports.ModelKeys=ModelKeys={}));
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @summary Defines the various Model keys used for reflection
3
+ *
4
+ * @property {string} REFLECT prefix to all other keys
5
+ * @property {string} TYPE type key
6
+ * @property {string} PARAMS method params key
7
+ * @property {string} RETURN method return key
8
+ * @property {string} MODEL model key
9
+ * @property {string} ANCHOR anchor key. will serve as a ghost property in the model
10
+ *
11
+ * @constant ModelKeys
12
+ * @memberOf module:decorator-validation.Model
13
+ * @category Model
14
+ */
15
+ export declare enum ModelKeys {
16
+ REFLECT = "model.definition.",
17
+ TYPE = "design:type",
18
+ PARAMS = "design:paramtypes",
19
+ RETURN = "design:returntype",
20
+ MODEL = "model",
21
+ ANCHOR = "__modelDefinition"
22
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.dateFromFormat=exports.evaluateDesignTypes=exports.checkTypes=exports.checkType=exports.getClassDecorators=exports.isModel=exports.hashObj=exports.hashSerialization=exports.hashCode=exports.isEqual=exports.stringFormat=exports.getAllProperties=exports.getAllPropertyDecorators=exports.getTypeFromDecorator=exports.getPropertyDecorators=void 0,require("reflect-metadata");const validation_1=require("../validation/index.cjs"),constants_1=require("./constants/index.cjs"),model_1=require("../model/index.cjs");function getPropertyDecorators(e,r,t,o=!1,s=!0,a){o=function(r,o,s,t=!1,e){e=Reflect.getMetadataKeys(o,s).filter(e=>!t&&e===constants_1.ModelKeys.TYPE||e.toString().startsWith(r)).reduce((e,t)=>{t={key:t!==constants_1.ModelKeys.TYPE?t.substring(r.length):t,props:Reflect.getMetadata(t,o,s)};return e.concat(t)},e||[]);return{prop:s.toString(),decorators:e}}(e,r,t,o,a);return s&&Object.getPrototypeOf(r)!==Object.prototype?getPropertyDecorators(e,Object.getPrototypeOf(r.constructor),t,!0,s,o.decorators):{prop:o.prop,decorators:function(e){const t={};return e.filter(e=>e.key in t?(isEqual(e.props,t[e.key])||console.log(stringFormat("Found a similar decorator for the {0} property of a {1} model but with different attributes. The original one will be kept",e.key,r.constructor.name)),!1):(t[e.key.toString()]=e.props,!0))}(o.decorators)}}function getTypeFromDecorator(e,t){var e=getPropertyDecorators(constants_1.ModelKeys.REFLECT,e,t,!1);return e&&e.decorators&&"Function"!==(e=(t=e.decorators.shift()).props?t.props.name:void 0)?e:void 0}exports.getPropertyDecorators=getPropertyDecorators,exports.getTypeFromDecorator=getTypeFromDecorator;const getAllPropertyDecorators=function(a,...e){if(e&&e.length)return Object.getOwnPropertyNames(a).reduce((o,s)=>(e.forEach((e,t)=>{var r,e=getPropertyDecorators(e,a,s,0!==t);t=o=o||{},r=s,(e=e.decorators)&&e.length&&(t[r]||(t[r]=[]),t[r].push(...e))}),o),void 0)};function getAllProperties(e,t=!0,r="Object"){const o=[];let s=e;do{Object.getOwnPropertyNames(s).forEach(function(e){-1===o.indexOf(e)&&o.push(e)})}while(function(){if(t){var e=Object.getPrototypeOf(s);if(e&&e.constructor.name!==r)return s=e}}());return o}function stringFormat(e,...r){return e.replace(/{(\d+)}/g,function(e,t){return void 0!==r[t]?r[t]:e})}function isEqual(t,r,...o){var e;return t===r||(t instanceof Date&&r instanceof Date?t.getTime()===r.getTime():!t||!r||"object"!=typeof t&&"object"!=typeof r?t===r:null!=t&&null!=r&&typeof t==typeof r&&t.prototype===r.prototype&&(e=Object.keys(t).filter(e=>-1===o.indexOf(e))).length===Object.keys(r).filter(e=>-1===o.indexOf(e)).length&&e.every(e=>-1!==o.indexOf(e)||isEqual(t[e],r[e],...o)))}function hashCode(t){t=String(t);let r=0;for(let e=0;e<t.length;e++){var o=t.charCodeAt(e);r=(r<<5)-r+o,r&=r}return r}function hashSerialization(e){return hashCode(model_1.Model.serialize(e))}function hashObj(e){function o(e){return void 0===e?"":-1!==["string","number","symbol"].indexOf(typeof e)?r(e.toString()):e instanceof Date?r(e.getTime()):(Array.isArray(e)?e:Object.values(e)).reduce(t,void 0)}const t=function(e,t){var r=o(t);return"string"==typeof r?o((e||"")+o(t)):(e=((e=e||0)<<5)-e+r)&e},r=hashCode;e=Object.values(e).reduce(t,0);return"number"==typeof e?Math.abs(e):e}function isModel(e){return!!e[constants_1.ModelKeys.ANCHOR]||!!getClassDecorators(constants_1.ModelKeys.REFLECT,e).find(e=>e.key===constants_1.ModelKeys.MODEL&&e.props&&e.props.class)}function getClassDecorators(r,o){return Reflect.getOwnMetadataKeys(o.constructor).filter(e=>e.toString().startsWith(r)).reduce((e,t)=>{t={key:t.substring(r.length),props:Reflect.getMetadata(t,o.constructor)};return e.concat(t)},[])}function checkType(e,t){return typeof e===t||e.constructor&&e.constructor.name.toLowerCase()===t.toLowerCase()}function checkTypes(t,e){return!e.every(e=>!checkType(t,e))}function evaluateDesignTypes(e,t){switch(typeof t){case"string":return checkType(e,t);case"object":return Array.isArray(t)?checkTypes(e,t):!0;case"function":return t.name&&"Object"!==t.name?checkType(e,t.name):!0;default:return!0}}function dateFromFormat(e,t){let r=t;r.match(/hh/)?r=r.replace("hh","(?<hour>\\d{2})"):r.match(/h/)?r=r.replace("h","(?<hour>\\d{1,2})"):r.match(/HH/)?r=r.replace("HH","(?<hour>\\d{2})"):r.match(/H/)&&(r=r.replace("H","(?<hour>\\d{1,2})")),r.match(/mm/)?r=r.replace("mm","(?<minutes>\\d{2})"):r.match(/m/)&&(r=r.replace("m","(?<minutes>\\d{1,2})")),r.match(/ss/)?r=r.replace("ss","(?<seconds>\\d{2})"):r.match(/s/)&&(r=r.replace("s","(?<seconds>\\d{1,2})")),r.match(/dd/)?r=r.replace("dd","(?<day>\\d{2})"):r.match(/d/)&&(r=r.replace("d","(?<day>\\d{1,2})")),r.match(/EEEE/)?r=r.replace("EEEE","(?<dayofweek>\\w+)"):r.match(/EEEE/)&&(r=r.replace("EEE","(?<dayofweek>\\w+)")),r.match(/yyyy/)?r=r.replace("yyyy","(?<year>\\d{4})"):r.match(/yy/)&&(r=r.replace("yy","(?<year>\\d{2})")),r.match(/MMMM/)?r=r.replace("MMMM","(?<monthname>\\w+)"):r.match(/MMM/)&&(r=r.replace("MMM","(?<monthnamesmall>\\w+)")),r.match(/MM/)?r=r.replace("MM","(?<month>\\d{2})"):r.match(/M/)&&(r=r.replace("M","(?<month>\\d{1,2})")),r=r.replace("S","(?<milis>\\d{1,3})").replace("aaa","(?<ampm>\\w{2})");t=new RegExp(r,"g").exec(e);if(!t||!t.groups)return new Date(e);function o(e){return!e||(e=parseInt(e),isNaN(e))?0:e}var s=o(t.groups.year),a=o(t.groups.day),n=t.groups.ampm;let c=o(t.groups.hour);n&&(c="PM"===n?c+12:c);var n=o(t.groups.minutes),p=o(t.groups.seconds),i=o(t.groups.milis),l=t.groups.monthname;const u=t.groups.monthnamesmall;let d=t.groups.month;if(l)d=validation_1.MONTH_NAMES.indexOf(l);else if(u){t=validation_1.MONTH_NAMES.find(e=>e.toLowerCase().startsWith(u.toLowerCase()));if(!t)return new Date(e);d=validation_1.MONTH_NAMES.indexOf(t)}else d=o(""+d);return new Date(s,d-1,a,c,n,p,i)}exports.getAllPropertyDecorators=getAllPropertyDecorators,exports.getAllProperties=getAllProperties,exports.stringFormat=stringFormat,exports.isEqual=isEqual,exports.hashCode=hashCode,exports.hashSerialization=hashSerialization,exports.hashObj=hashObj,exports.isModel=isModel,exports.getClassDecorators=getClassDecorators,exports.checkType=checkType,exports.checkTypes=checkTypes,exports.evaluateDesignTypes=evaluateDesignTypes,exports.dateFromFormat=dateFromFormat;
@@ -0,0 +1,193 @@
1
+ import "reflect-metadata";
2
+ import { DecoratorMetadata } from "../validation";
3
+ import { Model } from "../model";
4
+ /**
5
+ * @summary Util function to retrieve the decorators for the provided Property
6
+ *
7
+ * @param {string} annotationPrefix
8
+ * @param {any} target
9
+ * @param {string | symbol} propertyName
10
+ * @param {boolean} [ignoreType] defaults to false. decides if the {@link ModelKeys.TYPE} is ignored or not
11
+ * @param {boolean} [recursive] defaults to true. decides if it should climb the prototypal tree searching for more decorators on that property
12
+ * @param {DecoratorMetadata[]} [accumulator] used when recursive is true, to cache decorators while it climbs the prototypal tree
13
+ *
14
+ * @function getPropertyDecorators
15
+ * @memberOf module:decorator-validation.Reflection
16
+ * @category Reflection
17
+ */
18
+ export declare function getPropertyDecorators(annotationPrefix: string, target: any, propertyName: string | symbol, ignoreType?: boolean, recursive?: boolean, accumulator?: DecoratorMetadata[]): {
19
+ prop: string;
20
+ decorators: DecoratorMetadata[];
21
+ };
22
+ /**
23
+ * @summary gets the prop type from the decorator
24
+ * @param {any} model
25
+ * @param {string | symbol} propKey
26
+ * @return {string | undefined}
27
+ *
28
+ * @function geTypeFromDecorators
29
+ *
30
+ * @memberOf module:decorator-validation.Reflection
31
+ */
32
+ export declare function getTypeFromDecorator(model: any, propKey: string | symbol): string | undefined;
33
+ /**
34
+ * @summary Retrieves the decorators for an object's properties prefixed by {@param prefixes}
35
+ *
36
+ * @param {T} model
37
+ * @param {string[]} prefixes
38
+ *
39
+ * @function getAllPropertyDecorators
40
+ *
41
+ * @memberOf module:db-decorators.Reflection
42
+ */
43
+ export declare const getAllPropertyDecorators: <T extends Model>(model: T, ...prefixes: string[]) => Record<string, any> | undefined;
44
+ /**
45
+ * @summary Retrieves all properties of an object
46
+ * @description
47
+ * - and of all its prototypes if {@param climbTree} until it reaches {@param stopAt} (or ends the prototype chain)
48
+ *
49
+ * @param obj
50
+ * @param {boolean} [climbTree] default to true
51
+ * @param {string} [stopAt] defaults to 'Object'
52
+ *
53
+ * @function getAllProperties
54
+ *
55
+ * @memberOf module:decorator-validation.Model
56
+ */
57
+ export declare function getAllProperties(obj: Record<any, any>, climbTree?: boolean, stopAt?: string): string[];
58
+ /**
59
+ * @summary Util function to provide string format functionality similar to C#'s string.format
60
+ *
61
+ * @param {string} string
62
+ * @param {string} args replacements made by order of appearance (replacement0 wil replace {0} and so on)
63
+ * @return {string} formatted string
64
+ *
65
+ * @function stringFormat
66
+ * @memberOf module:decorator-validation.Utils.Format
67
+ * @category Format
68
+ */
69
+ export declare function stringFormat(string: string, ...args: string[]): string;
70
+ /**
71
+ * @summary Deep Object Comparison
72
+ * @description algorithm from {@link https://stackoverflow.com/questions/30476150/javascript-deep-comparison-recursively-objects-and-properties}
73
+ * but with optional ignored properties
74
+ *
75
+ * @param {any} a
76
+ * @param {any} b
77
+ * @param {string} [propsToIgnore]
78
+ *
79
+ * @function isEqual
80
+ * @memberOf module:decorator-validation.Utils.Equality
81
+ * @category Validation
82
+ */
83
+ export declare function isEqual(a: any, b: any, ...propsToIgnore: string[]): boolean;
84
+ /**
85
+ * @summary Mimics Java's String's Hash implementation
86
+ *
87
+ * @param {string | number | symbol | Date} obj
88
+ * @return {number} hash value of obj
89
+ *
90
+ * @function hashCode
91
+ * @memberOf module:decorator-validation.Utils.Hashing
92
+ * @category Hashing
93
+ */
94
+ export declare function hashCode(obj: string | number | symbol | Date): number;
95
+ /**
96
+ * @summary Defines teh type for a Hashing function
97
+ * @memberOf module:decorator-validation.Utils.Hashing
98
+ * @category Hashing
99
+ */
100
+ export type HashingFunction = (value: any) => string | number;
101
+ /**
102
+ * @summary Hashes an object serializing it and then hashing the string
103
+ * @description The Serialization algorithm used by default (JSON.stringify)
104
+ * is not deterministic and should not be used for hashing
105
+ *
106
+ * @param {Record<string, any>} obj
107
+ * @return {string} the resulting hash
108
+ *
109
+ * @function hashSerialization
110
+ * @memberOf module:decorator-validation.Utils.Hashing
111
+ *
112
+ * @category Hashing
113
+ */
114
+ export declare function hashSerialization(obj: Record<string, any> | any[]): number;
115
+ /**
116
+ * @summary Hashes an object by combining the hash of all its properties
117
+ *
118
+ * @param {Record<string, any>} obj
119
+ * @return {string} the resulting hash
120
+ *
121
+ * @function hashObj
122
+ * @memberOf module:decorator-validation.Utils.Hashing
123
+ * @category Hashing
124
+ */
125
+ export declare function hashObj(obj: Record<string, any> | any[]): string | number;
126
+ /**
127
+ * @summary For Serialization/deserialization purposes.
128
+ * @description Reads the {@link ModelKeys.ANCHOR} property of a {@link Model} to discover the class to instantiate
129
+ *
130
+ * @function isModel
131
+ * @memberOf module:decorator-validation.Validation
132
+ * @category Validation
133
+ */
134
+ export declare function isModel(target: Record<string, any>): boolean;
135
+ /**
136
+ * @summary Util function to retrieve the Class decorators
137
+ *
138
+ * @function getClassDecorators
139
+ * @memberOf module:decorator-validation.Reflection
140
+ * @category Reflection
141
+ */
142
+ export declare function getClassDecorators(annotationPrefix: string, target: any): {
143
+ key: string;
144
+ props: any;
145
+ }[];
146
+ /**
147
+ * @summary Util function to check a type according to a typeName
148
+ *
149
+ * @param {any} value
150
+ * @param {string} acceptedType
151
+ * @return {boolean} true for a match, false otherwise
152
+ *
153
+ * @function checkType
154
+ * @memberOf module:decorator-validation.Validation
155
+ * @category Validation
156
+ */
157
+ export declare function checkType(value: any, acceptedType: string): any;
158
+ /**
159
+ * @summary Util function to check a type according multiple possibilities
160
+ * @param {any} value
161
+ * @param {string[]} acceptedTypes
162
+ * @return {boolean} true if any is a match, false otherwise
163
+ *
164
+ * @function checkTypes
165
+ * @memberOf module:decorator-validation.Validation
166
+ * @category Validation
167
+ */
168
+ export declare function checkTypes(value: any, acceptedTypes: string[]): boolean;
169
+ /**
170
+ * @summary The model type
171
+ *
172
+ * @param {any} value
173
+ * @param {string | string[] | {name: string}} types
174
+ *
175
+ * @function evaluateDesignTypes
176
+ * @memberOf module:decorator-validation.Validation
177
+ * @category Validation
178
+ */
179
+ export declare function evaluateDesignTypes(value: any, types: string | string[] | {
180
+ name: string;
181
+ }): any;
182
+ /**
183
+ * @summary Reverses the process from {@link formatDate}
184
+ *
185
+ * @param {string} date the date string to be converted back into date
186
+ * @param {string} format the date format
187
+ * @return {Date} the date from the format or the standard new Date({@prop date}) if the string couldn't be parsed (are you sure the format matches the string?)
188
+ *
189
+ * @function dateFromFormat
190
+ * @memberOf module:decorator-validation.Utils.Dates
191
+ * @category Format
192
+ */
193
+ export declare function dateFromFormat(date: string, format: string): Date;
@@ -0,0 +1 @@
1
+ "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,r,t,i){void 0===i&&(i=t);var o=Object.getOwnPropertyDescriptor(r,t);o&&("get"in o?r.__esModule:!o.writable&&!o.configurable)||(o={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,i,o)}:function(e,r,t,i){e[i=void 0===i?t:i]=r[t]}),__exportStar=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||Object.prototype.hasOwnProperty.call(r,t)||__createBinding(r,e,t)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./general/index.cjs"),exports),__exportStar(require("./registry/index.cjs"),exports),__exportStar(require("./serialization/index.cjs"),exports),__exportStar(require("./constants/index.cjs"),exports);
@@ -0,0 +1,4 @@
1
+ export * from "./general";
2
+ export * from "./registry";
3
+ export * from "./serialization";
4
+ export * from "./constants";
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});
@@ -0,0 +1,68 @@
1
+ import { Constructor } from "../model/types";
2
+ /**
3
+ * @summary Basic interface for Registries
4
+ *
5
+ * @interface IRegistry
6
+ *
7
+ * @category Utilities
8
+ */
9
+ export interface IRegistry<T> {
10
+ /**
11
+ * @summary Registers an Object
12
+ *
13
+ * @param {T} obj
14
+ * @param {any[]} args
15
+ *
16
+ * @method
17
+ */
18
+ register(obj: T | any, ...args: any[]): void;
19
+ /**
20
+ * @summary Retrieves an Object if it can find it
21
+ *
22
+ * @param {any} key
23
+ * @param {any[]} args
24
+ * @return {T | undefined}
25
+ *
26
+ * @method
27
+ */
28
+ get(key: any, ...args: any[]): T | undefined;
29
+ }
30
+ /**
31
+ * @summary Basic Builder Registry Interface
32
+ *
33
+ * @typedef T
34
+ * @interface BuilderRegistry<T>
35
+ *
36
+ * @category Construction
37
+ */
38
+ export interface BuilderRegistry<T> extends IRegistry<Constructor<T>> {
39
+ /**
40
+ * @summary Retrieves an Builder Object by name if it can
41
+ *
42
+ * @param {string} name
43
+ * @param {any[]} args
44
+ *
45
+ * @method
46
+ */
47
+ get(name: string, ...args: any[]): Constructor<T> | undefined;
48
+ /**
49
+ * @summary Registers a constructor by name
50
+ *
51
+ * @param {Constructor<T>} [constructor]
52
+ * @param {name} name
53
+ * @param {any[]} args
54
+ *
55
+ * @method
56
+ */
57
+ register(constructor: Constructor<T>, name?: string, ...args: any[]): void;
58
+ /**
59
+ * @summary Builds an Object by name
60
+ *
61
+ * @param {{}} obj
62
+ * @param {any[]} args
63
+ * @return T
64
+ *
65
+ * @method
66
+ */
67
+ build(obj: Record<string, any> | T, ...args: any[]): T;
68
+ }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.JSONSerializer=void 0;const Model_1=require("../model/Model/index.cjs"),constants_1=require("./constants/index.cjs");class JSONSerializer{preSerialize(e){var r=Object.assign({},e);return r[constants_1.ModelKeys.ANCHOR]=e[constants_1.ModelKeys.ANCHOR],r}deserialize(e){e=JSON.parse(e);return Model_1.Model.build(e)}serialize(e){return JSON.stringify(this.preSerialize(e))}}exports.JSONSerializer=JSONSerializer;
@@ -0,0 +1,65 @@
1
+ import { Model } from "../model/Model";
2
+ /**
3
+ * @summary Helper in serialization
4
+ *
5
+ * @interface Serializer
6
+ * @category Serialization
7
+ */
8
+ export interface Serializer<T extends Model> {
9
+ /**
10
+ * @summary Serializes a model
11
+ * @param {T} model
12
+ *
13
+ * @method
14
+ *
15
+ * @throws {Error}
16
+ */
17
+ serialize(model: T): string;
18
+ /**
19
+ * @summary Rebuilds a model from serialization
20
+ * @param {string} str
21
+ *
22
+ * @method
23
+ *
24
+ * @throws {Error}
25
+ */
26
+ deserialize(str: string): T;
27
+ }
28
+ /**
29
+ * @summary Concrete implementation of a {@link Serializer} in JSON format
30
+ * @description JS's native JSON.stringify (used here) is not deterministic
31
+ * and therefore should not be used for hashing purposes
32
+ *
33
+ * To keep dependencies low, we will not implement this, but we recommend
34
+ * implementing a similar {@link JSONSerializer} using 'deterministic-json' libraries
35
+ *
36
+ * @class JSONSerializer
37
+ * @implements Serializer
38
+ *
39
+ * @category Serialization
40
+ */
41
+ export declare class JSONSerializer<T extends Model> implements Serializer<T> {
42
+ /**
43
+ * @summary prepares the model for serialization
44
+ * @description returns a shallow copy of the object, containing an enumerable {@link ModelKeys#ANCHOR} property
45
+ * so the object can be recognized upon deserialization
46
+ *
47
+ * @param {T} model
48
+ * @protected
49
+ */
50
+ protected preSerialize(model: T): {} & T;
51
+ /**
52
+ * @summary Rebuilds a model from a serialization
53
+ * @param {string} str
54
+ *
55
+ * @throws {Error} If it fails to parse the string, or to build the model
56
+ */
57
+ deserialize(str: string): T;
58
+ /**
59
+ * @summary Serializes a model
60
+ * @param {T} model
61
+ *
62
+ * @throws {Error} if fails to serialize
63
+ */
64
+ serialize(model: T): string;
65
+ }