@decaf-ts/decorator-validation 1.5.1 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/package.json +15 -9
  2. package/dist/decorator-validation.bundle.min.js +0 -2
  3. package/dist/decorator-validation.bundle.min.js.LICENSE.txt +0 -14
  4. package/dist/esm/decorator-validation.bundle.min.esm.js +0 -2
  5. package/dist/esm/decorator-validation.bundle.min.esm.js.LICENSE.txt +0 -14
  6. package/lib/esm/index.d.ts +0 -42
  7. package/lib/esm/index.js +0 -42
  8. package/lib/esm/model/Model.d.ts +0 -138
  9. package/lib/esm/model/Model.js +0 -296
  10. package/lib/esm/model/ModelErrorDefinition.d.ts +0 -22
  11. package/lib/esm/model/ModelErrorDefinition.js +0 -52
  12. package/lib/esm/model/Registry.d.ts +0 -59
  13. package/lib/esm/model/Registry.js +0 -73
  14. package/lib/esm/model/constants.d.ts +0 -56
  15. package/lib/esm/model/constants.js +0 -69
  16. package/lib/esm/model/construction.d.ts +0 -29
  17. package/lib/esm/model/construction.js +0 -63
  18. package/lib/esm/model/decorators.d.ts +0 -25
  19. package/lib/esm/model/decorators.js +0 -62
  20. package/lib/esm/model/index.d.ts +0 -9
  21. package/lib/esm/model/index.js +0 -9
  22. package/lib/esm/model/types.d.ts +0 -79
  23. package/lib/esm/model/types.js +0 -1
  24. package/lib/esm/model/utils.d.ts +0 -11
  25. package/lib/esm/model/utils.js +0 -25
  26. package/lib/esm/model/validation.d.ts +0 -14
  27. package/lib/esm/model/validation.js +0 -135
  28. package/lib/esm/utils/constants.d.ts +0 -26
  29. package/lib/esm/utils/constants.js +0 -27
  30. package/lib/esm/utils/dates.d.ts +0 -76
  31. package/lib/esm/utils/dates.js +0 -243
  32. package/lib/esm/utils/decorators.d.ts +0 -2
  33. package/lib/esm/utils/decorators.js +0 -18
  34. package/lib/esm/utils/hashing.d.ts +0 -52
  35. package/lib/esm/utils/hashing.js +0 -99
  36. package/lib/esm/utils/index.d.ts +0 -7
  37. package/lib/esm/utils/index.js +0 -7
  38. package/lib/esm/utils/registry.d.ts +0 -68
  39. package/lib/esm/utils/registry.js +0 -1
  40. package/lib/esm/utils/serialization.d.ts +0 -79
  41. package/lib/esm/utils/serialization.js +0 -88
  42. package/lib/esm/utils/strings.d.ts +0 -25
  43. package/lib/esm/utils/strings.js +0 -31
  44. package/lib/esm/validation/Validation.d.ts +0 -51
  45. package/lib/esm/validation/Validation.js +0 -71
  46. package/lib/esm/validation/Validators/DateValidator.d.ts +0 -30
  47. package/lib/esm/validation/Validators/DateValidator.js +0 -54
  48. package/lib/esm/validation/Validators/EmailValidator.d.ts +0 -27
  49. package/lib/esm/validation/Validators/EmailValidator.js +0 -50
  50. package/lib/esm/validation/Validators/ListValidator.d.ts +0 -30
  51. package/lib/esm/validation/Validators/ListValidator.js +0 -65
  52. package/lib/esm/validation/Validators/MaxLengthValidator.d.ts +0 -31
  53. package/lib/esm/validation/Validators/MaxLengthValidator.js +0 -52
  54. package/lib/esm/validation/Validators/MaxValidator.d.ts +0 -30
  55. package/lib/esm/validation/Validators/MaxValidator.js +0 -57
  56. package/lib/esm/validation/Validators/MinLengthValidator.d.ts +0 -31
  57. package/lib/esm/validation/Validators/MinLengthValidator.js +0 -52
  58. package/lib/esm/validation/Validators/MinValidator.d.ts +0 -30
  59. package/lib/esm/validation/Validators/MinValidator.js +0 -57
  60. package/lib/esm/validation/Validators/PasswordValidator.d.ts +0 -27
  61. package/lib/esm/validation/Validators/PasswordValidator.js +0 -50
  62. package/lib/esm/validation/Validators/PatternValidator.d.ts +0 -39
  63. package/lib/esm/validation/Validators/PatternValidator.js +0 -70
  64. package/lib/esm/validation/Validators/RequiredValidator.d.ts +0 -27
  65. package/lib/esm/validation/Validators/RequiredValidator.js +0 -57
  66. package/lib/esm/validation/Validators/StepValidator.d.ts +0 -31
  67. package/lib/esm/validation/Validators/StepValidator.js +0 -52
  68. package/lib/esm/validation/Validators/TypeValidator.d.ts +0 -29
  69. package/lib/esm/validation/Validators/TypeValidator.js +0 -60
  70. package/lib/esm/validation/Validators/URLValidator.d.ts +0 -32
  71. package/lib/esm/validation/Validators/URLValidator.js +0 -46
  72. package/lib/esm/validation/Validators/Validator.d.ts +0 -43
  73. package/lib/esm/validation/Validators/Validator.js +0 -47
  74. package/lib/esm/validation/Validators/ValidatorRegistry.d.ts +0 -47
  75. package/lib/esm/validation/Validators/ValidatorRegistry.js +0 -80
  76. package/lib/esm/validation/Validators/constants.d.ts +0 -96
  77. package/lib/esm/validation/Validators/constants.js +0 -134
  78. package/lib/esm/validation/Validators/decorators.d.ts +0 -12
  79. package/lib/esm/validation/Validators/decorators.js +0 -25
  80. package/lib/esm/validation/Validators/index.d.ts +0 -52
  81. package/lib/esm/validation/Validators/index.js +0 -52
  82. package/lib/esm/validation/Validators/types.d.ts +0 -51
  83. package/lib/esm/validation/Validators/types.js +0 -1
  84. package/lib/esm/validation/decorators.d.ts +0 -178
  85. package/lib/esm/validation/decorators.js +0 -288
  86. package/lib/esm/validation/index.d.ts +0 -4
  87. package/lib/esm/validation/index.js +0 -4
  88. package/lib/esm/validation/types.d.ts +0 -46
  89. package/lib/esm/validation/types.js +0 -1
  90. package/lib/index.cjs +0 -59
  91. package/lib/index.d.ts +0 -42
  92. package/lib/model/Model.cjs +0 -300
  93. package/lib/model/Model.d.ts +0 -138
  94. package/lib/model/ModelErrorDefinition.cjs +0 -56
  95. package/lib/model/ModelErrorDefinition.d.ts +0 -22
  96. package/lib/model/Registry.cjs +0 -78
  97. package/lib/model/Registry.d.ts +0 -59
  98. package/lib/model/constants.cjs +0 -72
  99. package/lib/model/constants.d.ts +0 -56
  100. package/lib/model/construction.cjs +0 -68
  101. package/lib/model/construction.d.ts +0 -29
  102. package/lib/model/decorators.cjs +0 -67
  103. package/lib/model/decorators.d.ts +0 -25
  104. package/lib/model/index.cjs +0 -25
  105. package/lib/model/index.d.ts +0 -9
  106. package/lib/model/types.cjs +0 -2
  107. package/lib/model/types.d.ts +0 -79
  108. package/lib/model/utils.cjs +0 -29
  109. package/lib/model/utils.d.ts +0 -11
  110. package/lib/model/validation.cjs +0 -138
  111. package/lib/model/validation.d.ts +0 -14
  112. package/lib/utils/constants.cjs +0 -30
  113. package/lib/utils/constants.d.ts +0 -26
  114. package/lib/utils/dates.cjs +0 -251
  115. package/lib/utils/dates.d.ts +0 -76
  116. package/lib/utils/decorators.cjs +0 -22
  117. package/lib/utils/decorators.d.ts +0 -2
  118. package/lib/utils/hashing.cjs +0 -106
  119. package/lib/utils/hashing.d.ts +0 -52
  120. package/lib/utils/index.cjs +0 -23
  121. package/lib/utils/index.d.ts +0 -7
  122. package/lib/utils/registry.cjs +0 -2
  123. package/lib/utils/registry.d.ts +0 -68
  124. package/lib/utils/serialization.cjs +0 -93
  125. package/lib/utils/serialization.d.ts +0 -79
  126. package/lib/utils/strings.cjs +0 -35
  127. package/lib/utils/strings.d.ts +0 -25
  128. package/lib/validation/Validation.cjs +0 -75
  129. package/lib/validation/Validation.d.ts +0 -51
  130. package/lib/validation/Validators/DateValidator.cjs +0 -57
  131. package/lib/validation/Validators/DateValidator.d.ts +0 -30
  132. package/lib/validation/Validators/EmailValidator.cjs +0 -53
  133. package/lib/validation/Validators/EmailValidator.d.ts +0 -27
  134. package/lib/validation/Validators/ListValidator.cjs +0 -68
  135. package/lib/validation/Validators/ListValidator.d.ts +0 -30
  136. package/lib/validation/Validators/MaxLengthValidator.cjs +0 -55
  137. package/lib/validation/Validators/MaxLengthValidator.d.ts +0 -31
  138. package/lib/validation/Validators/MaxValidator.cjs +0 -60
  139. package/lib/validation/Validators/MaxValidator.d.ts +0 -30
  140. package/lib/validation/Validators/MinLengthValidator.cjs +0 -55
  141. package/lib/validation/Validators/MinLengthValidator.d.ts +0 -31
  142. package/lib/validation/Validators/MinValidator.cjs +0 -60
  143. package/lib/validation/Validators/MinValidator.d.ts +0 -30
  144. package/lib/validation/Validators/PasswordValidator.cjs +0 -53
  145. package/lib/validation/Validators/PasswordValidator.d.ts +0 -27
  146. package/lib/validation/Validators/PatternValidator.cjs +0 -73
  147. package/lib/validation/Validators/PatternValidator.d.ts +0 -39
  148. package/lib/validation/Validators/RequiredValidator.cjs +0 -60
  149. package/lib/validation/Validators/RequiredValidator.d.ts +0 -27
  150. package/lib/validation/Validators/StepValidator.cjs +0 -55
  151. package/lib/validation/Validators/StepValidator.d.ts +0 -31
  152. package/lib/validation/Validators/TypeValidator.cjs +0 -63
  153. package/lib/validation/Validators/TypeValidator.d.ts +0 -29
  154. package/lib/validation/Validators/URLValidator.cjs +0 -49
  155. package/lib/validation/Validators/URLValidator.d.ts +0 -32
  156. package/lib/validation/Validators/Validator.cjs +0 -51
  157. package/lib/validation/Validators/Validator.d.ts +0 -43
  158. package/lib/validation/Validators/ValidatorRegistry.cjs +0 -85
  159. package/lib/validation/Validators/ValidatorRegistry.d.ts +0 -47
  160. package/lib/validation/Validators/constants.cjs +0 -137
  161. package/lib/validation/Validators/constants.d.ts +0 -96
  162. package/lib/validation/Validators/decorators.cjs +0 -28
  163. package/lib/validation/Validators/index.cjs +0 -69
  164. package/lib/validation/Validators/types.cjs +0 -2
  165. package/lib/validation/decorators.cjs +0 -304
  166. package/lib/validation/decorators.d.ts +0 -178
  167. package/lib/validation/index.cjs +0 -20
  168. package/lib/validation/index.d.ts +0 -4
  169. package/lib/validation/types.cjs +0 -2
  170. package/lib/validation/types.d.ts +0 -46
