@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,25 +0,0 @@
1
- /**
2
- * @summary defines the tpe os an InstanceCallback function
3
- *
4
- * @memberOf module:decorator-validation.Model
5
- */
6
- export type InstanceCallback = (instance: any, ...args: any[]) => void;
7
- /**
8
- * @summary Defines a class as a Model class
9
- * @description
10
- *
11
- * - Registers the class under the model registry so it can be easily rebuilt;
12
- * - Overrides the class constructor;
13
- * - Runs the global {@link ModelBuilderFunction} if defined;
14
- * - Runs the optional {@link InstanceCallback} if provided;
15
- *
16
- * @param {InstanceCallback} [instanceCallback] optional callback that will be called with the instance upon instantiation. defaults to undefined
17
- *
18
- * @function model
19
- *
20
- * @memberOf module:decorator-validation.Model
21
- *
22
- */
23
- export declare function model(instanceCallback?: InstanceCallback): any;
24
- export declare function hashedBy(algorithm: string, ...args: any[]): (target: object, propertyKey?: string | symbol | unknown, descriptor?: PropertyDescriptor) => void;
25
- export declare function serializedBy(serializer: string, ...args: any[]): (target: object, propertyKey?: string | symbol | unknown, descriptor?: PropertyDescriptor) => void;
@@ -1,67 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.model = model;
4
- exports.hashedBy = hashedBy;
5
- exports.serializedBy = serializedBy;
6
- const construction_1 = require("./construction");
7
- const constants_1 = require("../utils/constants");
8
- const Model_1 = require("./Model");
9
- const reflection_1 = require("@decaf-ts/reflection");
10
- /**
11
- * @summary Defines a class as a Model class
12
- * @description
13
- *
14
- * - Registers the class under the model registry so it can be easily rebuilt;
15
- * - Overrides the class constructor;
16
- * - Runs the global {@link ModelBuilderFunction} if defined;
17
- * - Runs the optional {@link InstanceCallback} if provided;
18
- *
19
- * @param {InstanceCallback} [instanceCallback] optional callback that will be called with the instance upon instantiation. defaults to undefined
20
- *
21
- * @function model
22
- *
23
- * @memberOf module:decorator-validation.Model
24
- *
25
- */
26
- function model(instanceCallback) {
27
- return ((original) => {
28
- // the new constructor behaviour
29
- const newConstructor = function (...args) {
30
- const instance = (0, construction_1.construct)(original, ...args);
31
- (0, construction_1.bindModelPrototype)(instance);
32
- // run a builder function if defined with the first argument (The ModelArg)
33
- const builder = Model_1.Model.getBuilder();
34
- if (builder)
35
- builder(instance, args.length ? args[0] : undefined);
36
- (0, reflection_1.metadata)(Model_1.Model.key(constants_1.ModelKeys.MODEL), original.name)(instance.constructor);
37
- if (instanceCallback)
38
- instanceCallback(instance, ...args);
39
- return instance;
40
- };
41
- // copy prototype so instanceof operator still works
42
- newConstructor.prototype = original.prototype;
43
- // Sets the proper constructor name for type verification
44
- Object.defineProperty(newConstructor, "name", {
45
- writable: false,
46
- enumerable: true,
47
- configurable: false,
48
- value: original.prototype.constructor.name,
49
- });
50
- (0, reflection_1.metadata)(Model_1.Model.key(constants_1.ModelKeys.MODEL), original.name)(original);
51
- Model_1.Model.register(newConstructor, original.name);
52
- // return new constructor (will override original)
53
- return newConstructor;
54
- });
55
- }
56
- function hashedBy(algorithm, ...args) {
57
- return (0, reflection_1.metadata)(Model_1.Model.key(constants_1.ModelKeys.HASHING), {
58
- algorithm: algorithm,
59
- args: args,
60
- });
61
- }
62
- function serializedBy(serializer, ...args) {
63
- return (0, reflection_1.metadata)(Model_1.Model.key(constants_1.ModelKeys.SERIALIZATION), {
64
- serializer: serializer,
65
- args: args,
66
- });
67
- }
@@ -1,8 +0,0 @@
1
- export * from "./constants";
2
- export * from "./construction";
3
- export * from "./decorators";
4
- export * from "./Model";
5
- export * from "./ModelErrorDefinition";
6
- export * from "./types";
7
- export * from "./utils";
8
- export * from "./validation";
@@ -1,24 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./constants"), exports);
18
- __exportStar(require("./construction"), exports);
19
- __exportStar(require("./decorators"), exports);
20
- __exportStar(require("./Model"), exports);
21
- __exportStar(require("./ModelErrorDefinition"), exports);
22
- __exportStar(require("./types"), exports);
23
- __exportStar(require("./utils"), exports);
24
- __exportStar(require("./validation"), exports);
@@ -1,79 +0,0 @@
1
- import { ModelErrorDefinition } from "./ModelErrorDefinition";
2
- import { Model } from "./Model";
3
- /**
4
- * @summary Typo of a Model builder function
5
- * @memberOf module:decorator-validation.Construction
6
- * @category Construction
7
- */
8
- export type ModelBuilderFunction = <T extends Model>(self: T, obj?: T | Record<string, any>) => T;
9
- /**
10
- * @summary Definition of a Model Constructor Argument
11
- *
12
- * @memberOf module:decorator-validation.Model
13
- * @category Model
14
- *
15
- * @see ModelConstructor
16
- */
17
- export type ModelArg<T> = T | Record<string, any>;
18
- /**
19
- * @summary Definition of a Class Constructor
20
- * @description Generic type for Constructor functions
21
- *
22
- * @typedef Constructor
23
- *
24
- * @param {any[]} [args]
25
- * @memberOf module:decorator-validation.Model
26
- * @category Model
27
- */
28
- export type Constructor<T> = {
29
- new (...args: any[]): T;
30
- };
31
- /**
32
- * @summary Definition of a Model Constructor
33
- * @description Generic type for all Model Constructor functions
34
- *
35
- * @typedef ModelConstructor
36
- *
37
- * @param {ModelArg<T>} [model]
38
- * @param {any[]} [args]
39
- * @memberOf module:decorator-validation.Model
40
- * @category Construction
41
- */
42
- export type ModelConstructor<T extends Model> = {
43
- new (model?: ModelArg<T>, ...args: any[]): T;
44
- };
45
- /**
46
- * @summary Defines the Validation API for validatable models
47
- * @interface Validatable
48
- *
49
- * @category Validation
50
- */
51
- export interface Validatable {
52
- /**
53
- * @summary Validates the model and returns the {@link ModelErrorDefinition} if any
54
- * @param {any} [args]
55
- *
56
- * @method
57
- */
58
- hasErrors(...args: any[]): ModelErrorDefinition | undefined;
59
- }
60
- /**
61
- * @summary Serializable interface
62
- *
63
- * @interface Serializable
64
- *
65
- * @category Serialization
66
- */
67
- export interface Serializable {
68
- /**
69
- * @summary serializes the model
70
- * @method
71
- */
72
- serialize(): string;
73
- }
74
- export interface Hashable {
75
- hash(): string;
76
- }
77
- export interface Comparable<T> {
78
- equals(other: T, ...args: any[]): boolean;
79
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +0,0 @@
1
- import { Model } from "./Model";
2
- export declare function isPropertyModel<M extends Model>(target: M, attribute: string): boolean | string | undefined;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isPropertyModel = isPropertyModel;
4
- const constants_1 = require("../utils/constants");
5
- const Model_1 = require("./Model");
6
- function isPropertyModel(target, attribute) {
7
- if ((0, Model_1.isModel)(target[attribute]))
8
- return true;
9
- const metadata = Reflect.getMetadata(constants_1.ModelKeys.TYPE, target, attribute);
10
- return Model_1.Model.get(metadata.name) ? metadata.name : undefined;
11
- }
@@ -1,14 +0,0 @@
1
- import { ModelErrorDefinition } from "./ModelErrorDefinition";
2
- import { Model } from "./Model";
3
- /**
4
- * @summary Analyses the decorations of the properties and validates the obj according to them
5
- *
6
- * @typedef T extends Model
7
- * @prop {T} obj Model object to validate
8
- * @prop {string[]} [propsToIgnore] object properties to ignore in the validation
9
- *
10
- * @function validate
11
- * @memberOf module:decorator-validation.Validation
12
- * @category Validation
13
- */
14
- export declare function validate<T extends Model>(obj: T, ...propsToIgnore: string[]): ModelErrorDefinition | undefined;
@@ -1,138 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validate = validate;
4
- const ModelErrorDefinition_1 = require("./ModelErrorDefinition");
5
- const reflection_1 = require("@decaf-ts/reflection");
6
- const constants_1 = require("../utils/constants");
7
- const strings_1 = require("../utils/strings");
8
- const constants_2 = require("./constants");
9
- const Model_1 = require("./Model");
10
- const Validation_1 = require("../validation/Validation");
11
- const constants_3 = require("../validation/Validators/constants");
12
- /**
13
- * @summary Analyses the decorations of the properties and validates the obj according to them
14
- *
15
- * @typedef T extends Model
16
- * @prop {T} obj Model object to validate
17
- * @prop {string[]} [propsToIgnore] object properties to ignore in the validation
18
- *
19
- * @function validate
20
- * @memberOf module:decorator-validation.Validation
21
- * @category Validation
22
- */
23
- function validate(obj, ...propsToIgnore) {
24
- const decoratedProperties = [];
25
- for (const prop in obj)
26
- if (Object.prototype.hasOwnProperty.call(obj, prop) &&
27
- propsToIgnore.indexOf(prop) === -1)
28
- decoratedProperties.push(reflection_1.Reflection.getPropertyDecorators(constants_3.ValidationKeys.REFLECT, obj, prop));
29
- let result = undefined;
30
- for (const decoratedProperty of decoratedProperties) {
31
- const { prop, decorators } = decoratedProperty;
32
- if (!decorators || !decorators.length)
33
- continue;
34
- const defaultTypeDecorator = decorators[0];
35
- // tries to find any type decorators or other decorators that already enforce type (the ones with the allowed types property defined). if so, skip the default type verification
36
- if (decorators.find((d) => {
37
- if (d.key === constants_3.ValidationKeys.TYPE)
38
- return true;
39
- return !!d.props.types?.find((t) => t === defaultTypeDecorator.props.name);
40
- })) {
41
- decorators.shift(); // remove the design:type decorator, since the type will already be checked
42
- }
43
- let errs = undefined;
44
- for (const decorator of decorators) {
45
- const validator = Validation_1.Validation.get(decorator.key);
46
- if (!validator) {
47
- throw new Error(`Missing validator for ${decorator.key}`);
48
- }
49
- const decoratorProps = decorator.key === constants_1.ModelKeys.TYPE
50
- ? [decorator.props]
51
- : decorator.props || {};
52
- const err = validator.hasErrors(obj[prop.toString()], decoratorProps);
53
- if (err) {
54
- errs = errs || {};
55
- errs[decorator.key] = err;
56
- }
57
- }
58
- if (errs) {
59
- result = result || {};
60
- result[decoratedProperty.prop.toString()] = errs;
61
- }
62
- }
63
- // tests nested classes
64
- for (const prop of Object.keys(obj).filter((k) => !result || !result[k])) {
65
- let err;
66
- // if a nested Model
67
- const allDecorators = reflection_1.Reflection.getPropertyDecorators(constants_3.ValidationKeys.REFLECT, obj, prop).decorators;
68
- const decorators = reflection_1.Reflection.getPropertyDecorators(constants_3.ValidationKeys.REFLECT, obj, prop).decorators.filter((d) => [constants_1.ModelKeys.TYPE, constants_3.ValidationKeys.TYPE].indexOf(d.key) !== -1);
69
- if (!decorators || !decorators.length)
70
- continue;
71
- const dec = decorators.pop();
72
- const clazz = dec.props.name
73
- ? [dec.props.name]
74
- : Array.isArray(dec.props.customTypes)
75
- ? dec.props.customTypes
76
- : [dec.props.customTypes];
77
- const reserved = Object.values(constants_2.ReservedModels).map((v) => v.toLowerCase());
78
- for (const c of clazz) {
79
- if (reserved.indexOf(c.toLowerCase()) === -1) {
80
- const typeDecoratorKey = Array.isArray(obj[prop])
81
- ? constants_3.ValidationKeys.LIST
82
- : constants_3.ValidationKeys.TYPE;
83
- const types = allDecorators.find((d) => d.key === typeDecoratorKey) || {};
84
- let allowedTypes = [];
85
- if (types && types.props) {
86
- const customTypes = Array.isArray(obj[prop])
87
- ? types.props.class
88
- : types.props.customTypes;
89
- if (customTypes)
90
- allowedTypes = Array.isArray(customTypes)
91
- ? customTypes.map((t) => `${t}`.toLowerCase())
92
- : [customTypes.toLowerCase()];
93
- }
94
- const validate = (prop, value) => {
95
- if (typeof value === "object" || typeof value === "function")
96
- return (0, Model_1.isModel)(value)
97
- ? value.hasErrors()
98
- : allowedTypes.includes(typeof value)
99
- ? undefined
100
- : "Value has no validatable type";
101
- };
102
- switch (c) {
103
- case Array.name:
104
- case Set.name:
105
- if (allDecorators.length) {
106
- const listDec = allDecorators.find((d) => d.key === constants_3.ValidationKeys.LIST);
107
- if (listDec) {
108
- err = (c === Array.name
109
- ? obj[prop]
110
- : // If it's a Set
111
- obj[prop].values())
112
- .map((v) => validate(prop, v))
113
- .filter((e) => !!e);
114
- if (!err?.length) {
115
- // if the result is an empty list...
116
- err = undefined;
117
- }
118
- }
119
- }
120
- break;
121
- default:
122
- try {
123
- if (obj[prop])
124
- err = validate(prop, obj[prop]);
125
- }
126
- catch (e) {
127
- console.warn((0, strings_1.sf)("Model should be validatable but its not: " + e));
128
- }
129
- }
130
- }
131
- if (err) {
132
- result = result || {};
133
- result[prop] = err;
134
- }
135
- }
136
- }
137
- return result ? new ModelErrorDefinition_1.ModelErrorDefinition(result) : undefined;
138
- }
@@ -1,26 +0,0 @@
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 = "decaf.model.",
17
- TYPE = "design:type",
18
- PARAMS = "design:paramtypes",
19
- RETURN = "design:returntype",
20
- MODEL = "model",
21
- ANCHOR = "__model",
22
- CONSTRUCTION = "constructed-by",
23
- ATTRIBUTE = "__attributes",
24
- HASHING = "hashing",
25
- SERIALIZATION = "serialization"
26
- }
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModelKeys = void 0;
4
- /**
5
- * @summary Defines the various Model keys used for reflection
6
- *
7
- * @property {string} REFLECT prefix to all other keys
8
- * @property {string} TYPE type key
9
- * @property {string} PARAMS method params key
10
- * @property {string} RETURN method return key
11
- * @property {string} MODEL model key
12
- * @property {string} ANCHOR anchor key. will serve as a ghost property in the model
13
- *
14
- * @constant ModelKeys
15
- * @memberOf module:decorator-validation.Model
16
- * @category Model
17
- */
18
- var ModelKeys;
19
- (function (ModelKeys) {
20
- ModelKeys["REFLECT"] = "decaf.model.";
21
- ModelKeys["TYPE"] = "design:type";
22
- ModelKeys["PARAMS"] = "design:paramtypes";
23
- ModelKeys["RETURN"] = "design:returntype";
24
- ModelKeys["MODEL"] = "model";
25
- ModelKeys["ANCHOR"] = "__model";
26
- ModelKeys["CONSTRUCTION"] = "constructed-by";
27
- ModelKeys["ATTRIBUTE"] = "__attributes";
28
- ModelKeys["HASHING"] = "hashing";
29
- ModelKeys["SERIALIZATION"] = "serialization";
30
- })(ModelKeys || (exports.ModelKeys = ModelKeys = {}));
@@ -1,76 +0,0 @@
1
- import "reflect-metadata";
2
- /**
3
- * @summary Reverses the process from {@link formatDate}
4
- *
5
- * @param {string} date the date string to be converted back into date
6
- * @param {string} format the date format
7
- * @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?)
8
- *
9
- * @function dateFromFormat
10
- * @memberOf module:decorator-validation.Utils.Dates
11
- * @category Format
12
- */
13
- export declare function dateFromFormat(date: string, format: string): Date;
14
- /**
15
- * @summary Binds a date format to a string
16
- * @param {Date} [date]
17
- * @param {string} [format]
18
- * @memberOf module:decorator-validation.Utils.Format
19
- * @category Utilities
20
- */
21
- export declare function bindDateToString(date: Date | undefined, format: string): Date | undefined;
22
- /**
23
- * @summary Helper function to be used instead of instanceOf Date
24
- * @param date
25
- * @memberOf module:decorator-validation.Utils.Dates
26
- * @category Validation
27
- */
28
- export declare function isValidDate(date: any): boolean;
29
- /**
30
- * @summary Util function to pad numbers
31
- * @param {number} num
32
- *
33
- * @return {string}
34
- *
35
- * @function twoDigitPad
36
- * @memberOf module:decorator-validation.Utils.Format
37
- * @category Format
38
- */
39
- export declare function twoDigitPad(num: number): string;
40
- /**
41
- * @summary Date Format Handling
42
- * @description Code from {@link https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date}
43
- *
44
- * <pre>
45
- * Using similar formatting as Moment.js, Class DateTimeFormatter (Java), and Class SimpleDateFormat (Java),
46
- * I implemented a comprehensive solution formatDate(date, patternStr) where the code is easy to read and modify.
47
- * You can display date, time, AM/PM, etc.
48
- *
49
- * Date and Time Patterns
50
- * yy = 2-digit year; yyyy = full year
51
- * M = digit month; MM = 2-digit month; MMM = short month name; MMMM = full month name
52
- * EEEE = full weekday name; EEE = short weekday name
53
- * d = digit day; dd = 2-digit day
54
- * h = hours am/pm; hh = 2-digit hours am/pm; H = hours; HH = 2-digit hours
55
- * m = minutes; mm = 2-digit minutes; aaa = AM/PM
56
- * s = seconds; ss = 2-digit seconds
57
- * S = miliseconds
58
- * </pre>
59
- *
60
- * @param {Date} date
61
- * @param {string} [patternStr] defaults to 'yyyy/MM/dd'
62
- * @return {string} the formatted date
63
- *
64
- * @function formatDate
65
- * @memberOf module:decorator-validation.Utils.Dates
66
- * @category Format
67
- */
68
- export declare function formatDate(date: Date, patternStr?: string): string;
69
- /**
70
- * @summary Parses a date from a specified format
71
- * @param {string} format
72
- * @param {string | Date | number} [v]
73
- * @memberOf module:decorator-validation.Utils.Dates
74
- * @category Format
75
- */
76
- export declare function parseDate(format: string, v?: string | Date | number): Date | undefined;