vrack2-core 0.0.1 → 1.0.1

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 (148) hide show
  1. package/README.md +33 -4
  2. package/docs/Bootstrap.md +77 -0
  3. package/docs/Container.md +124 -0
  4. package/docs/Device.md +272 -0
  5. package/docs/FastStart.md +111 -0
  6. package/docs/Structure.md +148 -0
  7. package/lib/Bootstrap.d.ts +79 -0
  8. package/lib/Bootstrap.js +103 -0
  9. package/lib/Container.d.ts +202 -6
  10. package/lib/Container.js +295 -27
  11. package/lib/IServiceStructure.d.ts +8 -0
  12. package/lib/IStructureDevice.d.ts +5 -0
  13. package/lib/ImportManager.d.ts +85 -3
  14. package/lib/ImportManager.js +122 -16
  15. package/lib/MainProcess.d.ts +30 -3
  16. package/lib/MainProcess.js +28 -6
  17. package/lib/Utility.d.ts +15 -21
  18. package/lib/Utility.js +40 -40
  19. package/lib/actions/Action.d.ts +10 -0
  20. package/lib/actions/Action.js +10 -0
  21. package/lib/actions/BasicAction.d.ts +60 -0
  22. package/lib/actions/BasicAction.js +62 -0
  23. package/lib/actions/GlobalAction.d.ts +5 -0
  24. package/lib/actions/GlobalAction.js +5 -0
  25. package/lib/actions/IAction.d.ts +7 -0
  26. package/lib/actions/ILocalAction.d.ts +7 -0
  27. package/lib/boot/BootClass.d.ts +93 -0
  28. package/lib/boot/BootClass.js +101 -0
  29. package/lib/boot/DeviceFileStorage.d.ts +38 -0
  30. package/lib/boot/DeviceFileStorage.js +112 -0
  31. package/lib/boot/DeviceManager.d.ts +190 -0
  32. package/lib/boot/DeviceManager.js +311 -0
  33. package/lib/boot/DeviceMetrics.d.ts +82 -0
  34. package/lib/boot/DeviceMetrics.js +128 -0
  35. package/lib/boot/StructureStorage.d.ts +59 -0
  36. package/lib/boot/StructureStorage.js +125 -0
  37. package/lib/errors/CoreError.d.ts +42 -25
  38. package/lib/errors/CoreError.js +44 -24
  39. package/lib/errors/ErrorManager.d.ts +18 -20
  40. package/lib/errors/ErrorManager.js +23 -22
  41. package/lib/index.d.ts +20 -4
  42. package/lib/index.js +28 -4
  43. package/lib/metrics/BasicMetric.d.ts +49 -0
  44. package/lib/metrics/BasicMetric.js +79 -0
  45. package/lib/metrics/IMetricSettings.d.ts +32 -0
  46. package/lib/metrics/IMetricSettings.js +2 -0
  47. package/lib/metrics/IvMs.d.ts +9 -0
  48. package/lib/metrics/IvMs.js +22 -0
  49. package/lib/metrics/IvS.d.ts +9 -0
  50. package/lib/metrics/IvS.js +22 -0
  51. package/lib/metrics/IvUs.d.ts +9 -0
  52. package/lib/metrics/IvUs.js +22 -0
  53. package/lib/metrics/Metric.d.ts +17 -0
  54. package/lib/metrics/Metric.js +27 -0
  55. package/lib/ports/BasicPort.d.ts +39 -0
  56. package/lib/ports/BasicPort.js +39 -0
  57. package/lib/ports/ILocalPort.d.ts +7 -0
  58. package/lib/ports/IPort.d.ts +7 -0
  59. package/lib/ports/Port.d.ts +10 -0
  60. package/lib/ports/Port.js +10 -0
  61. package/lib/ports/ReturnPort.d.ts +5 -0
  62. package/lib/ports/ReturnPort.js +5 -0
  63. package/lib/service/Device.d.ts +213 -78
  64. package/lib/service/Device.js +185 -83
  65. package/lib/service/DeviceConnect.d.ts +4 -8
  66. package/lib/service/DeviceConnect.js +4 -8
  67. package/lib/service/DevicePort.d.ts +15 -6
  68. package/lib/service/DevicePort.js +29 -12
  69. package/lib/service/IDeviceEvent.d.ts +4 -1
  70. package/lib/validator/IValidationProblem.d.ts +7 -0
  71. package/lib/validator/IValidationRule.d.ts +12 -0
  72. package/lib/validator/IValidationSubrule.d.ts +2 -0
  73. package/lib/validator/Rule.d.ts +6 -2
  74. package/lib/validator/Rule.js +12 -2
  75. package/lib/validator/Validator.d.ts +48 -3
  76. package/lib/validator/Validator.js +70 -18
  77. package/lib/validator/types/AnyType.d.ts +17 -0
  78. package/lib/validator/types/AnyType.js +34 -0
  79. package/lib/validator/types/ArrayType.d.ts +37 -4
  80. package/lib/validator/types/ArrayType.js +42 -6
  81. package/lib/validator/types/BasicType.d.ts +67 -7
  82. package/lib/validator/types/BasicType.js +74 -8
  83. package/lib/validator/types/BooleanType.d.ts +23 -0
  84. package/lib/validator/types/BooleanType.js +47 -0
  85. package/lib/validator/types/FunctionType.d.ts +17 -0
  86. package/lib/validator/types/FunctionType.js +38 -0
  87. package/lib/validator/types/NumberType.d.ts +40 -5
  88. package/lib/validator/types/NumberType.js +53 -14
  89. package/lib/validator/types/ObjectType.d.ts +32 -5
  90. package/lib/validator/types/ObjectType.js +42 -8
  91. package/lib/validator/types/StringType.d.ts +30 -5
  92. package/lib/validator/types/StringType.js +33 -7
  93. package/package.json +15 -14
  94. package/src/Bootstrap.ts +122 -0
  95. package/src/Container.ts +411 -43
  96. package/src/IServiceStructure.ts +9 -0
  97. package/src/IStructureDevice.ts +5 -0
  98. package/src/ImportManager.ts +119 -11
  99. package/src/MainProcess.ts +53 -8
  100. package/src/Utility.ts +35 -36
  101. package/src/actions/Action.ts +12 -0
  102. package/src/actions/BasicAction.ts +63 -0
  103. package/src/actions/GlobalAction.ts +5 -0
  104. package/src/actions/IAction.ts +7 -0
  105. package/src/actions/ILocalAction.ts +7 -0
  106. package/src/boot/BootClass.ts +117 -0
  107. package/src/boot/DeviceFileStorage.ts +96 -0
  108. package/src/boot/DeviceManager.ts +346 -0
  109. package/src/boot/DeviceMetrics.ts +129 -0
  110. package/src/boot/StructureStorage.ts +108 -0
  111. package/src/errors/CoreError.ts +52 -26
  112. package/src/errors/ErrorManager.ts +46 -33
  113. package/src/index.ts +30 -6
  114. package/src/metrics/BasicMetric.ts +84 -0
  115. package/src/metrics/IMetricSettings.ts +38 -0
  116. package/src/metrics/IvMs.ts +18 -0
  117. package/src/metrics/IvS.ts +18 -0
  118. package/src/metrics/IvUs.ts +17 -0
  119. package/src/metrics/Metric.ts +25 -0
  120. package/src/ports/BasicPort.ts +39 -0
  121. package/src/ports/ILocalPort.ts +7 -0
  122. package/src/ports/IPort.ts +7 -0
  123. package/src/ports/Port.ts +11 -1
  124. package/src/ports/ReturnPort.ts +5 -0
  125. package/src/service/Device.ts +234 -103
  126. package/src/service/DeviceConnect.ts +4 -8
  127. package/src/service/DevicePort.ts +30 -11
  128. package/src/service/IDeviceEvent.ts +4 -1
  129. package/src/validator/IValidationProblem.ts +7 -0
  130. package/src/validator/IValidationRule.ts +12 -0
  131. package/src/validator/IValidationSubrule.ts +3 -0
  132. package/src/validator/Rule.ts +16 -2
  133. package/src/validator/Validator.ts +74 -23
  134. package/src/validator/types/AnyType.ts +32 -0
  135. package/src/validator/types/ArrayType.ts +43 -7
  136. package/src/validator/types/BasicType.ts +78 -9
  137. package/src/validator/types/BooleanType.ts +49 -0
  138. package/src/validator/types/FunctionType.ts +39 -0
  139. package/src/validator/types/NumberType.ts +53 -15
  140. package/src/validator/types/ObjectType.ts +52 -14
  141. package/src/validator/types/StringType.ts +34 -10
  142. package/docs/RU-README.md +0 -6
  143. package/lib/DeviceManager.d.ts +0 -32
  144. package/lib/DeviceManager.js +0 -143
  145. package/lib/test.d.ts +0 -1
  146. package/lib/test.js +0 -58
  147. package/src/DeviceManager.ts +0 -124
  148. package/src/test.ts +0 -82