@@ -1,63 +0,0 @@
1
- import { Model } from "./Model";
2
- /**
3
- * @summary Helper Function to override constructors
4
- *
5
- * @param {Function} constructor
6
- * @param {any[]} [args]
7
- * @return {T} the new instance
8
- *
9
- * @function construct
10
- * @memberOf module:decorator-validation.Construction
11
- */
12
- export function construct(constructor, ...args) {
13
- const _constr = (...argz) => new constructor(...argz);
14
- _constr.prototype = constructor.prototype;
15
- return _constr(...args);
16
- }
17
- /**
18
- * @summary Recursively finds the last prototype before Object
19
- * @param {object} obj
20
- *
21
- * @function findLastProtoBeforeObject
22
- * @memberOf module:decorator-validation.Construction
23
- */
24
- export function findLastProtoBeforeObject(obj) {
25
- let prototype = Object.getPrototypeOf(obj);
26
- if (prototype === Object.prototype)
27
- return obj;
28
- while (prototype !== Object.prototype) {
29
- prototype = Object.getPrototypeOf(prototype);
30
- if (prototype === Object.prototype)
31
- return prototype;
32
- if (Object.getPrototypeOf(prototype) === Object.prototype)
33
- return prototype;
34
- }
35
- throw new Error("Could not find proper prototype");
36
- }
37
- /**
38
- * @sumary binds the {@link Model} class as a root prototype of the provided instance
39
- *
40
- * @param {unknown} obj
41
- *
42
- * @function bindModelPrototype
43
- * @memberOf module:decorator-validation.Construction
44
- */
45
- export function bindModelPrototype(obj) {
46
- if (obj instanceof Model)
47
- return;
48
- function bindPrototype(objToOverride, prototype) {
49
- Object.setPrototypeOf(objToOverride, prototype);
50
- }
51
- const prototype = Object.getPrototypeOf(obj);
52
- if (prototype === Object.prototype) {
53
- return bindPrototype(obj, Model.prototype);
54
- }
55
- while (prototype !== Object.prototype) {
56
- const prot = Object.getPrototypeOf(prototype);
57
- if (prot === Object.prototype ||
58
- Object.getPrototypeOf(prot) === Object.prototype) {
59
- return bindPrototype(prototype, Model.prototype);
60
- }
61
- }
62
- throw new Error("Could not find proper prototype to bind");
63
- }
@@ -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,62 +0,0 @@
1
- import { bindModelPrototype, construct } from "./construction";
2
- import { ModelKeys } from "../utils/constants";
3
- import { Model } from "./Model";
4
- import { metadata } from "@decaf-ts/reflection";
5
- /**
6
- * @summary Defines a class as a Model class
7
- * @description
8
- *
9
- * - Registers the class under the model registry so it can be easily rebuilt;
10
- * - Overrides the class constructor;
11
- * - Runs the global {@link ModelBuilderFunction} if defined;
12
- * - Runs the optional {@link InstanceCallback} if provided;
13
- *
14
- * @param {InstanceCallback} [instanceCallback] optional callback that will be called with the instance upon instantiation. defaults to undefined
15
- *
16
- * @function model
17
- *
18
- * @memberOf module:decorator-validation.Model
19
- *
20
- */
21
- export function model(instanceCallback) {
22
- return ((original) => {
23
- // the new constructor behaviour
24
- const newConstructor = function (...args) {
25
- const instance = construct(original, ...args);
26
- bindModelPrototype(instance);
27
- // run a builder function if defined with the first argument (The ModelArg)
28
- const builder = Model.getBuilder();
29
- if (builder)
30
- builder(instance, args.length ? args[0] : undefined);
31
- metadata(Model.key(ModelKeys.MODEL), original.name)(instance.constructor);
32
- if (instanceCallback)
33
- instanceCallback(instance, ...args);
34
- return instance;
35
- };
36
- // copy prototype so instanceof operator still works
37
- newConstructor.prototype = original.prototype;
38
- // Sets the proper constructor name for type verification
39
- Object.defineProperty(newConstructor, "name", {
40
- writable: false,
41
- enumerable: true,
42
- configurable: false,
43
- value: original.prototype.constructor.name,
44
- });
45
- metadata(Model.key(ModelKeys.MODEL), original.name)(original);
46
- Model.register(newConstructor, original.name);
47
- // return new constructor (will override original)
48
- return newConstructor;
49
- });
50
- }
51
- export function hashedBy(algorithm, ...args) {
52
- return metadata(Model.key(ModelKeys.HASHING), {
53
- algorithm: algorithm,
54
- args: args,
55
- });
56
- }
57
- export function serializedBy(serializer, ...args) {
58
- return metadata(Model.key(ModelKeys.SERIALIZATION), {
59
- serializer: serializer,
60
- args: args,
61
- });
62
- }
@@ -1,9 +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 "./Registry";
7
- export * from "./types";
8
- export * from "./utils";
9
- export * from "./validation";
@@ -1,9 +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 "./Registry";
7
- export * from "./types";
8
- export * from "./utils";
9
- export * from "./validation";
@@ -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 +0,0 @@
1
- export {};
@@ -1,11 +0,0 @@
1
- import { Model } from "./Model";
2
- export declare function isPropertyModel<M extends Model>(target: M, attribute: string): boolean | string | undefined;
3
- /**
4
- * @summary For Serialization/deserialization purposes.
5
- * @description Reads the {@link ModelKeys.ANCHOR} property of a {@link Model} to discover the class to instantiate
6
- *
7
- * @function isModel
8
- * @memberOf module:decorator-validation.Validation
9
- * @category Validation
10
- */
11
- export declare function isModel(target: Record<string, any>): boolean;
@@ -1,25 +0,0 @@
1
- import { ModelKeys } from "../utils/constants";
2
- import { Model } from "./Model";
3
- export function isPropertyModel(target, attribute) {
4
- if (isModel(target[attribute]))
5
- return true;
6
- const metadata = Reflect.getMetadata(ModelKeys.TYPE, target, attribute);
7
- return Model.get(metadata.name) ? metadata.name : undefined;
8
- }
9
- /**
10
- * @summary For Serialization/deserialization purposes.
11
- * @description Reads the {@link ModelKeys.ANCHOR} property of a {@link Model} to discover the class to instantiate
12
- *
13
- * @function isModel
14
- * @memberOf module:decorator-validation.Validation
15
- * @category Validation
16
- */
17
- export function isModel(target) {
18
- try {
19
- return target instanceof Model || !!Model.getMetadata(target);
20
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
- }
22
- catch (e) {
23
- return false;
24
- }
25
- }
@@ -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,135 +0,0 @@
1
- import { ModelErrorDefinition } from "./ModelErrorDefinition";
2
- import { Reflection } from "@decaf-ts/reflection";
3
- import { ModelKeys } from "../utils/constants";
4
- import { sf } from "../utils/strings";
5
- import { ReservedModels } from "./constants";
6
- import { Validation } from "../validation/Validation";
7
- import { ValidationKeys } from "../validation/Validators/constants";
8
- import { isModel } from "./utils";
9
- /**
10
- * @summary Analyses the decorations of the properties and validates the obj according to them
11
- *
12
- * @typedef T extends Model
13
- * @prop {T} obj Model object to validate
14
- * @prop {string[]} [propsToIgnore] object properties to ignore in the validation
15
- *
16
- * @function validate
17
- * @memberOf module:decorator-validation.Validation
18
- * @category Validation
19
- */
20
- export function validate(obj, ...propsToIgnore) {
21
- const decoratedProperties = [];
22
- for (const prop in obj)
23
- if (Object.prototype.hasOwnProperty.call(obj, prop) &&
24
- propsToIgnore.indexOf(prop) === -1)
25
- decoratedProperties.push(Reflection.getPropertyDecorators(ValidationKeys.REFLECT, obj, prop));
26
- let result = undefined;
27
- for (const decoratedProperty of decoratedProperties) {
28
- const { prop, decorators } = decoratedProperty;
29
- if (!decorators || !decorators.length)
30
- continue;
31
- const defaultTypeDecorator = decorators[0];
32
- // 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
33
- if (decorators.find((d) => {
34
- if (d.key === ValidationKeys.TYPE)
35
- return true;
36
- return !!d.props.types?.find((t) => t === defaultTypeDecorator.props.name);
37
- })) {
38
- decorators.shift(); // remove the design:type decorator, since the type will already be checked
39
- }
40
- let errs = undefined;
41
- for (const decorator of decorators) {
42
- const validator = Validation.get(decorator.key);
43
- if (!validator) {
44
- throw new Error(`Missing validator for ${decorator.key}`);
45
- }
46
- const decoratorProps = decorator.key === ModelKeys.TYPE
47
- ? [decorator.props]
48
- : (decorator.props || {});
49
- const err = validator.hasErrors(obj[prop.toString()], decoratorProps);
50
- if (err) {
51
- errs = errs || {};
52
- errs[decorator.key] = err;
53
- }
54
- }
55
- if (errs) {
56
- result = result || {};
57
- result[decoratedProperty.prop.toString()] = errs;
58
- }
59
- }
60
- // tests nested classes
61
- for (const prop of Object.keys(obj).filter((k) => !result || !result[k])) {
62
- let err;
63
- // if a nested Model
64
- const allDecorators = Reflection.getPropertyDecorators(ValidationKeys.REFLECT, obj, prop).decorators;
65
- const decorators = Reflection.getPropertyDecorators(ValidationKeys.REFLECT, obj, prop).decorators.filter((d) => [ModelKeys.TYPE, ValidationKeys.TYPE].indexOf(d.key) !== -1);
66
- if (!decorators || !decorators.length)
67
- continue;
68
- const dec = decorators.pop();
69
- const clazz = dec.props.name
70
- ? [dec.props.name]
71
- : Array.isArray(dec.props.customTypes)
72
- ? dec.props.customTypes
73
- : [dec.props.customTypes];
74
- const reserved = Object.values(ReservedModels).map((v) => v.toLowerCase());
75
- for (const c of clazz) {
76
- if (reserved.indexOf(c.toLowerCase()) === -1) {
77
- const typeDecoratorKey = Array.isArray(obj[prop])
78
- ? ValidationKeys.LIST
79
- : ValidationKeys.TYPE;
80
- const types = allDecorators.find((d) => d.key === typeDecoratorKey) || {};
81
- let allowedTypes = [];
82
- if (types && types.props) {
83
- const customTypes = Array.isArray(obj[prop])
84
- ? types.props.class
85
- : types.props.customTypes;
86
- if (customTypes)
87
- allowedTypes = Array.isArray(customTypes)
88
- ? customTypes.map((t) => `${t}`.toLowerCase())
89
- : [customTypes.toLowerCase()];
90
- }
91
- const validate = (prop, value) => {
92
- if (typeof value === "object" || typeof value === "function")
93
- return isModel(value)
94
- ? value.hasErrors()
95
- : allowedTypes.includes(typeof value)
96
- ? undefined
97
- : "Value has no validatable type";
98
- };
99
- switch (c) {
100
- case Array.name:
101
- case Set.name:
102
- if (allDecorators.length) {
103
- const listDec = allDecorators.find((d) => d.key === ValidationKeys.LIST);
104
- if (listDec) {
105
- err = (c === Array.name
106
- ? obj[prop]
107
- : // If it's a Set
108
- obj[prop].values())
109
- .map((v) => validate(prop, v))
110
- .filter((e) => !!e);
111
- if (!err?.length) {
112
- // if the result is an empty list...
113
- err = undefined;
114
- }
115
- }
116
- }
117
- break;
118
- default:
119
- try {
120
- if (obj[prop])
121
- err = validate(prop, obj[prop]);
122
- }
123
- catch (e) {
124
- console.warn(sf("Model should be validatable but its not: " + e));
125
- }
126
- }
127
- }
128
- if (err) {
129
- result = result || {};
130
- result[prop] = err;
131
- }
132
- }
133
- }
134
- return result ? new ModelErrorDefinition(result) : undefined;
135
- }
@@ -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,27 +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 var ModelKeys;
16
- (function (ModelKeys) {
17
- ModelKeys["REFLECT"] = "decaf.model.";
18
- ModelKeys["TYPE"] = "design:type";
19
- ModelKeys["PARAMS"] = "design:paramtypes";
20
- ModelKeys["RETURN"] = "design:returntype";
21
- ModelKeys["MODEL"] = "model";
22
- ModelKeys["ANCHOR"] = "__model";
23
- ModelKeys["CONSTRUCTION"] = "constructed-by";
24
- ModelKeys["ATTRIBUTE"] = "__attributes";
25
- ModelKeys["HASHING"] = "hashing";
26
- ModelKeys["SERIALIZATION"] = "serialization";
27
- })(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;