@decaf-ts/decorator-validation 1.5.5 → 1.5.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 (130) hide show
  1. package/dist/decorator-validation.js +1 -1
  2. package/dist/esm/decorator-validation.js +1 -1
  3. package/lib/esm/index.d.ts +1 -1
  4. package/lib/esm/index.js +1 -3
  5. package/lib/esm/model/Model.js +0 -2
  6. package/lib/esm/model/ModelErrorDefinition.js +0 -2
  7. package/lib/esm/model/Registry.js +0 -2
  8. package/lib/esm/model/constants.js +0 -2
  9. package/lib/esm/model/construction.js +0 -2
  10. package/lib/esm/model/decorators.js +0 -2
  11. package/lib/esm/model/index.js +0 -2
  12. package/lib/esm/model/types.js +0 -2
  13. package/lib/esm/model/utils.js +0 -2
  14. package/lib/esm/model/validation.js +0 -2
  15. package/lib/esm/utils/constants.js +0 -2
  16. package/lib/esm/utils/dates.js +0 -2
  17. package/lib/esm/utils/decorators.js +0 -2
  18. package/lib/esm/utils/hashing.js +0 -2
  19. package/lib/esm/utils/index.js +0 -2
  20. package/lib/esm/utils/registry.js +0 -2
  21. package/lib/esm/utils/serialization.js +0 -2
  22. package/lib/esm/utils/strings.js +0 -2
  23. package/lib/esm/validation/Validation.js +0 -2
  24. package/lib/esm/validation/Validators/DateValidator.js +0 -2
  25. package/lib/esm/validation/Validators/EmailValidator.js +0 -2
  26. package/lib/esm/validation/Validators/ListValidator.js +0 -2
  27. package/lib/esm/validation/Validators/MaxLengthValidator.js +0 -2
  28. package/lib/esm/validation/Validators/MaxValidator.js +0 -2
  29. package/lib/esm/validation/Validators/MinLengthValidator.js +0 -2
  30. package/lib/esm/validation/Validators/MinValidator.js +0 -2
  31. package/lib/esm/validation/Validators/PasswordValidator.js +0 -2
  32. package/lib/esm/validation/Validators/PatternValidator.js +0 -2
  33. package/lib/esm/validation/Validators/RequiredValidator.js +0 -2
  34. package/lib/esm/validation/Validators/StepValidator.js +0 -2
  35. package/lib/esm/validation/Validators/TypeValidator.js +0 -2
  36. package/lib/esm/validation/Validators/URLValidator.js +0 -2
  37. package/lib/esm/validation/Validators/Validator.js +0 -2
  38. package/lib/esm/validation/Validators/ValidatorRegistry.js +0 -2
  39. package/lib/esm/validation/Validators/constants.js +0 -2
  40. package/lib/esm/validation/Validators/decorators.js +0 -2
  41. package/lib/esm/validation/Validators/index.js +0 -2
  42. package/lib/esm/validation/Validators/types.js +0 -2
  43. package/lib/esm/validation/decorators.js +0 -2
  44. package/lib/esm/validation/index.js +0 -2
  45. package/lib/esm/validation/types.js +0 -2
  46. package/lib/index.d.ts +1 -1
  47. package/lib/index.js +59 -0
  48. package/lib/model/Model.js +300 -0
  49. package/lib/model/ModelErrorDefinition.js +56 -0
  50. package/lib/model/Registry.js +78 -0
  51. package/lib/model/constants.js +72 -0
  52. package/lib/model/construction.js +68 -0
  53. package/lib/model/decorators.js +67 -0
  54. package/lib/{validation/index.cjs → model/index.js} +9 -6
  55. package/lib/model/types.js +2 -0
  56. package/lib/model/utils.js +29 -0
  57. package/lib/model/validation.js +138 -0
  58. package/lib/utils/constants.js +30 -0
  59. package/lib/utils/dates.js +251 -0
  60. package/lib/utils/decorators.js +22 -0
  61. package/lib/utils/hashing.js +106 -0
  62. package/lib/utils/index.js +23 -0
  63. package/lib/utils/registry.js +2 -0
  64. package/lib/utils/serialization.js +93 -0
  65. package/lib/utils/strings.js +35 -0
  66. package/lib/validation/Validation.js +75 -0
  67. package/lib/validation/Validators/DateValidator.js +57 -0
  68. package/lib/validation/Validators/EmailValidator.js +53 -0
  69. package/lib/validation/Validators/ListValidator.js +70 -0
  70. package/lib/validation/Validators/MaxLengthValidator.js +55 -0
  71. package/lib/validation/Validators/MaxValidator.js +60 -0
  72. package/lib/validation/Validators/MinLengthValidator.js +55 -0
  73. package/lib/validation/Validators/MinValidator.js +60 -0
  74. package/lib/validation/Validators/PasswordValidator.js +53 -0
  75. package/lib/validation/Validators/PatternValidator.js +73 -0
  76. package/lib/validation/Validators/RequiredValidator.js +60 -0
  77. package/lib/validation/Validators/StepValidator.js +55 -0
  78. package/lib/validation/Validators/TypeValidator.js +63 -0
  79. package/lib/validation/Validators/URLValidator.js +52 -0
  80. package/lib/validation/Validators/Validator.js +51 -0
  81. package/lib/validation/Validators/ValidatorRegistry.js +85 -0
  82. package/lib/validation/Validators/constants.js +137 -0
  83. package/lib/validation/Validators/decorators.js +28 -0
  84. package/lib/validation/Validators/index.js +69 -0
  85. package/lib/validation/Validators/types.js +2 -0
  86. package/lib/validation/decorators.js +304 -0
  87. package/lib/validation/index.js +20 -0
  88. package/lib/validation/types.js +2 -0
  89. package/package.json +1 -1
  90. package/lib/index.cjs +0 -61
  91. package/lib/model/Model.cjs +0 -302
  92. package/lib/model/ModelErrorDefinition.cjs +0 -58
  93. package/lib/model/Registry.cjs +0 -80
  94. package/lib/model/constants.cjs +0 -74
  95. package/lib/model/construction.cjs +0 -70
  96. package/lib/model/decorators.cjs +0 -69
  97. package/lib/model/index.cjs +0 -27
  98. package/lib/model/types.cjs +0 -4
  99. package/lib/model/utils.cjs +0 -31
  100. package/lib/model/validation.cjs +0 -140
  101. package/lib/utils/constants.cjs +0 -32
  102. package/lib/utils/dates.cjs +0 -253
  103. package/lib/utils/decorators.cjs +0 -24
  104. package/lib/utils/hashing.cjs +0 -108
  105. package/lib/utils/index.cjs +0 -25
  106. package/lib/utils/registry.cjs +0 -4
  107. package/lib/utils/serialization.cjs +0 -95
  108. package/lib/utils/strings.cjs +0 -37
  109. package/lib/validation/Validation.cjs +0 -77
  110. package/lib/validation/Validators/DateValidator.cjs +0 -59
  111. package/lib/validation/Validators/EmailValidator.cjs +0 -55
  112. package/lib/validation/Validators/ListValidator.cjs +0 -72
  113. package/lib/validation/Validators/MaxLengthValidator.cjs +0 -57
  114. package/lib/validation/Validators/MaxValidator.cjs +0 -62
  115. package/lib/validation/Validators/MinLengthValidator.cjs +0 -57
  116. package/lib/validation/Validators/MinValidator.cjs +0 -62
  117. package/lib/validation/Validators/PasswordValidator.cjs +0 -55
  118. package/lib/validation/Validators/PatternValidator.cjs +0 -75
  119. package/lib/validation/Validators/RequiredValidator.cjs +0 -62
  120. package/lib/validation/Validators/StepValidator.cjs +0 -57
  121. package/lib/validation/Validators/TypeValidator.cjs +0 -65
  122. package/lib/validation/Validators/URLValidator.cjs +0 -54
  123. package/lib/validation/Validators/Validator.cjs +0 -53
  124. package/lib/validation/Validators/ValidatorRegistry.cjs +0 -87
  125. package/lib/validation/Validators/constants.cjs +0 -139
  126. package/lib/validation/Validators/decorators.cjs +0 -30
  127. package/lib/validation/Validators/index.cjs +0 -71
  128. package/lib/validation/Validators/types.d.ts +0 -88
  129. package/lib/validation/decorators.cjs +0 -306
  130. package/lib/validation/types.cjs +0 -4