@@ -3,12 +3,57 @@ import CoreError from "../errors/CoreError";
3
3
  import IValidationProblem from './IValidationProblem';
4
4
  import IValidationRule from "./IValidationRule";
5
5
  export default class Validator {
6
+ /**
7
+ * Validation of the object by rule.
8
+ * You cannot validate a specific value.
9
+ * You can only validate an object property.
10
+ *
11
+ * For validation you need to create an object with rules
12
+ *
13
+ * ```ts
14
+ * const rules = {
15
+ * bool: Rule.boolean().require().default(true).description('Boolean checkbox')
16
+ * }
17
+ * ```
18
+ *
19
+ * After validating the object with the same properties
20
+ *
21
+ * ```
22
+ * Validator.validate(rules, { bool: 'not a boolean value?'})
23
+ * ```
24
+ *
25
+ * @param rules List of rules like a associate object
26
+ * @param data Data object for validate
27
+ */
6
28
  static validate(rules: {
7
29
  [key: string]: BasicType;
8
30
  }, data: {
9
31
  [key: string]: any;
10
32
  }): boolean;
11
- private static makeError;
12
- protected static makeProblem(err: CoreError, key: string, rule: IValidationRule): IValidationProblem;
13
- private static getClass;
33
+ /**
34
+ * Creates a top-level error for validation problems
35
+ *
36
+ * @param eList List of validation errors
37
+ */
38
+ protected static makeError(eList: Array<IValidationProblem>): void;
39
+ /**
40
+ * Create validation problem
41
+ *
42
+ * @param err Validation exception
43
+ * @param key key for getting value from object
44
+ * @param rule Checked rule
45
+ */
46
+ protected static makeProblem(err: CoreError, key: string, rule: IValidationRule, value: any): IValidationProblem;
47
+ /**
48
+ * Make message use message template
49
+ *
50
+ * If you want to change the default message, you can use a template in the message parameter
51
+ *
52
+ * @example
53
+ * ```js
54
+ * Rule.number().description('My number').message('{description} must by 1,2,3,4,5,6... not {value}')
55
+ * ```
56
+ */
57
+ protected static makeMessage(rule: IValidationRule, value: any): string;
58
+ protected static toInspect(value: any): any;
14
59
  }
