@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,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.PasswordValidator = void 0;
13
+ const PatternValidator_1 = require("./PatternValidator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary Handles Password Validation
18
+ *
19
+ * @param {string} [errorMessage] defaults to {@link DEFAULT_ERROR_MESSAGES#PASSWORD}
20
+ *
21
+ * @class PasswordValidator
22
+ * @extends PatternValidator
23
+ *
24
+ * @category Validators
25
+ */
26
+ let PasswordValidator = class PasswordValidator extends PatternValidator_1.PatternValidator {
27
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.PASSWORD) {
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 PatternValidator#hasErrors
41
+ */
42
+ hasErrors(value, options = {}) {
43
+ return super.hasErrors(value, {
44
+ ...options,
45
+ message: options.message || this.message,
46
+ });
47
+ }
48
+ };
49
+ exports.PasswordValidator = PasswordValidator;
50
+ exports.PasswordValidator = PasswordValidator = __decorate([
51
+ (0, decorators_1.validator)(constants_1.ValidationKeys.PASSWORD),
52
+ __metadata("design:paramtypes", [Object])
53
+ ], PasswordValidator);
@@ -0,0 +1,73 @@
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.PatternValidator = exports.regexpParser = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ exports.regexpParser = new RegExp("^/(.+)/([gimus]*)$");
17
+ /**
18
+ * @summary Pattern Validator
19
+ *
20
+ * @param {string} [key] defaults to {@link ValidationKeys#PATTERN}
21
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#PATTERN}
22
+ *
23
+ * @class PatternValidator
24
+ * @extends Validator
25
+ *
26
+ * @category Validators
27
+ */
28
+ let PatternValidator = class PatternValidator extends Validator_1.Validator {
29
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.PATTERN) {
30
+ super(message, "string");
31
+ }
32
+ /**
33
+ * @summary parses and validates a pattern
34
+ *
35
+ * @param {string} pattern
36
+ * @private
37
+ */
38
+ getPattern(pattern) {
39
+ if (!exports.regexpParser.test(pattern))
40
+ return new RegExp(pattern);
41
+ const match = pattern.match(exports.regexpParser);
42
+ return new RegExp(match[1], match[2]);
43
+ }
44
+ /**
45
+ * @summary Validates a Model
46
+ *
47
+ * @param {string} value
48
+ * @param {PatternValidatorOptions} options
49
+ *
50
+ * @return {string | undefined}
51
+ *
52
+ * @override
53
+ *
54
+ * @see Validator#hasErrors
55
+ */
56
+ hasErrors(value, options) {
57
+ if (!value)
58
+ return;
59
+ let { pattern } = options;
60
+ if (!pattern)
61
+ throw new Error("Missing Pattern");
62
+ pattern = typeof pattern === "string" ? this.getPattern(pattern) : pattern;
63
+ pattern.lastIndex = 0; // resets pattern position for repeat validation requests
64
+ return !pattern.test(value)
65
+ ? this.getMessage(options.message || this.message)
66
+ : undefined;
67
+ }
68
+ };
69
+ exports.PatternValidator = PatternValidator;
70
+ exports.PatternValidator = PatternValidator = __decorate([
71
+ (0, decorators_1.validator)(constants_1.ValidationKeys.PATTERN),
72
+ __metadata("design:paramtypes", [String])
73
+ ], PatternValidator);
@@ -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.RequiredValidator = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary Required Validator
18
+ *
19
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#REQUIRED}
20
+ *
21
+ * @class RequiredValidator
22
+ * @extends Validator
23
+ *
24
+ * @category Validators
25
+ */
26
+ let RequiredValidator = class RequiredValidator extends Validator_1.Validator {
27
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.REQUIRED) {
28
+ super(message);
29
+ }
30
+ /**
31
+ * @summary Validates a model
32
+ *
33
+ * @param {string} value
34
+ * @param {ValidatorOptions} [options={}]
35
+ *
36
+ * @return {string | undefined}
37
+ *
38
+ * @override
39
+ *
40
+ * @see Validator#hasErrors
41
+ */
42
+ hasErrors(value, options = {}) {
43
+ switch (typeof value) {
44
+ case "boolean":
45
+ case "number":
46
+ return typeof value === "undefined"
47
+ ? this.getMessage(options.message || this.message)
48
+ : undefined;
49
+ default:
50
+ return !value
51
+ ? this.getMessage(options.message || this.message)
52
+ : undefined;
53
+ }
54
+ }
55
+ };
56
+ exports.RequiredValidator = RequiredValidator;
57
+ exports.RequiredValidator = RequiredValidator = __decorate([
58
+ (0, decorators_1.validator)(constants_1.ValidationKeys.REQUIRED),
59
+ __metadata("design:paramtypes", [String])
60
+ ], RequiredValidator);
@@ -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.StepValidator = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary Step Validator
18
+ *
19
+ * @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#STEP}
20
+ *
21
+ * @class StepValidator
22
+ * @extends Validator
23
+ *
24
+ * @category Validators
25
+ */
26
+ let StepValidator = class StepValidator extends Validator_1.Validator {
27
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.STEP) {
28
+ super(message, "number", "string");
29
+ }
30
+ /**
31
+ * @summary Validates a model
32
+ *
33
+ * @param {string} value
34
+ * @param {number} step
35
+ * @param {StepValidatorOptions} 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 Number(value) % Number(options.step) !== 0
47
+ ? this.getMessage(options.message || this.message, options.step)
48
+ : undefined;
49
+ }
50
+ };
51
+ exports.StepValidator = StepValidator;
52
+ exports.StepValidator = StepValidator = __decorate([
53
+ (0, decorators_1.validator)(constants_1.ValidationKeys.STEP),
54
+ __metadata("design:paramtypes", [String])
55
+ ], StepValidator);
@@ -0,0 +1,63 @@
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.TypeValidator = void 0;
13
+ const Validator_1 = require("./Validator");
14
+ const constants_1 = require("./constants");
15
+ const decorators_1 = require("./decorators");
16
+ const Validation_1 = require("../Validation");
17
+ const constants_2 = require("../../utils/constants");
18
+ const reflection_1 = require("@decaf-ts/reflection");
19
+ /**
20
+ * @summary Required Validator
21
+ *
22
+ * @class RequiredValidator
23
+ * @extends Validator
24
+ *
25
+ * @category Validators
26
+ */
27
+ let TypeValidator = class TypeValidator extends Validator_1.Validator {
28
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.TYPE) {
29
+ super(message);
30
+ }
31
+ /**
32
+ * @summary Validates a model
33
+ * @param {string} value
34
+ * @param {TypeValidatorOptions} 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; // Don't try and enforce type if undefined
45
+ const { types, message } = options;
46
+ if (!reflection_1.Reflection.evaluateDesignTypes(value, types))
47
+ return this.getMessage(message || this.message, typeof types === "string"
48
+ ? types
49
+ : Array.isArray(types)
50
+ ? types.join(", ")
51
+ : types.name, typeof value);
52
+ }
53
+ };
54
+ exports.TypeValidator = TypeValidator;
55
+ exports.TypeValidator = TypeValidator = __decorate([
56
+ (0, decorators_1.validator)(constants_1.ValidationKeys.TYPE),
57
+ __metadata("design:paramtypes", [String])
58
+ ], TypeValidator);
59
+ Validation_1.Validation.register({
60
+ validator: TypeValidator,
61
+ validationKey: constants_2.ModelKeys.TYPE,
62
+ save: false,
63
+ });
@@ -0,0 +1,52 @@
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.URLValidator = void 0;
13
+ const constants_1 = require("./constants");
14
+ const PatternValidator_1 = require("./PatternValidator");
15
+ const decorators_1 = require("./decorators");
16
+ /**
17
+ * @summary URL Validator
18
+ * @description Pattern from {@link https://gist.github.com/dperini/729294}
19
+ *
20
+ * @class URLValidator
21
+ * @extends PatternValidator
22
+ *
23
+ * @category Validators
24
+ */
25
+ let URLValidator = class URLValidator extends PatternValidator_1.PatternValidator {
26
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.URL) {
27
+ super(message);
28
+ }
29
+ /**
30
+ * @summary Validates a model
31
+ *
32
+ * @param {string} value
33
+ * @param {PatternValidatorOptions} [options={}]
34
+ *
35
+ * @return {string | undefined}
36
+ *
37
+ * @override
38
+ *
39
+ * @see Validator#hasErrors
40
+ */
41
+ hasErrors(value, options = {}) {
42
+ return super.hasErrors(value, {
43
+ ...options,
44
+ pattern: options.pattern || constants_1.DEFAULT_PATTERNS.URL,
45
+ });
46
+ }
47
+ };
48
+ exports.URLValidator = URLValidator;
49
+ exports.URLValidator = URLValidator = __decorate([
50
+ (0, decorators_1.validator)(constants_1.ValidationKeys.URL),
51
+ __metadata("design:paramtypes", [String])
52
+ ], URLValidator);
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Validator = void 0;
4
+ const constants_1 = require("./constants");
5
+ const strings_1 = require("../../utils/strings");
6
+ const reflection_1 = require("@decaf-ts/reflection");
7
+ /**
8
+ * @summary Base Implementation for Validators
9
+ * @description Provides the underlying functionality for {@link Validator}s
10
+ *
11
+ * @param {string} validationKey the key to register the validator under
12
+ * @param {string} [message] the error message. Defaults to {@link DEFAULT_ERROR_MESSAGES#DEFAULT}
13
+ * @param {string[]} [acceptedTypes] defines the value types this validator can validate
14
+ *
15
+ * @class Validator
16
+ * @abstract
17
+ * @category Validators
18
+ */
19
+ class Validator {
20
+ constructor(message = constants_1.DEFAULT_ERROR_MESSAGES.DEFAULT, ...acceptedTypes) {
21
+ this.message = message;
22
+ if (acceptedTypes.length)
23
+ this.acceptedTypes = acceptedTypes;
24
+ if (this.acceptedTypes)
25
+ this.hasErrors = this.checkTypeAndHasErrors(this.hasErrors.bind(this));
26
+ }
27
+ /**
28
+ * @summary builds the error message
29
+ * @param {string} message
30
+ * @param {any[]} args
31
+ * @protected
32
+ */
33
+ getMessage(message, ...args) {
34
+ return (0, strings_1.sf)(message, ...args);
35
+ }
36
+ /**
37
+ * @summary Validates type
38
+ * @param {any} unbound
39
+ * @private
40
+ */
41
+ checkTypeAndHasErrors(unbound) {
42
+ return function (value, ...args) {
43
+ if (value === undefined || !this.acceptedTypes)
44
+ return unbound(value, ...args);
45
+ if (!reflection_1.Reflection.checkTypes(value, this.acceptedTypes))
46
+ return this.getMessage(constants_1.DEFAULT_ERROR_MESSAGES.TYPE, this.acceptedTypes.join(", "), typeof value);
47
+ return unbound(value, ...args);
48
+ }.bind(this);
49
+ }
50
+ }
51
+ exports.Validator = Validator;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidatorRegistry = void 0;
4
+ exports.isValidator = isValidator;
5
+ /**
6
+ * @summary Duck typing for Validators
7
+ * @function isValidator
8
+ * @param val
9
+ */
10
+ function isValidator(val) {
11
+ return val.constructor && val["hasErrors"];
12
+ }
13
+ /**
14
+ * @summary Base Implementation of a Validator Registry
15
+ *
16
+ * @prop {Validator[]} [validators] the initial validators to register
17
+ *
18
+ * @class ValidatorRegistry
19
+ * @implements IValidatorRegistry<T>
20
+ *
21
+ * @category Validation
22
+ */
23
+ class ValidatorRegistry {
24
+ constructor(...validators) {
25
+ this.cache = {};
26
+ this.customKeyCache = {};
27
+ this.register(...validators);
28
+ }
29
+ /**
30
+ * @summary retrieves the custom keys
31
+ */
32
+ getCustomKeys() {
33
+ return Object.assign({}, this.customKeyCache);
34
+ }
35
+ /**
36
+ * @summary retrieves the registered validators keys
37
+ */
38
+ getKeys() {
39
+ return Object.keys(this.cache);
40
+ }
41
+ /**
42
+ * @summary Retrieves a validator
43
+ *
44
+ * @param {string} validatorKey one of the {@link ValidationKeys}
45
+ * @return {Validator | undefined} the registered Validator or undefined if there is nono matching the provided key
46
+ */
47
+ get(validatorKey) {
48
+ if (!(validatorKey in this.cache))
49
+ return undefined;
50
+ const classOrInstance = this.cache[validatorKey];
51
+ if (isValidator(classOrInstance))
52
+ return classOrInstance;
53
+ const constructor = classOrInstance.default || classOrInstance;
54
+ const instance = new constructor();
55
+ this.cache[validatorKey] = instance;
56
+ return instance;
57
+ }
58
+ /**
59
+ * @summary Registers the provided validators onto the registry
60
+ *
61
+ * @param {T[] | ValidatorDefinition[]} validator
62
+ */
63
+ register(...validator) {
64
+ validator.forEach((v) => {
65
+ if (isValidator(v)) {
66
+ // const k =
67
+ if (v.validationKey in this.cache)
68
+ return;
69
+ this.cache[v.validationKey] = v;
70
+ }
71
+ else {
72
+ const { validationKey, validator, save } = v;
73
+ if (validationKey in this.cache)
74
+ return;
75
+ this.cache[validationKey] = validator;
76
+ if (!save)
77
+ return;
78
+ const obj = {};
79
+ obj[validationKey.toUpperCase()] = validationKey;
80
+ this.customKeyCache = Object.assign({}, this.customKeyCache, obj);
81
+ }
82
+ });
83
+ }
84
+ }
85
+ exports.ValidatorRegistry = ValidatorRegistry;
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_PATTERNS = exports.DEFAULT_ERROR_MESSAGES = exports.DAYS_OF_WEEK_NAMES = exports.MONTH_NAMES = exports.ValidationKeys = void 0;
4
+ const constants_1 = require("../../utils/constants");
5
+ /**
6
+ * @summary The keys used for validation
7
+ *
8
+ * @property {string} REFLECT prefixes others
9
+ * @property {string} REQUIRED sets as required
10
+ * @property {string} MIN defines min value
11
+ * @property {string} MAX defines max value
12
+ * @property {string} STEP defines step
13
+ * @property {string} MIN_LENGTH defines min length
14
+ * @property {string} MAX_LENGTH defines max length
15
+ * @property {string} PATTERN defines pattern
16
+ * @property {string} EMAIL defines email
17
+ * @property {string} URL defines url
18
+ * @property {string} DATE defines date
19
+ * @property {string} TYPE defines type
20
+ * @property {string} PASSWORD defines password
21
+ * @property {string} LIST defines list
22
+ *
23
+ * @constant ValidationKeys
24
+ * @memberOf module:decorator-validation.Validation
25
+ * @category Validation
26
+ */
27
+ exports.ValidationKeys = {
28
+ REFLECT: `${constants_1.ModelKeys.REFLECT}validation.`,
29
+ VALIDATOR: "validator",
30
+ REQUIRED: "required",
31
+ MIN: "min",
32
+ MAX: "max",
33
+ STEP: "step",
34
+ MIN_LENGTH: "minlength",
35
+ MAX_LENGTH: "maxlength",
36
+ PATTERN: "pattern",
37
+ EMAIL: "email",
38
+ URL: "url",
39
+ DATE: "date",
40
+ TYPE: "type",
41
+ PASSWORD: "password",
42
+ LIST: "list",
43
+ };
44
+ /**
45
+ * @summary list of month names
46
+ * @description Stores month names. Can be changed for localization purposes
47
+ *
48
+ * @constant MONTH_NAMES
49
+ * @memberOf module:decorator-validation.Validation
50
+ * @category Validation
51
+ */
52
+ exports.MONTH_NAMES = [
53
+ "January",
54
+ "February",
55
+ "March",
56
+ "April",
57
+ "May",
58
+ "June",
59
+ "July",
60
+ "August",
61
+ "September",
62
+ "October",
63
+ "November",
64
+ "December",
65
+ ];
66
+ /**
67
+ * @summary list of names of days of the week
68
+ * @description Stores names for days of the week. Can be changed for localization purposes
69
+ *
70
+ * @constant DAYS_OF_WEEK_NAMES
71
+ * @memberOf module:decorator-validation.Validation
72
+ * @category Validation
73
+ */
74
+ exports.DAYS_OF_WEEK_NAMES = [
75
+ "Sunday",
76
+ "Monday",
77
+ "Tuesday",
78
+ "Wednesday",
79
+ "Thursday",
80
+ "Friday",
81
+ "Saturday",
82
+ ];
83
+ /**
84
+ * @summary Defines the default error messages
85
+ *
86
+ * @property {string} REQUIRED default error message
87
+ * @property {string} MIN default error message
88
+ * @property {string} MAX default error message
89
+ * @property {string} MIN_LENGTH default error message
90
+ * @property {string} MAX_LENGTH default error message
91
+ * @property {string} PATTERN default error message
92
+ * @property {string} EMAIL default error message
93
+ * @property {string} URL default error message
94
+ * @property {string} TYPE default error message
95
+ * @property {string} STEP default error message
96
+ * @property {string} DATE default error message
97
+ * @property {string} DEFAULT default error message
98
+ * @property {string} PASSWORD default error message
99
+ * @property {string} LIST default error message
100
+ * @property {string} LIST_INSIDE default error message
101
+ * @property {string} MODEL_NOT_FOUND default error message
102
+ *
103
+ * @constant DEFAULT_ERROR_MESSAGES
104
+ * @memberOf module:decorator-validation.Validation
105
+ * @category Validation
106
+ */
107
+ exports.DEFAULT_ERROR_MESSAGES = {
108
+ REQUIRED: "This field is required",
109
+ MIN: "The minimum value is {0}",
110
+ MAX: "The maximum value is {0}",
111
+ MIN_LENGTH: "The minimum length is {0}",
112
+ MAX_LENGTH: "The maximum length is {0}",
113
+ PATTERN: "The value does not match the pattern",
114
+ EMAIL: "The value is not a valid email",
115
+ URL: "The value is not a valid URL",
116
+ TYPE: "Invalid type. Expected {0}, received {1}",
117
+ STEP: "Invalid value. Not a step of {0}",
118
+ DATE: "Invalid value. not a valid Date",
119
+ DEFAULT: "There is an Error",
120
+ PASSWORD: "Must be at least 8 characters and contain one of number, lower and upper case letters, and special character (@$!%*?&_-.,)",
121
+ LIST: "Invalid list of {0}",
122
+ MODEL_NOT_FOUND: "No model registered under {0}",
123
+ };
124
+ /**
125
+ * @summary Defines the various default regexp patterns used
126
+ *
127
+ * @enum DEFAULT_PATTERNS
128
+ * @memberOf module:decorator-validation.Validation
129
+ * @category Validation
130
+ */
131
+ exports.DEFAULT_PATTERNS = {
132
+ EMAIL: /[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?/,
133
+ URL: /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i,
134
+ PASSWORD: {
135
+ CHAR8_ONE_OF_EACH: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&_\-.,])[A-Za-z\d@$!%*?&_\-.,]{8,}$/g,
136
+ },
137
+ };
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validator = validator;
4
+ const Validation_1 = require("../Validation");
5
+ const constants_1 = require("./constants");
6
+ const reflection_1 = require("@decaf-ts/reflection");
7
+ /**
8
+ * @summary Marks the class as a validator for a certain key.
9
+ * @description Registers the class in the {@link Validation} with the provided key
10
+ *
11
+ * @param {string} keys the validation key
12
+ *
13
+ * @function validator
14
+ *
15
+ * @category Decorators
16
+ */
17
+ function validator(...keys) {
18
+ return (0, reflection_1.apply)(((original) => {
19
+ keys.forEach((k) => {
20
+ Validation_1.Validation.register({
21
+ validator: original,
22
+ validationKey: k,
23
+ save: true,
24
+ });
25
+ });
26
+ return original;
27
+ }), (0, reflection_1.metadata)(Validation_1.Validation.key(constants_1.ValidationKeys.VALIDATOR), keys));
28
+ }