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