@@ -7,51 +7,103 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
7
7
  return (mod && mod.__esModule) ? mod : { "default": mod };
8
8
  };
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
+ const util_1 = __importDefault(require("util"));
10
11
  const ErrorManager_1 = __importDefault(require("../errors/ErrorManager"));
11
- const ArrayType_1 = __importDefault(require("./types/ArrayType"));
12
- const NumberType_1 = __importDefault(require("./types/NumberType"));
13
- const ObjectType_1 = __importDefault(require("./types/ObjectType"));
14
- const StringType_1 = __importDefault(require("./types/StringType"));
15
12
  const CoreError_1 = __importDefault(require("../errors/CoreError"));
16
13
  ErrorManager_1.default.register('Validator', 'VXLeMLVnIXGf', 'VR_TYPE_NOT_EXISTS', 'Type not exists', {});
17
14
  ErrorManager_1.default.register('Validator', 'X1UP4P2HRHWd', 'VR_NOT_PASS', 'Validation error - data not pass', {});
18
15
  class Validator {
16
+ /**
17
+ * Validation of the object by rule.
18
+ * You cannot validate a specific value.
19
+ * You can only validate an object property.
20
+ *
21
+ * For validation you need to create an object with rules
22
+ *
23
+ * ```ts
24
+ * const rules = {
25
+ * bool: Rule.boolean().require().default(true).description('Boolean checkbox')
26
+ * }
27
+ * ```
28
+ *
29
+ * After validating the object with the same properties
30
+ *
31
+ * ```
32
+ * Validator.validate(rules, { bool: 'not a boolean value?'})
33
+ * ```
34
+ *
35
+ * @param rules List of rules like a associate object
36
+ * @param data Data object for validate
37
+ */
19
38
  static validate(rules, data) {
20
39
  const problems = [];
21
40
  for (const key of Object.keys(rules)) {
22
41
  const rule = rules[key];
23
- const ruleRaw = rule.export();
24
- const cs = Validator.getClass(ruleRaw.type);
25
42
  try {
26
- cs.validate(data, key, ruleRaw);
43
+ rule.validate(data, key);
27
44
  }
28
45
  catch (err) {
29
46
  if (err instanceof CoreError_1.default)
30
- problems.push(Validator.makeProblem(err, key, ruleRaw));
47
+ problems.push(Validator.makeProblem(err, key, rule.export(), data[key]));
31
48
  }
32
49
  }
33
50
  if (problems.length)
34
51
  Validator.makeError(problems);
35
52
  return true;
36
53
  }
54
+ /**
55
+ * Creates a top-level error for validation problems
56
+ *
57
+ * @param eList List of validation errors
58
+ */
37
59
  static makeError(eList) {
38
60
  throw ErrorManager_1.default.make('VR_NOT_PASS', {
39
61
  problems: eList
40
62
  });
41
63
  }
42
- static makeProblem(err, key, rule) {
43
- return {
44
- type: err.vShort, code: err.vCode, description: err.message, rule, arg: err.vAdd
64
+ /**
65
+ * Create validation problem
66
+ *
67
+ * @param err Validation exception
68
+ * @param key key for getting value from object
69
+ * @param rule Checked rule
70
+ */
71
+ static makeProblem(err, key, rule, value) {
72
+ if (rule.message)
73
+ err.message = this.makeMessage(rule, value);
74
+ const nvp = {
75
+ type: err.vShort, code: err.vCode, fieldKey: key, description: err.message, rule, arg: {}
45
76
  };
77
+ for (const sk of err.vAdd)
78
+ nvp.arg[sk] = err[sk];
79
+ return nvp;
46
80
  }
47
- static getClass(cn) {
48
- switch (cn) {
49
- case 'string': return StringType_1.default;
50
- case 'number': return NumberType_1.default;
51
- case 'object': return ObjectType_1.default;
52
- case 'array': return ArrayType_1.default;
81
+ /**
82
+ * Make message use message template
83
+ *
84
+ * If you want to change the default message, you can use a template in the message parameter
85
+ *
86
+ * @example
87
+ * ```js
88
+ * Rule.number().description('My number').message('{description} must by 1,2,3,4,5,6... not {value}')
89
+ * ```
90
+ */
91
+ static makeMessage(rule, value) {
92
+ let message = rule.message;
93
+ const val = Validator.toInspect(value);
94
+ const example = Validator.toInspect(rule.example);
95
+ const def = Validator.toInspect(rule.default);
96
+ message.replace('{value}', val);
97
+ message.replace('{example}', example);
98
+ message.replace('{default}', def);
99
+ message.replace('{description}', rule.description);
100
+ return message;
101
+ }
102
+ static toInspect(value) {
103
+ if (typeof value === "object" || typeof value === "function") {
104
+ return util_1.default.inspect(value, { showHidden: false, depth: null, compact: false });
53
105
  }
54
- throw ErrorManager_1.default.make('VR_TYPE_NOT_EXISTS', { type: cn });
106
+ return value;
55
107
  }
56
108
  }
57
109
  exports.default = Validator;
@@ -0,0 +1,17 @@
1
+ import BasicType from "./BasicType";
2
+ export default class AnyType extends BasicType {
3
+ constructor();
4
+ /**
5
+ * Setting the default value
6
+ */
7
+ default(def: any): this;
8
+ /**
9
+ * Method of validation of this type
10
+ *
11
+ * @param obj Validation object
12
+ * @param key Key for getting value from object
13
+ */
14
+ validate(obj: {
15
+ [key: string]: any;
16
+ }, key: string): boolean;
17
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright © 2022 Boris Bobylev. All rights reserved.
4
+ * Licensed under the Apache License, Version 2.0
5
+ */
6
+ var __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const BasicType_1 = __importDefault(require("./BasicType"));
11
+ class AnyType extends BasicType_1.default {
12
+ constructor() {
13
+ super();
14
+ this.rule.type = 'any';
15
+ }
16
+ /**
17
+ * Setting the default value
18
+ */
19
+ default(def) {
20
+ this.rule.default = def;
21
+ return this;
22
+ }
23
+ /**
24
+ * Method of validation of this type
25
+ *
26
+ * @param obj Validation object
27
+ * @param key Key for getting value from object
28
+ */
29
+ validate(obj, key) {
30
+ this.basicValidate(obj, key);
31
+ return true;
32
+ }
33
+ }
34
+ exports.default = AnyType;
@@ -1,14 +1,47 @@
1
- import IValidationRule from "../IValidationRule";
2
1
  import BasicType from "./BasicType";
3
2
  import IValidationSubrule from "../IValidationSubrule";
4
3
  export default class ArrayType extends BasicType {
5
4
  constructor();
5
+ /**
6
+ * Setting the default value
7
+ */
6
8
  default(def: Array<any>): this;
9
+ /**
10
+ * Sets the rule to be applied to each element of the array
11
+ *
12
+ * @example
13
+ *
14
+ * ```
15
+ * Rule.array().require().content(
16
+ * Rule.string().default('').maxLength(24).description('Element of list')
17
+ * )
18
+ * ```
19
+ *
20
+ */
7
21
  content(t: BasicType): this;
8
- static validate(obj: {
22
+ /**
23
+ * Example of a valid value for this rule
24
+ *
25
+ * @param ex Example valid value
26
+ */
27
+ example(ex: Array<any>): this;
28
+ /**
29
+ * Method of validation of this type
30
+ *
31
+ * @param obj Validation object
32
+ * @param key Key for getting value from object
33
+ */
34
+ validate(obj: {
9
35
  [key: string]: any;
10
- }, key: string, rule: IValidationRule): boolean;
11
- protected static checkContent(obj: {
36
+ }, key: string): boolean;
37
+ /**
38
+ * Checks the rules for content inside the array
39
+ *
40
+ * @param obj Validation object
41
+ * @param key Key for getting value from object
42
+ * @param sub Sub rule for check array content
43
+ */
44
+ protected checkContent(obj: {
12
45
  [key: string]: any;
13
46
  }, key: string, sub: IValidationSubrule): void;
14
47
  }
@@ -16,28 +16,64 @@ class ArrayType extends BasicType_1.default {
16
16
  super();
17
17
  this.rule.type = 'array';
18
18
  }
19
+ /**
20
+ * Setting the default value
21
+ */
19
22
  default(def) {
20
23
  this.rule.default = def;
21
24
  return this;
22
25
  }
26
+ /**
27
+ * Sets the rule to be applied to each element of the array
28
+ *
29
+ * @example
30
+ *
31
+ * ```
32
+ * Rule.array().require().content(
33
+ * Rule.string().default('').maxLength(24).description('Element of list')
34
+ * )
35
+ * ```
36
+ *
37
+ */
23
38
  content(t) {
24
39
  this.rule.rules.push({ name: 'contain', args: t });
25
40
  return this;
26
41
  }
27
- static validate(obj, key, rule) {
28
- BasicType_1.default.validate(obj, key, rule);
42
+ /**
43
+ * Example of a valid value for this rule
44
+ *
45
+ * @param ex Example valid value
46
+ */
47
+ example(ex) {
48
+ this.rule.example = ex;
49
+ return this;
50
+ }
51
+ /**
52
+ * Method of validation of this type
53
+ *
54
+ * @param obj Validation object
55
+ * @param key Key for getting value from object
56
+ */
57
+ validate(obj, key) {
58
+ this.basicValidate(obj, key);
29
59
  if (!Array.isArray(obj[key]))
30
60
  throw ErrorManager_1.default.make('VR_IS_NOT_ARRAY', {});
31
- for (const subrule of rule.rules) {
61
+ for (const subrule of this.rule.rules) {
32
62
  switch (subrule.name) {
33
63
  case 'fields':
34
- ArrayType.checkContent(obj, key, subrule);
35
- break;
64
+ this.checkContent(obj, key, subrule);
36
65
  }
37
66
  }
38
67
  return true;
39
68
  }
40
- static checkContent(obj, key, sub) {
69
+ /**
70
+ * Checks the rules for content inside the array
71
+ *
72
+ * @param obj Validation object
73
+ * @param key Key for getting value from object
74
+ * @param sub Sub rule for check array content
75
+ */
76
+ checkContent(obj, key, sub) {
41
77
  const sw = { value: undefined };
42
78
  const tr = { value: sub.args };
43
79
  for (const index of obj[key]) {
@@ -3,16 +3,76 @@ export default class BasicType {
3
3
  protected rule: IValidationRule;
4
4
  constructor();
5
5
  require(): this;
6
+ /**
7
+ * Example of a valid value for this rule
8
+ *
9
+ * @param ex Example valid value
10
+ */
6
11
  example(ex: any): this;
12
+ /**
13
+ * Description of the validated object property
14
+ *
15
+ * @param desc
16
+ */
7
17
  description(desc: string): this;
8
- export(): IValidationRule;
9
- static validate(obj: {
18
+ /**
19
+ * Make message use message template
20
+ *
21
+ * If you want to change the default message, you can use a template in the message parameter
22
+ *
23
+ * @example
24
+ * ```js
25
+ * Rule.number().description('My number').message('{description} must by 1,2,3,4,5,6... not {value}')
26
+ * ```
27
+ * Use {description} {value} {default} {example} in template
28
+ */
29
+ message(mess: string): this;
30
+ /**
31
+ * Exporting a rule for use
32
+ * Typically used within VRack or VRack-Core
33
+ *
34
+ * !!! hide for external users !!!
35
+ * @private
36
+ */
37
+ export(): any;
38
+ /**
39
+ * This method will be executed when converting a Rule object to JSON
40
+ */
41
+ toJSON(): IValidationRule;
42
+ /**
43
+ * Performs general validation rules
44
+ *
45
+ * @param obj Validation object
46
+ * @param key Key for getting value from object
47
+ */
48
+ protected basicValidate(obj: {
10
49
  [key: string]: any;
11
- }, key: string, rule: IValidationRule): boolean;
12
- protected static checkDefault(obj: {
50
+ }, key: string): boolean;
51
+ /**
52
+ * Method of validation of this type
53
+ *
54
+ * @param obj Validation object
55
+ * @param key Key for getting value from object
56
+ */
57
+ validate(obj: {
13
58
  [key: string]: any;
14
- }, key: string, rule: IValidationRule): void;
15
- protected static checkRequire(obj: {
59
+ }, key: string): boolean;
60
+ /**
61
+ * Sets the default value if it has not been set
62
+ *
63
+ * @param obj Validation object
64
+ * @param key Key for getting value from object
65
+ */
66
+ protected checkDefault(obj: {
16
67
  [key: string]: any;
17
- }, key: string, rule: IValidationRule): void;
68
+ }, key: string): void;
69
+ /**
70
+ * Checks if the initialized property of an object
71
+ *
72
+ * @param obj Validation object
73
+ * @param key Key for getting value from object
74
+ */
75
+ protected checkRequire(obj: {
76
+ [key: string]: any;
77
+ }, key: string): void;
18
78
  }
@@ -16,36 +16,102 @@ class BasicType {
16
16
  default: undefined,
17
17
  rules: [],
18
18
  example: undefined,
19
- description: ''
19
+ description: '',
20
+ message: '',
20
21
  };
21
22
  }
22
23
  require() {
23
24
  this.rule.require = true;
24
25
  return this;
25
26
  }
27
+ /**
28
+ * Example of a valid value for this rule
29
+ *
30
+ * @param ex Example valid value
31
+ */
26
32
  example(ex) {
27
33
  this.rule.example = ex;
28
34
  return this;
29
35
  }
36
+ /**
37
+ * Description of the validated object property
38
+ *
39
+ * @param desc
40
+ */
30
41
  description(desc) {
31
42
  this.rule.description = desc;
32
43
  return this;
33
44
  }
45
+ /**
46
+ * Make message use message template
47
+ *
48
+ * If you want to change the default message, you can use a template in the message parameter
49
+ *
50
+ * @example
51
+ * ```js
52
+ * Rule.number().description('My number').message('{description} must by 1,2,3,4,5,6... not {value}')
53
+ * ```
54
+ * Use {description} {value} {default} {example} in template
55
+ */
56
+ message(mess) {
57
+ this.rule.message = mess;
58
+ return this;
59
+ }
60
+ /**
61
+ * Exporting a rule for use
62
+ * Typically used within VRack or VRack-Core
63
+ *
64
+ * !!! hide for external users !!!
65
+ * @private
66
+ */
34
67
  export() {
68
+ return JSON.parse(JSON.stringify(this.rule));
69
+ }
70
+ /**
71
+ * This method will be executed when converting a Rule object to JSON
72
+ */
73
+ toJSON() {
35
74
  return this.rule;
36
75
  }
37
- static validate(obj, key, rule) {
38
- BasicType.checkDefault(obj, key, rule);
39
- BasicType.checkRequire(obj, key, rule);
76
+ /**
77
+ * Performs general validation rules
78
+ *
79
+ * @param obj Validation object
80
+ * @param key Key for getting value from object
81
+ */
82
+ basicValidate(obj, key) {
83
+ this.checkDefault(obj, key);
84
+ this.checkRequire(obj, key);
85
+ return true;
86
+ }
87
+ /**
88
+ * Method of validation of this type
89
+ *
90
+ * @param obj Validation object
91
+ * @param key Key for getting value from object
92
+ */
93
+ validate(obj, key) {
40
94
  return true;
41
95
  }
42
- static checkDefault(obj, key, rule) {
43
- if (rule.default === undefined)
96
+ /**
97
+ * Sets the default value if it has not been set
98
+ *
99
+ * @param obj Validation object
100
+ * @param key Key for getting value from object
101
+ */
102
+ checkDefault(obj, key) {
103
+ if (this.rule.default === undefined)
44
104
  return;
45
105
  if (obj[key] === undefined)
46
- obj[key] = rule.default;
106
+ obj[key] = this.rule.default;
47
107
  }
48
- static checkRequire(obj, key, rule) {
108
+ /**
109
+ * Checks if the initialized property of an object
110
+ *
111
+ * @param obj Validation object
112
+ * @param key Key for getting value from object
113
+ */
114
+ checkRequire(obj, key) {
49
115
  if (obj[key] === undefined)
50
116
  throw ErrorManager_1.default.make('VR_ERROR_REQUAERED', { key });
51
117
  }
@@ -0,0 +1,23 @@
1
+ import BasicType from "./BasicType";
2
+ export default class BooleanType extends BasicType {
3
+ constructor();
4
+ /**
5
+ * Setting the default value
6
+ */
7
+ default(def: boolean): this;
8
+ /**
9
+ * Example of a valid value for this rule
10
+ *
11
+ * @param ex Example valid value
12
+ */
13
+ example(ex: boolean): this;
14
+ /**
15
+ * Method of validation of this type
16
+ *
17
+ * @param obj Validation object
18
+ * @param key Key for getting value from object
19
+ */
20
+ validate(obj: {
21
+ [key: string]: any;
22
+ }, key: string): boolean;
23
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright © 2022 Boris Bobylev. All rights reserved.
4
+ * Licensed under the Apache License, Version 2.0
5
+ */
6
+ var __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const BasicType_1 = __importDefault(require("./BasicType"));
11
+ const ErrorManager_1 = __importDefault(require("../../errors/ErrorManager"));
12
+ class BooleanType extends BasicType_1.default {
13
+ constructor() {
14
+ super();
15
+ this.rule.type = 'boolean';
16
+ }
17
+ /**
18
+ * Setting the default value
19
+ */
20
+ default(def) {
21
+ this.rule.default = def;
22
+ return this;
23
+ }
24
+ /**
25
+ * Example of a valid value for this rule
26
+ *
27
+ * @param ex Example valid value
28
+ */
29
+ example(ex) {
30
+ this.rule.example = ex;
31
+ return this;
32
+ }
33
+ /**
34
+ * Method of validation of this type
35
+ *
36
+ * @param obj Validation object
37
+ * @param key Key for getting value from object
38
+ */
39
+ validate(obj, key) {
40
+ this.basicValidate(obj, key);
41
+ if (typeof obj[key] !== 'boolean')
42
+ throw ErrorManager_1.default.make('VR_IS_NOT_BOOLEAN', { key });
43
+ return true;
44
+ }
45
+ }
46
+ exports.default = BooleanType;
47
+ ErrorManager_1.default.register('Validator', 'Fr7BvAlZyZPm', 'VR_IS_NOT_BOOLEAN', 'Value must be a number', {});
@@ -0,0 +1,17 @@
1
+ import BasicType from "./BasicType";
2
+ export default class FunctionType extends BasicType {
3
+ constructor();
4
+ /**
5
+ * Setting the default value
6
+ */
7
+ default(def: () => any): this;
8
+ /**
9
+ * Method of validation of this type
10
+ *
11
+ * @param obj Validation object
12
+ * @param key Key for getting value from object
13
+ */
14
+ validate(obj: {
15
+ [key: string]: any;
16
+ }, key: string): boolean;
17
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright © 2022 Boris Bobylev. All rights reserved.
4
+ * Licensed under the Apache License, Version 2.0
5
+ */
6
+ var __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ const BasicType_1 = __importDefault(require("./BasicType"));
11
+ const ErrorManager_1 = __importDefault(require("../../errors/ErrorManager"));
12
+ class FunctionType extends BasicType_1.default {
13
+ constructor() {
14
+ super();
15
+ this.rule.type = 'function';
16
+ }
17
+ /**
18
+ * Setting the default value
19
+ */
20
+ default(def) {
21
+ this.rule.default = def;
22
+ return this;
23
+ }
24
+ /**
25
+ * Method of validation of this type
26
+ *
27
+ * @param obj Validation object
28
+ * @param key Key for getting value from object
29
+ */
30
+ validate(obj, key) {
31
+ this.basicValidate(obj, key);
32
+ if (typeof obj[key] !== 'function')
33
+ throw ErrorManager_1.default.make('VR_IS_NOT_FUNCTION', { key });
34
+ return true;
35
+ }
36
+ }
37
+ exports.default = FunctionType;
38
+ ErrorManager_1.default.register('Validator', 'P2K7PE7C3JRU', 'VR_IS_NOT_FUNCTION', 'Value must be a function', {});