@@ -0,0 +1,23 @@
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("./dates"), exports);
19
+ __exportStar(require("./decorators"), exports);
20
+ __exportStar(require("./hashing"), exports);
21
+ __exportStar(require("./registry"), exports);
22
+ __exportStar(require("./serialization"), exports);
23
+ __exportStar(require("./strings"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Serialization = exports.DefaultSerializationMethod = exports.JSONSerializer = void 0;
4
+ const Model_1 = require("../model/Model");
5
+ const constants_1 = require("./constants");
6
+ /**
7
+ * @summary Concrete implementation of a {@link Serializer} in JSON format
8
+ * @description JS's native JSON.stringify (used here) is not deterministic
9
+ * and therefore should not be used for hashing purposes
10
+ *
11
+ * To keep dependencies low, we will not implement this, but we recommend
12
+ * implementing a similar {@link JSONSerializer} using 'deterministic-json' libraries
13
+ *
14
+ * @class JSONSerializer
15
+ * @implements Serializer
16
+ *
17
+ * @category Serialization
18
+ */
19
+ class JSONSerializer {
20
+ /**
21
+ * @summary prepares the model for serialization
22
+ * @description returns a shallow copy of the object, containing an enumerable {@link ModelKeys#ANCHOR} property
23
+ * so the object can be recognized upon deserialization
24
+ *
25
+ * @param {T} model
26
+ * @protected
27
+ */
28
+ preSerialize(model) {
29
+ // TODO: nested preserialization (so increase performance when deserializing)
30
+ const toSerialize = Object.assign({}, model);
31
+ const metadata = Model_1.Model.getMetadata(model);
32
+ toSerialize[constants_1.ModelKeys.ANCHOR] = metadata || model.constructor.name;
33
+ return toSerialize;
34
+ }
35
+ /**
36
+ * @summary Rebuilds a model from a serialization
37
+ * @param {string} str
38
+ *
39
+ * @throws {Error} If it fails to parse the string, or to build the model
40
+ */
41
+ deserialize(str) {
42
+ const deserialization = JSON.parse(str);
43
+ const className = deserialization[constants_1.ModelKeys.ANCHOR];
44
+ if (!className)
45
+ throw new Error("Could not find class reference in serialized model");
46
+ const model = Model_1.Model.build(deserialization, className);
47
+ return model;
48
+ }
49
+ /**
50
+ * @summary Serializes a model
51
+ * @param {T} model
52
+ *
53
+ * @throws {Error} if fails to serialize
54
+ */
55
+ serialize(model) {
56
+ return JSON.stringify(this.preSerialize(model));
57
+ }
58
+ }
59
+ exports.JSONSerializer = JSONSerializer;
60
+ exports.DefaultSerializationMethod = "json";
61
+ class Serialization {
62
+ static { this.current = exports.DefaultSerializationMethod; }
63
+ static { this.cache = {
64
+ json: new JSONSerializer(),
65
+ }; }
66
+ constructor() { }
67
+ static get(key) {
68
+ if (key in this.cache)
69
+ return this.cache[key];
70
+ throw new Error(`No serialization method registered under ${key}`);
71
+ }
72
+ static register(key, func, setDefault = false) {
73
+ if (key in this.cache)
74
+ throw new Error(`Serialization method ${key} already registered`);
75
+ this.cache[key] = new func();
76
+ if (setDefault)
77
+ this.current = key;
78
+ }
79
+ static serialize(obj, method, ...args) {
80
+ if (!method)
81
+ return this.get(this.current).serialize(obj, ...args);
82
+ return this.get(method).serialize(obj, ...args);
83
+ }
84
+ static deserialize(obj, method, ...args) {
85
+ if (!method)
86
+ return this.get(this.current).deserialize(obj, ...args);
87
+ return this.get(method).deserialize(obj, ...args);
88
+ }
89
+ static setDefault(method) {
90
+ this.current = this.get(method);
91
+ }
92
+ }
93
+ exports.Serialization = Serialization;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sf = void 0;
4
+ exports.stringFormat = stringFormat;
5
+ /**
6
+ * @summary Util function to provide string format functionality similar to C#'s string.format
7
+ *
8
+ * @param {string} string
9
+ * @param {Array<string | number>} [args] replacements made by order of appearance (replacement0 wil replace {0} and so on)
10
+ * @return {string} formatted string
11
+ *
12
+ * @function stringFormat
13
+ * @memberOf module:decorator-validation.Utils.Format
14
+ * @category Format
15
+ */
16
+ function stringFormat(string, ...args) {
17
+ return string.replace(/{(\d+)}/g, function (match, number) {
18
+ return typeof args[number] !== "undefined"
19
+ ? args[number].toString()
20
+ : "undefined";
21
+ });
22
+ }
23
+ /**
24
+ * @summary Util function to provide string format functionality similar to C#'s string.format
25
+ * @description alias for {@link stringFormat}
26
+ *
27
+ * @param {string} string
28
+ * @param {string} args replacements made by order of appearance (replacement0 wil replace {0} and so on)
29
+ * @return {string} formatted string
30
+ *
31
+ * @function sf
32
+ * @memberOf module:decorator-validation.Utils.Format
33
+ * @category Format
34
+ */
35
+ exports.sf = stringFormat;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Validation = void 0;
4
+ const ValidatorRegistry_1 = require("./Validators/ValidatorRegistry");
5
+ const constants_1 = require("./Validators/constants");
6
+ /**
7
+ * @summary Static class acting as a namespace for the Validation
8
+ *
9
+ * @class Validation
10
+ * @static
11
+ *
12
+ * @category Validation
13
+ */
14
+ class Validation {
15
+ static { this.actingValidatorRegistry = undefined; }
16
+ constructor() { }
17
+ /**
18
+ * @summary Defines the acting ValidatorRegistry
19
+ *
20
+ * @param {IValidatorRegistry} validatorRegistry the new implementation of the validator Registry
21
+ * @param {function(Validator): Validator} [migrationHandler] the method to map the validator if required;
22
+ */
23
+ static setRegistry(validatorRegistry, migrationHandler) {
24
+ if (migrationHandler && Validation.actingValidatorRegistry)
25
+ Validation.actingValidatorRegistry.getKeys().forEach((k) => {
26
+ const validator = validatorRegistry.get(k);
27
+ if (validator)
28
+ validatorRegistry.register(migrationHandler(validator));
29
+ });
30
+ Validation.actingValidatorRegistry = validatorRegistry;
31
+ }
32
+ /**
33
+ * @summary Returns the current ValidatorRegistry
34
+ *
35
+ * @return IValidatorRegistry, defaults to {@link ValidatorRegistry}
36
+ */
37
+ static getRegistry() {
38
+ if (!Validation.actingValidatorRegistry)
39
+ Validation.actingValidatorRegistry = new ValidatorRegistry_1.ValidatorRegistry();
40
+ return Validation.actingValidatorRegistry;
41
+ }
42
+ /**
43
+ * @summary Retrieves a validator
44
+ *
45
+ * @param {string} validatorKey one of the {@link ValidationKeys}
46
+ * @return {Validator | undefined} the registered Validator or undefined if there is nono matching the provided key
47
+ */
48
+ static get(validatorKey) {
49
+ return Validation.getRegistry().get(validatorKey);
50
+ }
51
+ /**
52
+ * @summary Registers the provided validators onto the registry
53
+ *
54
+ * @param {T[] | ValidatorDefinition[]} validator
55
+ */
56
+ static register(...validator) {
57
+ return Validation.getRegistry().register(...validator);
58
+ }
59
+ /**
60
+ * @summary Builds the key to store as Metadata under Reflections
61
+ * @description concatenates {@link ValidationKeys#REFLECT} with the provided key
62
+ *
63
+ * @param {string} key
64
+ */
65
+ static key(key) {
66
+ return constants_1.ValidationKeys.REFLECT + key;
67
+ }
68
+ /**
69
+ * @summary Returns all registered validation keys
70
+ */
71
+ static keys() {
72
+ return this.getRegistry().getKeys();
73
+ }
74
+ }
75
+ exports.Validation = Validation;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.DateValidator = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary Date Validator
18
+ *
19
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#DATE}
20
+ *
21
+ * @class DateValidator
22
+ * @extends Validator
23
+ *
24
+ * @category Validators
25
+ */
26
+ let DateValidator = class DateValidator extends Validator_1.Validator {
27
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.DATE) {
28
+ super(message, Number.name, Date.name, String.name);
29
+ }
30
+ /**
31
+ * @summary Validates a model
32
+ *
33
+ * @param {Date | string} value
34
+ * @param {DateValidatorOptions} [options]
35
+ *
36
+ * @return {string | undefined}
37
+ *
38
+ * @override
39
+ *
40
+ * @see Validator#hasErrors
41
+ */
42
+ hasErrors(value, options = {}) {
43
+ if (value === undefined)
44
+ return;
45
+ if (typeof value === "string")
46
+ value = new Date(value);
47
+ if (Number.isNaN(value.getDate())) {
48
+ const { message = "" } = options;
49
+ return this.getMessage(message || this.message);
50
+ }
51
+ }
52
+ };
53
+ exports.DateValidator = DateValidator;
54
+ exports.DateValidator = DateValidator = __decorate([
55
+ (0, decorators_1.validator)(constants_1.ValidationKeys.DATE),
56
+ __metadata("design:paramtypes", [String])
57
+ ], DateValidator);
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.EmailValidator = void 0;
13
+ const constants_1 = require("./constants");
14
+ const PatternValidator_1 = require("./PatternValidator");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary Email Validator
18
+ *
19
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#EMAIL}
20
+ *
21
+ * @class EmailValidator
22
+ * @extends PatternValidator
23
+ *
24
+ * @category Validators
25
+ */
26
+ let EmailValidator = class EmailValidator extends PatternValidator_1.PatternValidator {
27
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.EMAIL) {
28
+ super(message);
29
+ }
30
+ /**
31
+ * @summary Validates a model
32
+ *
33
+ * @param {string} value
34
+ * @param {PatternValidatorOptions} [options]
35
+ *
36
+ * @return {string | undefined}
37
+ *
38
+ * @override
39
+ *
40
+ * @see Validator#hasErrors
41
+ */
42
+ hasErrors(value, options = {}) {
43
+ return super.hasErrors(value, {
44
+ ...options,
45
+ pattern: options?.pattern || constants_1.DEFAULT_PATTERNS.EMAIL,
46
+ });
47
+ }
48
+ };
49
+ exports.EmailValidator = EmailValidator;
50
+ exports.EmailValidator = EmailValidator = __decorate([
51
+ (0, decorators_1.validator)(constants_1.ValidationKeys.EMAIL),
52
+ __metadata("design:paramtypes", [String])
53
+ ], EmailValidator);
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ListValidator = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary List Validator
18
+ *
19
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#LIST}
20
+ *
21
+ * @class ListValidator
22
+ * @extends Validator
23
+ *
24
+ * @category Validators
25
+ */
26
+ let ListValidator = class ListValidator extends Validator_1.Validator {
27
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.LIST) {
28
+ super(message, Array.name, Set.name);
29
+ }
30
+ /**
31
+ * @summary Validates a model
32
+ *
33
+ * @param {any[] | Set<any>} value
34
+ * @param {ListValidatorOptions} options
35
+ *
36
+ * @return {string | undefined}
37
+ *
38
+ * @override
39
+ *
40
+ * @see Validator#hasErrors
41
+ */
42
+ hasErrors(value, options) {
43
+ if (!value || (Array.isArray(value) ? !value.length : !value.size))
44
+ return;
45
+ const clazz = Array.isArray(options.clazz)
46
+ ? options.clazz
47
+ : [options.clazz];
48
+ let val, isValid = true;
49
+ for (let i = 0; i < (Array.isArray(value) ? value.length : value.size); i++) {
50
+ val = value[i];
51
+ switch (typeof val) {
52
+ case "object":
53
+ case "function":
54
+ isValid = clazz.includes(val.constructor?.name);
55
+ break;
56
+ default:
57
+ isValid = clazz.some((c) => typeof val === c.toLowerCase());
58
+ break;
59
+ }
60
+ }
61
+ return isValid
62
+ ? undefined
63
+ : this.getMessage(options.message || this.message, clazz);
64
+ }
65
+ };
66
+ exports.ListValidator = ListValidator;
67
+ exports.ListValidator = ListValidator = __decorate([
68
+ (0, decorators_1.validator)(constants_1.ValidationKeys.LIST),
69
+ __metadata("design:paramtypes", [String])
70
+ ], ListValidator);
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MaxLengthValidator = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary Maximum Length Validator
18
+ * @description Validates strings and Arrays on their maximum length
19
+ *
20
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#MAX_LENGTH}
21
+ *
22
+ * @class MinLengthValidator
23
+ * @extends Validator
24
+ *
25
+ * @category Validators
26
+ */
27
+ let MaxLengthValidator = class MaxLengthValidator extends Validator_1.Validator {
28
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.MAX_LENGTH) {
29
+ super(message, String.name, Array.name);
30
+ }
31
+ /**
32
+ * @summary Validates a model
33
+ *
34
+ * @param {string} value
35
+ * @param {MaxLengthValidatorOptions} options
36
+ *
37
+ * @return {string | undefined}
38
+ *
39
+ * @override
40
+ *
41
+ * @see Validator#hasErrors
42
+ */
43
+ hasErrors(value, options) {
44
+ if (typeof value === "undefined")
45
+ return;
46
+ return value.length > options.maxLength
47
+ ? this.getMessage(options.message || this.message, options.maxLength)
48
+ : undefined;
49
+ }
50
+ };
51
+ exports.MaxLengthValidator = MaxLengthValidator;
52
+ exports.MaxLengthValidator = MaxLengthValidator = __decorate([
53
+ (0, decorators_1.validator)(constants_1.ValidationKeys.MAX_LENGTH),
54
+ __metadata("design:paramtypes", [String])
55
+ ], MaxLengthValidator);
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MaxValidator = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary Max Validator
18
+ *
19
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#MAX}
20
+ *
21
+ * @class MaxValidator
22
+ * @extends Validator
23
+ *
24
+ * @category Validators
25
+ */
26
+ let MaxValidator = class MaxValidator extends Validator_1.Validator {
27
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.MAX) {
28
+ super(message, "number", "Date", "string");
29
+ }
30
+ /**
31
+ * @summary Validates a Model
32
+ *
33
+ * @param {string} value
34
+ * @param {MaxValidatorOptions} options
35
+ *
36
+ * @return {string | undefined}
37
+ *
38
+ * @override
39
+ *
40
+ * @see Validator#hasErrors
41
+ */
42
+ hasErrors(value, options) {
43
+ if (typeof value === "undefined")
44
+ return;
45
+ let { max } = options;
46
+ if (value instanceof Date && !(max instanceof Date)) {
47
+ max = new Date(max);
48
+ if (Number.isNaN(max.getDate()))
49
+ throw new Error("Invalid Max param defined");
50
+ }
51
+ return value > max
52
+ ? this.getMessage(options.message || this.message, max)
53
+ : undefined;
54
+ }
55
+ };
56
+ exports.MaxValidator = MaxValidator;
57
+ exports.MaxValidator = MaxValidator = __decorate([
58
+ (0, decorators_1.validator)(constants_1.ValidationKeys.MAX),
59
+ __metadata("design:paramtypes", [String])
60
+ ], MaxValidator);
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MinLengthValidator = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary Minimum Length Validator
18
+ * @description Validates strings and Arrays on their minimum length
19
+ *
20
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#MIN_LENGTH}
21
+ *
22
+ * @class MinLengthValidator
23
+ * @extends Validator
24
+ *
25
+ * @category Validators
26
+ */
27
+ let MinLengthValidator = class MinLengthValidator extends Validator_1.Validator {
28
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.MIN_LENGTH) {
29
+ super(message, String.name, Array.name);
30
+ }
31
+ /**
32
+ *
33
+ * @param {string | Array} value
34
+ * @param {MinLengthValidatorOptions} options
35
+ *
36
+ * @return {string | undefined}
37
+ *
38
+ * @memberOf MinLengthValidator
39
+ * @override
40
+ *
41
+ * @see Validator#hasErrors
42
+ */
43
+ hasErrors(value, options) {
44
+ if (typeof value === "undefined")
45
+ return;
46
+ return value.length < options.minLength
47
+ ? this.getMessage(options.message || this.message, options.minLength)
48
+ : undefined;
49
+ }
50
+ };
51
+ exports.MinLengthValidator = MinLengthValidator;
52
+ exports.MinLengthValidator = MinLengthValidator = __decorate([
53
+ (0, decorators_1.validator)(constants_1.ValidationKeys.MIN_LENGTH),
54
+ __metadata("design:paramtypes", [String])
55
+ ], MinLengthValidator);
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MinValidator = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary Min Validator
18
+ *
19
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#MIN}
20
+ *
21
+ * @class MinValidator
22
+ * @extends Validator
23
+ *
24
+ * @category Validators
25
+ */
26
+ let MinValidator = class MinValidator extends Validator_1.Validator {
27
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.MIN) {
28
+ super(message, "number", "Date", "string");
29
+ }
30
+ /**
31
+ * @summary Validates Model
32
+ *
33
+ * @param {string} value
34
+ * @param {MaxValidatorOptions} options
35
+ *
36
+ * @return {string | undefined}
37
+ *
38
+ * @override
39
+ *
40
+ * @see Validator#hasErrors
41
+ */
42
+ hasErrors(value, options) {
43
+ if (typeof value === "undefined")
44
+ return;
45
+ let { min } = options;
46
+ if (value instanceof Date && !(min instanceof Date)) {
47
+ min = new Date(min);
48
+ if (Number.isNaN(min.getDate()))
49
+ throw new Error("Invalid Min param defined");
50
+ }
51
+ return value < min
52
+ ? this.getMessage(options.message || this.message, min)
53
+ : undefined;
54
+ }
55
+ };
56
+ exports.MinValidator = MinValidator;
57
+ exports.MinValidator = MinValidator = __decorate([
58
+ (0, decorators_1.validator)(constants_1.ValidationKeys.MIN),
59
+ __metadata("design:paramtypes", [String])
60
+ ], MinValidator);