vscroll 1.6.3 → 1.7.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/dist/bundles/vscroll.esm5.js +5704 -5705
- package/dist/bundles/vscroll.esm5.js.map +1 -1
- package/dist/bundles/vscroll.esm5.min.js +2 -2
- package/dist/bundles/vscroll.esm5.min.js.map +1 -1
- package/dist/bundles/vscroll.esm6.js +5193 -5194
- package/dist/bundles/vscroll.esm6.js.map +1 -1
- package/dist/bundles/vscroll.esm6.min.js +2 -2
- package/dist/bundles/vscroll.esm6.min.js.map +1 -1
- package/dist/bundles/vscroll.umd.js +5712 -5714
- package/dist/bundles/vscroll.umd.js.map +1 -1
- package/dist/bundles/vscroll.umd.min.js +2 -2
- package/dist/bundles/vscroll.umd.min.js.map +1 -1
- package/dist/esm2015/classes/adapter/context.js +43 -43
- package/dist/esm2015/classes/adapter/context.js.map +1 -1
- package/dist/esm2015/classes/adapter/props.js +300 -300
- package/dist/esm2015/classes/adapter/wanted.js +28 -28
- package/dist/esm2015/classes/adapter/wanted.js.map +1 -1
- package/dist/esm2015/classes/adapter.js +487 -487
- package/dist/esm2015/classes/adapter.js.map +1 -1
- package/dist/esm2015/classes/buffer/cache.js +246 -246
- package/dist/esm2015/classes/buffer/cache.js.map +1 -1
- package/dist/esm2015/classes/buffer/checkCall.js +48 -48
- package/dist/esm2015/classes/buffer/checkCall.js.map +1 -1
- package/dist/esm2015/classes/buffer/defaultSize.js +127 -127
- package/dist/esm2015/classes/buffer/defaultSize.js.map +1 -1
- package/dist/esm2015/classes/buffer.js +342 -342
- package/dist/esm2015/classes/buffer.js.map +1 -1
- package/dist/esm2015/classes/datasource.js +23 -24
- package/dist/esm2015/classes/datasource.js.map +1 -1
- package/dist/esm2015/classes/domRoutines.js +160 -160
- package/dist/esm2015/classes/domRoutines.js.map +1 -1
- package/dist/esm2015/classes/item.js +61 -61
- package/dist/esm2015/classes/item.js.map +1 -1
- package/dist/esm2015/classes/logger.js +186 -186
- package/dist/esm2015/classes/logger.js.map +1 -1
- package/dist/esm2015/classes/paddings.js +72 -72
- package/dist/esm2015/classes/paddings.js.map +1 -1
- package/dist/esm2015/classes/reactive.js +54 -54
- package/dist/esm2015/classes/reactive.js.map +1 -1
- package/dist/esm2015/classes/settings.js +39 -39
- package/dist/esm2015/classes/settings.js.map +1 -1
- package/dist/esm2015/classes/state/clip.js +16 -16
- package/dist/esm2015/classes/state/clip.js.map +1 -1
- package/dist/esm2015/classes/state/cycle.js +60 -60
- package/dist/esm2015/classes/state/cycle.js.map +1 -1
- package/dist/esm2015/classes/state/fetch.js +122 -122
- package/dist/esm2015/classes/state/fetch.js.map +1 -1
- package/dist/esm2015/classes/state/render.js +14 -14
- package/dist/esm2015/classes/state/scroll.js +30 -30
- package/dist/esm2015/classes/state/scroll.js.map +1 -1
- package/dist/esm2015/classes/state.js +62 -62
- package/dist/esm2015/classes/state.js.map +1 -1
- package/dist/esm2015/classes/viewport.js +72 -72
- package/dist/esm2015/classes/viewport.js.map +1 -1
- package/dist/esm2015/index.js +9 -9
- package/dist/esm2015/inputs/adapter.js +242 -242
- package/dist/esm2015/inputs/common.js +11 -11
- package/dist/esm2015/inputs/datasource.js +20 -20
- package/dist/esm2015/inputs/index.js +7 -7
- package/dist/esm2015/inputs/settings.js +146 -146
- package/dist/esm2015/inputs/validation.js +371 -374
- package/dist/esm2015/inputs/validation.js.map +1 -1
- package/dist/esm2015/inputs/workflow.js +30 -30
- package/dist/esm2015/interfaces/adapter.js +1 -1
- package/dist/esm2015/interfaces/datasource.js +1 -1
- package/dist/esm2015/interfaces/index.js +1 -1
- package/dist/esm2015/interfaces/item.js +1 -1
- package/dist/esm2015/interfaces/process.js +1 -1
- package/dist/esm2015/interfaces/process.js.map +1 -1
- package/dist/esm2015/interfaces/routines.js +1 -1
- package/dist/esm2015/interfaces/settings.js +1 -1
- package/dist/esm2015/interfaces/state.js +1 -1
- package/dist/esm2015/interfaces/validation.js +1 -1
- package/dist/esm2015/interfaces/workflow.js +1 -1
- package/dist/esm2015/processes/adapter/append.js +36 -36
- package/dist/esm2015/processes/adapter/append.js.map +1 -1
- package/dist/esm2015/processes/adapter/check.js +32 -32
- package/dist/esm2015/processes/adapter/check.js.map +1 -1
- package/dist/esm2015/processes/adapter/clip.js +12 -12
- package/dist/esm2015/processes/adapter/fix.js +77 -77
- package/dist/esm2015/processes/adapter/fix.js.map +1 -1
- package/dist/esm2015/processes/adapter/insert.js +73 -73
- package/dist/esm2015/processes/adapter/insert.js.map +1 -1
- package/dist/esm2015/processes/adapter/pause.js +22 -22
- package/dist/esm2015/processes/adapter/pause.js.map +1 -1
- package/dist/esm2015/processes/adapter/reload.js +19 -19
- package/dist/esm2015/processes/adapter/reload.js.map +1 -1
- package/dist/esm2015/processes/adapter/remove.js +102 -102
- package/dist/esm2015/processes/adapter/remove.js.map +1 -1
- package/dist/esm2015/processes/adapter/replace.js +39 -39
- package/dist/esm2015/processes/adapter/replace.js.map +1 -1
- package/dist/esm2015/processes/adapter/reset.js +35 -35
- package/dist/esm2015/processes/adapter/reset.js.map +1 -1
- package/dist/esm2015/processes/adapter/update.js +43 -43
- package/dist/esm2015/processes/adapter/update.js.map +1 -1
- package/dist/esm2015/processes/adjust.js +141 -141
- package/dist/esm2015/processes/adjust.js.map +1 -1
- package/dist/esm2015/processes/clip.js +49 -49
- package/dist/esm2015/processes/clip.js.map +1 -1
- package/dist/esm2015/processes/end.js +52 -52
- package/dist/esm2015/processes/end.js.map +1 -1
- package/dist/esm2015/processes/fetch.js +91 -91
- package/dist/esm2015/processes/fetch.js.map +1 -1
- package/dist/esm2015/processes/index.js +24 -24
- package/dist/esm2015/processes/init.js +14 -14
- package/dist/esm2015/processes/misc/base.js +30 -30
- package/dist/esm2015/processes/misc/base.js.map +1 -1
- package/dist/esm2015/processes/misc/enums.js +36 -36
- package/dist/esm2015/processes/misc/index.js +2 -2
- package/dist/esm2015/processes/postFetch.js +65 -65
- package/dist/esm2015/processes/postFetch.js.map +1 -1
- package/dist/esm2015/processes/preClip.js +85 -85
- package/dist/esm2015/processes/preClip.js.map +1 -1
- package/dist/esm2015/processes/preFetch.js +240 -240
- package/dist/esm2015/processes/preFetch.js.map +1 -1
- package/dist/esm2015/processes/render.js +54 -54
- package/dist/esm2015/processes/render.js.map +1 -1
- package/dist/esm2015/processes/scroll.js +97 -97
- package/dist/esm2015/processes/scroll.js.map +1 -1
- package/dist/esm2015/processes/start.js +11 -11
- package/dist/esm2015/scroller.js +86 -86
- package/dist/esm2015/scroller.js.map +1 -1
- package/dist/esm2015/version.js +4 -4
- package/dist/esm2015/version.js.map +1 -1
- package/dist/esm2015/workflow-transducer.js +218 -218
- package/dist/esm2015/workflow-transducer.js.map +1 -1
- package/dist/esm2015/workflow.js +174 -174
- package/dist/esm2015/workflow.js.map +1 -1
- package/dist/esm5/classes/adapter/context.js +50 -50
- package/dist/esm5/classes/adapter/context.js.map +1 -1
- package/dist/esm5/classes/adapter/props.js +300 -300
- package/dist/esm5/classes/adapter/wanted.js +29 -29
- package/dist/esm5/classes/adapter/wanted.js.map +1 -1
- package/dist/esm5/classes/adapter.js +535 -535
- package/dist/esm5/classes/adapter.js.map +1 -1
- package/dist/esm5/classes/buffer/cache.js +260 -260
- package/dist/esm5/classes/buffer/cache.js.map +1 -1
- package/dist/esm5/classes/buffer/checkCall.js +56 -56
- package/dist/esm5/classes/buffer/checkCall.js.map +1 -1
- package/dist/esm5/classes/buffer/defaultSize.js +145 -145
- package/dist/esm5/classes/buffer/defaultSize.js.map +1 -1
- package/dist/esm5/classes/buffer.js +409 -409
- package/dist/esm5/classes/buffer.js.map +1 -1
- package/dist/esm5/classes/datasource.js +30 -31
- package/dist/esm5/classes/datasource.js.map +1 -1
- package/dist/esm5/classes/domRoutines.js +165 -165
- package/dist/esm5/classes/domRoutines.js.map +1 -1
- package/dist/esm5/classes/item.js +76 -76
- package/dist/esm5/classes/item.js.map +1 -1
- package/dist/esm5/classes/logger.js +210 -210
- package/dist/esm5/classes/logger.js.map +1 -1
- package/dist/esm5/classes/paddings.js +80 -80
- package/dist/esm5/classes/paddings.js.map +1 -1
- package/dist/esm5/classes/reactive.js +69 -69
- package/dist/esm5/classes/reactive.js.map +1 -1
- package/dist/esm5/classes/settings.js +47 -47
- package/dist/esm5/classes/settings.js.map +1 -1
- package/dist/esm5/classes/state/clip.js +22 -22
- package/dist/esm5/classes/state/clip.js.map +1 -1
- package/dist/esm5/classes/state/cycle.js +75 -75
- package/dist/esm5/classes/state/cycle.js.map +1 -1
- package/dist/esm5/classes/state/fetch.js +148 -148
- package/dist/esm5/classes/state/fetch.js.map +1 -1
- package/dist/esm5/classes/state/render.js +20 -20
- package/dist/esm5/classes/state/scroll.js +32 -32
- package/dist/esm5/classes/state/scroll.js.map +1 -1
- package/dist/esm5/classes/state.js +69 -69
- package/dist/esm5/classes/state.js.map +1 -1
- package/dist/esm5/classes/viewport.js +79 -79
- package/dist/esm5/classes/viewport.js.map +1 -1
- package/dist/esm5/index.js +9 -9
- package/dist/esm5/inputs/adapter.js +243 -243
- package/dist/esm5/inputs/common.js +11 -11
- package/dist/esm5/inputs/datasource.js +21 -21
- package/dist/esm5/inputs/index.js +7 -7
- package/dist/esm5/inputs/settings.js +147 -147
- package/dist/esm5/inputs/validation.js +393 -396
- package/dist/esm5/inputs/validation.js.map +1 -1
- package/dist/esm5/inputs/workflow.js +31 -31
- package/dist/esm5/interfaces/adapter.js +1 -1
- package/dist/esm5/interfaces/datasource.js +1 -1
- package/dist/esm5/interfaces/index.js +1 -1
- package/dist/esm5/interfaces/item.js +1 -1
- package/dist/esm5/interfaces/process.js +1 -1
- package/dist/esm5/interfaces/process.js.map +1 -1
- package/dist/esm5/interfaces/routines.js +1 -1
- package/dist/esm5/interfaces/settings.js +1 -1
- package/dist/esm5/interfaces/state.js +1 -1
- package/dist/esm5/interfaces/validation.js +1 -1
- package/dist/esm5/interfaces/workflow.js +1 -1
- package/dist/esm5/processes/adapter/append.js +44 -44
- package/dist/esm5/processes/adapter/append.js.map +1 -1
- package/dist/esm5/processes/adapter/check.js +39 -39
- package/dist/esm5/processes/adapter/check.js.map +1 -1
- package/dist/esm5/processes/adapter/clip.js +19 -19
- package/dist/esm5/processes/adapter/fix.js +89 -89
- package/dist/esm5/processes/adapter/fix.js.map +1 -1
- package/dist/esm5/processes/adapter/insert.js +81 -81
- package/dist/esm5/processes/adapter/insert.js.map +1 -1
- package/dist/esm5/processes/adapter/pause.js +29 -29
- package/dist/esm5/processes/adapter/pause.js.map +1 -1
- package/dist/esm5/processes/adapter/reload.js +26 -26
- package/dist/esm5/processes/adapter/reload.js.map +1 -1
- package/dist/esm5/processes/adapter/remove.js +115 -115
- package/dist/esm5/processes/adapter/remove.js.map +1 -1
- package/dist/esm5/processes/adapter/replace.js +47 -47
- package/dist/esm5/processes/adapter/replace.js.map +1 -1
- package/dist/esm5/processes/adapter/reset.js +42 -42
- package/dist/esm5/processes/adapter/reset.js.map +1 -1
- package/dist/esm5/processes/adapter/update.js +59 -59
- package/dist/esm5/processes/adapter/update.js.map +1 -1
- package/dist/esm5/processes/adjust.js +152 -152
- package/dist/esm5/processes/adjust.js.map +1 -1
- package/dist/esm5/processes/clip.js +60 -60
- package/dist/esm5/processes/clip.js.map +1 -1
- package/dist/esm5/processes/end.js +63 -63
- package/dist/esm5/processes/end.js.map +1 -1
- package/dist/esm5/processes/fetch.js +102 -102
- package/dist/esm5/processes/fetch.js.map +1 -1
- package/dist/esm5/processes/index.js +24 -24
- package/dist/esm5/processes/init.js +21 -21
- package/dist/esm5/processes/misc/base.js +44 -44
- package/dist/esm5/processes/misc/base.js.map +1 -1
- package/dist/esm5/processes/misc/enums.js +36 -36
- package/dist/esm5/processes/misc/index.js +2 -2
- package/dist/esm5/processes/postFetch.js +74 -74
- package/dist/esm5/processes/postFetch.js.map +1 -1
- package/dist/esm5/processes/preClip.js +94 -94
- package/dist/esm5/processes/preClip.js.map +1 -1
- package/dist/esm5/processes/preFetch.js +247 -247
- package/dist/esm5/processes/preFetch.js.map +1 -1
- package/dist/esm5/processes/render.js +63 -63
- package/dist/esm5/processes/render.js.map +1 -1
- package/dist/esm5/processes/scroll.js +107 -107
- package/dist/esm5/processes/scroll.js.map +1 -1
- package/dist/esm5/processes/start.js +18 -18
- package/dist/esm5/scroller.js +100 -100
- package/dist/esm5/scroller.js.map +1 -1
- package/dist/esm5/version.js +4 -4
- package/dist/esm5/version.js.map +1 -1
- package/dist/esm5/workflow-transducer.js +220 -220
- package/dist/esm5/workflow-transducer.js.map +1 -1
- package/dist/esm5/workflow.js +198 -198
- package/dist/esm5/workflow.js.map +1 -1
- package/dist/typings/classes/adapter/context.d.ts +4 -4
- package/dist/typings/classes/adapter/props.d.ts +52 -52
- package/dist/typings/classes/adapter/wanted.d.ts +14 -14
- package/dist/typings/classes/adapter.d.ts +79 -79
- package/dist/typings/classes/buffer/cache.d.ts +95 -95
- package/dist/typings/classes/buffer/checkCall.d.ts +12 -12
- package/dist/typings/classes/buffer/defaultSize.d.ts +32 -32
- package/dist/typings/classes/buffer.d.ts +64 -64
- package/dist/typings/classes/datasource.d.ts +11 -11
- package/dist/typings/classes/domRoutines.d.ts +36 -36
- package/dist/typings/classes/item.d.ts +28 -28
- package/dist/typings/classes/logger.d.ts +28 -28
- package/dist/typings/classes/paddings.d.ts +22 -22
- package/dist/typings/classes/reactive.d.ts +21 -21
- package/dist/typings/classes/settings.d.ts +119 -119
- package/dist/typings/classes/state/clip.d.ts +9 -9
- package/dist/typings/classes/state/cycle.d.ts +29 -29
- package/dist/typings/classes/state/fetch.d.ts +62 -62
- package/dist/typings/classes/state/render.d.ts +9 -9
- package/dist/typings/classes/state/scroll.d.ts +16 -16
- package/dist/typings/classes/state.d.ts +29 -29
- package/dist/typings/classes/viewport.d.ts +32 -32
- package/dist/typings/index.d.ts +10 -10
- package/dist/typings/inputs/adapter.d.ts +5 -5
- package/dist/typings/inputs/common.d.ts +9 -9
- package/dist/typings/inputs/datasource.d.ts +7 -7
- package/dist/typings/inputs/index.d.ts +7 -7
- package/dist/typings/inputs/settings.d.ts +41 -41
- package/dist/typings/inputs/validation.d.ts +82 -83
- package/dist/typings/inputs/workflow.d.ts +9 -9
- package/dist/typings/interfaces/adapter.d.ts +164 -164
- package/dist/typings/interfaces/datasource.d.ts +28 -28
- package/dist/typings/interfaces/index.d.ts +10 -10
- package/dist/typings/interfaces/item.d.ts +8 -8
- package/dist/typings/interfaces/process.d.ts +35 -35
- package/dist/typings/interfaces/routines.d.ts +166 -166
- package/dist/typings/interfaces/settings.d.ts +31 -31
- package/dist/typings/interfaces/state.d.ts +24 -24
- package/dist/typings/interfaces/validation.d.ts +33 -33
- package/dist/typings/interfaces/workflow.d.ts +53 -53
- package/dist/typings/processes/adapter/append.d.ts +14 -14
- package/dist/typings/processes/adapter/check.d.ts +6 -6
- package/dist/typings/processes/adapter/clip.d.ts +7 -7
- package/dist/typings/processes/adapter/fix.d.ts +13 -13
- package/dist/typings/processes/adapter/insert.d.ts +11 -11
- package/dist/typings/processes/adapter/pause.d.ts +8 -8
- package/dist/typings/processes/adapter/reload.d.ts +6 -6
- package/dist/typings/processes/adapter/remove.d.ts +11 -11
- package/dist/typings/processes/adapter/replace.d.ts +8 -8
- package/dist/typings/processes/adapter/reset.d.ts +7 -7
- package/dist/typings/processes/adapter/update.d.ts +8 -8
- package/dist/typings/processes/adjust.d.ts +10 -10
- package/dist/typings/processes/clip.d.ts +7 -7
- package/dist/typings/processes/end.d.ts +10 -10
- package/dist/typings/processes/fetch.d.ts +18 -18
- package/dist/typings/processes/index.d.ts +24 -24
- package/dist/typings/processes/init.d.ts +7 -7
- package/dist/typings/processes/misc/base.d.ts +4 -4
- package/dist/typings/processes/misc/enums.d.ts +33 -33
- package/dist/typings/processes/misc/index.d.ts +2 -2
- package/dist/typings/processes/postFetch.d.ts +8 -8
- package/dist/typings/processes/preClip.d.ts +12 -12
- package/dist/typings/processes/preFetch.d.ts +17 -17
- package/dist/typings/processes/render.d.ts +9 -9
- package/dist/typings/processes/scroll.d.ts +13 -13
- package/dist/typings/processes/start.d.ts +6 -6
- package/dist/typings/scroller.d.ts +26 -26
- package/dist/typings/version.d.ts +5 -5
- package/dist/typings/workflow-transducer.d.ts +2 -2
- package/dist/typings/workflow.d.ts +29 -29
- package/package.json +19 -18
|
@@ -1,375 +1,372 @@
|
|
|
1
|
-
export var ValidatorType;
|
|
2
|
-
(function (ValidatorType) {
|
|
3
|
-
ValidatorType["number"] = "must be a number";
|
|
4
|
-
ValidatorType["integer"] = "must be an integer";
|
|
5
|
-
ValidatorType["integerUnlimited"] = "must be an integer or infinity";
|
|
6
|
-
ValidatorType["moreOrEqual"] = "must be a number greater than (or equal to) {arg1}";
|
|
7
|
-
ValidatorType["itemList"] = "must be an array of items {arg1}";
|
|
8
|
-
ValidatorType["boolean"] = "must be a boolean";
|
|
9
|
-
ValidatorType["object"] = "must be an object";
|
|
10
|
-
ValidatorType["element"] = "must be an html element";
|
|
11
|
-
ValidatorType["function"] = "must be a function";
|
|
12
|
-
ValidatorType["funcOfxArguments"] = "must have {arg1} argument(s)";
|
|
13
|
-
ValidatorType["funcOfxAndMoreArguments"] = "must have at least {arg1} argument(s)";
|
|
14
|
-
ValidatorType["funcOfXToYArguments"] = "must have {arg1} to {arg2} arguments";
|
|
15
|
-
ValidatorType["oneOfCan"] = "can be present as only one item of {arg1} list";
|
|
16
|
-
ValidatorType["oneOfMust"] = "must be present as only one item of {arg1} list";
|
|
17
|
-
ValidatorType["or"] = "must satisfy at least 1 validator from {arg1} list";
|
|
18
|
-
ValidatorType["enum"] = "must belong to {arg1} list";
|
|
19
|
-
})(ValidatorType || (ValidatorType = {}));
|
|
20
|
-
const getError = (msg, args) => (args || ['']).reduce((acc, arg, index) => acc.replace(`{arg${index + 1}}`, arg), msg);
|
|
21
|
-
const getNumber = (value) => typeof value === 'number' || (typeof value === 'string' && value !== '')
|
|
22
|
-
? Number(value)
|
|
23
|
-
: NaN;
|
|
24
|
-
const onNumber = (value) => {
|
|
25
|
-
const parsedValue = getNumber(value);
|
|
26
|
-
const errors = [];
|
|
27
|
-
if (Number.isNaN(parsedValue)) {
|
|
28
|
-
errors.push(ValidatorType.number);
|
|
29
|
-
}
|
|
30
|
-
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
31
|
-
};
|
|
32
|
-
const onInteger = (value) => {
|
|
33
|
-
const errors = [];
|
|
34
|
-
value = getNumber(value);
|
|
35
|
-
const parsedValue = parseInt(String(value), 10);
|
|
36
|
-
if (value !== parsedValue) {
|
|
37
|
-
errors.push(ValidatorType.integer);
|
|
38
|
-
}
|
|
39
|
-
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
40
|
-
};
|
|
41
|
-
const onIntegerUnlimited = (value) => {
|
|
42
|
-
let parsedValue = value;
|
|
43
|
-
const errors = [];
|
|
44
|
-
value = getNumber(value);
|
|
45
|
-
if (!Number.isFinite(value)) {
|
|
46
|
-
parsedValue = value;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
parsedValue = parseInt(String(value), 10);
|
|
50
|
-
}
|
|
51
|
-
if (value !== parsedValue) {
|
|
52
|
-
errors.push(ValidatorType.integerUnlimited);
|
|
53
|
-
}
|
|
54
|
-
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
55
|
-
};
|
|
56
|
-
const onMoreOrEqual = (limit, fallback) => (value) => {
|
|
57
|
-
const result = onNumber(value);
|
|
58
|
-
if (!result.isValid) {
|
|
59
|
-
return result;
|
|
60
|
-
}
|
|
61
|
-
let parsedValue = result.value;
|
|
62
|
-
const errors = [];
|
|
63
|
-
if (parsedValue < limit) {
|
|
64
|
-
if (!fallback) {
|
|
65
|
-
errors.push(getError(ValidatorType.moreOrEqual, [String(limit)]));
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
parsedValue = limit;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
72
|
-
};
|
|
73
|
-
const onBoolean = (value) => {
|
|
74
|
-
const errors = [];
|
|
75
|
-
let parsedValue = value;
|
|
76
|
-
if (value === 'true') {
|
|
77
|
-
parsedValue = true;
|
|
78
|
-
}
|
|
79
|
-
else if (value === 'false') {
|
|
80
|
-
parsedValue = false;
|
|
81
|
-
}
|
|
82
|
-
if (typeof parsedValue !== 'boolean') {
|
|
83
|
-
errors.push(ValidatorType.boolean);
|
|
84
|
-
}
|
|
85
|
-
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
86
|
-
};
|
|
87
|
-
const onObject = (value) => {
|
|
88
|
-
const errors = [];
|
|
89
|
-
if (!value || Object.prototype.toString.call(value) !== '[object Object]') {
|
|
90
|
-
errors.push(ValidatorType.object);
|
|
91
|
-
}
|
|
92
|
-
return { value, isSet: true, isValid: !errors.length, errors };
|
|
93
|
-
};
|
|
94
|
-
const onHtmlElement = (value) => {
|
|
95
|
-
const errors = [];
|
|
96
|
-
if (!(value instanceof Element) && !(value instanceof Document)) {
|
|
97
|
-
errors.push(ValidatorType.element);
|
|
98
|
-
}
|
|
99
|
-
return { value, isSet: true, isValid: !errors.length, errors };
|
|
100
|
-
};
|
|
101
|
-
const onItemList = (value) => {
|
|
102
|
-
let parsedValue = value;
|
|
103
|
-
const errors = [];
|
|
104
|
-
if (!Array.isArray(value)) {
|
|
105
|
-
errors.push(ValidatorType.itemList);
|
|
106
|
-
parsedValue = [];
|
|
107
|
-
}
|
|
108
|
-
else if (!value.length) {
|
|
109
|
-
errors.push(getError(ValidatorType.itemList, ['with at least 1 item']));
|
|
110
|
-
}
|
|
111
|
-
else if (value.length > 1) {
|
|
112
|
-
const type = typeof value[0];
|
|
113
|
-
for (let i = value.length - 1; i >= 0; i--) {
|
|
114
|
-
if (typeof value[i] !== type) {
|
|
115
|
-
errors.push(getError(ValidatorType.itemList, ['of items of the same type']));
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
121
|
-
};
|
|
122
|
-
const onFunction = (value) => {
|
|
123
|
-
const errors = [];
|
|
124
|
-
if (typeof value !== 'function') {
|
|
125
|
-
errors.push(ValidatorType.function);
|
|
126
|
-
}
|
|
127
|
-
return { value: value, isSet: true, isValid: !errors.length, errors };
|
|
128
|
-
};
|
|
129
|
-
const onFunctionWithXArguments = (argsCount) => (value) => {
|
|
130
|
-
const result = onFunction(value);
|
|
131
|
-
if (!result.isValid) {
|
|
132
|
-
return result;
|
|
133
|
-
}
|
|
134
|
-
value = result.value;
|
|
135
|
-
const errors = [];
|
|
136
|
-
if (value.length !== argsCount) {
|
|
137
|
-
errors.push(getError(ValidatorType.funcOfxArguments, [String(argsCount)]));
|
|
138
|
-
}
|
|
139
|
-
return { value: value, isSet: true, isValid: !errors.length, errors };
|
|
140
|
-
};
|
|
141
|
-
const onFunctionWithXAndMoreArguments = (argsCount) => (value) => {
|
|
142
|
-
const result = onFunction(value);
|
|
143
|
-
if (!result.isValid) {
|
|
144
|
-
return result;
|
|
145
|
-
}
|
|
146
|
-
value = result.value;
|
|
147
|
-
const errors = [];
|
|
148
|
-
if (value.length < argsCount) {
|
|
149
|
-
errors.push(getError(ValidatorType.funcOfxArguments, [String(argsCount)]));
|
|
150
|
-
}
|
|
151
|
-
return { value: value, isSet: true, isValid: !errors.length, errors };
|
|
152
|
-
};
|
|
153
|
-
const onFunctionWithXToYArguments = (from, to) => (value) => {
|
|
154
|
-
const result = onFunction(value);
|
|
155
|
-
if (!result.isValid) {
|
|
156
|
-
return result;
|
|
157
|
-
}
|
|
158
|
-
value = result.value;
|
|
159
|
-
const errors = [];
|
|
160
|
-
if (value.length < from || value.length > to) {
|
|
161
|
-
errors.push(getError(ValidatorType.funcOfXToYArguments, [String(from), String(to)]));
|
|
162
|
-
}
|
|
163
|
-
return { value: value, isSet: true, isValid: !errors.length, errors };
|
|
164
|
-
};
|
|
165
|
-
const onOneOf = (tokens, must) => (value, context) => {
|
|
166
|
-
const errors = [];
|
|
167
|
-
const isSet = value !== void 0;
|
|
168
|
-
let noOneIsPresent = !isSet;
|
|
169
|
-
const err = must ? ValidatorType.oneOfMust : ValidatorType.oneOfCan;
|
|
170
|
-
if (!Array.isArray(tokens) || !tokens.length) {
|
|
171
|
-
errors.push(getError(err, ['undefined']));
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
for (let i = tokens.length - 1; i >= 0; i--) {
|
|
175
|
-
const token = tokens[i];
|
|
176
|
-
if (typeof token !== 'string') {
|
|
177
|
-
errors.push(getError(err, [tokens.join('", "')]) + ' (non-string token)');
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
const isAnotherPresent = context && Object.prototype.hasOwnProperty.call(context, token);
|
|
181
|
-
if (isSet && isAnotherPresent) {
|
|
182
|
-
errors.push(getError(err, [tokens.join('", "')]) + ` (${token} is present)`);
|
|
183
|
-
break;
|
|
184
|
-
}
|
|
185
|
-
if (noOneIsPresent && isAnotherPresent) {
|
|
186
|
-
noOneIsPresent = false;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
if (must && noOneIsPresent) {
|
|
190
|
-
errors.push(getError(err, [tokens.join('", "')]));
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return { value, isSet, isValid: !errors.length, errors };
|
|
194
|
-
};
|
|
195
|
-
const onOr = (validators) => (value) => {
|
|
196
|
-
const errors = [];
|
|
197
|
-
if (validators.every(validator => !validator.method(value).isValid)) {
|
|
198
|
-
errors.push(validators.map(v => v.type).join(' OR '));
|
|
199
|
-
}
|
|
200
|
-
return { value, isSet: true, isValid: !errors.length, errors };
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
this.
|
|
282
|
-
this.
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
this.isValidContext =
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
this.
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
this.
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
this.
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
this.
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
: getDefault(void 0, prop)));
|
|
373
|
-
return data;
|
|
374
|
-
};
|
|
1
|
+
export var ValidatorType;
|
|
2
|
+
(function (ValidatorType) {
|
|
3
|
+
ValidatorType["number"] = "must be a number";
|
|
4
|
+
ValidatorType["integer"] = "must be an integer";
|
|
5
|
+
ValidatorType["integerUnlimited"] = "must be an integer or infinity";
|
|
6
|
+
ValidatorType["moreOrEqual"] = "must be a number greater than (or equal to) {arg1}";
|
|
7
|
+
ValidatorType["itemList"] = "must be an array of items {arg1}";
|
|
8
|
+
ValidatorType["boolean"] = "must be a boolean";
|
|
9
|
+
ValidatorType["object"] = "must be an object";
|
|
10
|
+
ValidatorType["element"] = "must be an html element";
|
|
11
|
+
ValidatorType["function"] = "must be a function";
|
|
12
|
+
ValidatorType["funcOfxArguments"] = "must have {arg1} argument(s)";
|
|
13
|
+
ValidatorType["funcOfxAndMoreArguments"] = "must have at least {arg1} argument(s)";
|
|
14
|
+
ValidatorType["funcOfXToYArguments"] = "must have {arg1} to {arg2} arguments";
|
|
15
|
+
ValidatorType["oneOfCan"] = "can be present as only one item of {arg1} list";
|
|
16
|
+
ValidatorType["oneOfMust"] = "must be present as only one item of {arg1} list";
|
|
17
|
+
ValidatorType["or"] = "must satisfy at least 1 validator from {arg1} list";
|
|
18
|
+
ValidatorType["enum"] = "must belong to {arg1} list";
|
|
19
|
+
})(ValidatorType || (ValidatorType = {}));
|
|
20
|
+
const getError = (msg, args) => (args || ['']).reduce((acc, arg, index) => acc.replace(`{arg${index + 1}}`, arg), msg);
|
|
21
|
+
const getNumber = (value) => typeof value === 'number' || (typeof value === 'string' && value !== '')
|
|
22
|
+
? Number(value)
|
|
23
|
+
: NaN;
|
|
24
|
+
const onNumber = (value) => {
|
|
25
|
+
const parsedValue = getNumber(value);
|
|
26
|
+
const errors = [];
|
|
27
|
+
if (Number.isNaN(parsedValue)) {
|
|
28
|
+
errors.push(ValidatorType.number);
|
|
29
|
+
}
|
|
30
|
+
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
31
|
+
};
|
|
32
|
+
const onInteger = (value) => {
|
|
33
|
+
const errors = [];
|
|
34
|
+
value = getNumber(value);
|
|
35
|
+
const parsedValue = parseInt(String(value), 10);
|
|
36
|
+
if (value !== parsedValue) {
|
|
37
|
+
errors.push(ValidatorType.integer);
|
|
38
|
+
}
|
|
39
|
+
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
40
|
+
};
|
|
41
|
+
const onIntegerUnlimited = (value) => {
|
|
42
|
+
let parsedValue = value;
|
|
43
|
+
const errors = [];
|
|
44
|
+
value = getNumber(value);
|
|
45
|
+
if (!Number.isFinite(value)) {
|
|
46
|
+
parsedValue = value;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
parsedValue = parseInt(String(value), 10);
|
|
50
|
+
}
|
|
51
|
+
if (value !== parsedValue) {
|
|
52
|
+
errors.push(ValidatorType.integerUnlimited);
|
|
53
|
+
}
|
|
54
|
+
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
55
|
+
};
|
|
56
|
+
const onMoreOrEqual = (limit, fallback) => (value) => {
|
|
57
|
+
const result = onNumber(value);
|
|
58
|
+
if (!result.isValid) {
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
let parsedValue = result.value;
|
|
62
|
+
const errors = [];
|
|
63
|
+
if (parsedValue < limit) {
|
|
64
|
+
if (!fallback) {
|
|
65
|
+
errors.push(getError(ValidatorType.moreOrEqual, [String(limit)]));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
parsedValue = limit;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
72
|
+
};
|
|
73
|
+
const onBoolean = (value) => {
|
|
74
|
+
const errors = [];
|
|
75
|
+
let parsedValue = value;
|
|
76
|
+
if (value === 'true') {
|
|
77
|
+
parsedValue = true;
|
|
78
|
+
}
|
|
79
|
+
else if (value === 'false') {
|
|
80
|
+
parsedValue = false;
|
|
81
|
+
}
|
|
82
|
+
if (typeof parsedValue !== 'boolean') {
|
|
83
|
+
errors.push(ValidatorType.boolean);
|
|
84
|
+
}
|
|
85
|
+
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
86
|
+
};
|
|
87
|
+
const onObject = (value) => {
|
|
88
|
+
const errors = [];
|
|
89
|
+
if (!value || Object.prototype.toString.call(value) !== '[object Object]') {
|
|
90
|
+
errors.push(ValidatorType.object);
|
|
91
|
+
}
|
|
92
|
+
return { value, isSet: true, isValid: !errors.length, errors };
|
|
93
|
+
};
|
|
94
|
+
const onHtmlElement = (value) => {
|
|
95
|
+
const errors = [];
|
|
96
|
+
if (!(value instanceof Element) && !(value instanceof Document)) {
|
|
97
|
+
errors.push(ValidatorType.element);
|
|
98
|
+
}
|
|
99
|
+
return { value, isSet: true, isValid: !errors.length, errors };
|
|
100
|
+
};
|
|
101
|
+
const onItemList = (value) => {
|
|
102
|
+
let parsedValue = value;
|
|
103
|
+
const errors = [];
|
|
104
|
+
if (!Array.isArray(value)) {
|
|
105
|
+
errors.push(ValidatorType.itemList);
|
|
106
|
+
parsedValue = [];
|
|
107
|
+
}
|
|
108
|
+
else if (!value.length) {
|
|
109
|
+
errors.push(getError(ValidatorType.itemList, ['with at least 1 item']));
|
|
110
|
+
}
|
|
111
|
+
else if (value.length > 1) {
|
|
112
|
+
const type = typeof value[0];
|
|
113
|
+
for (let i = value.length - 1; i >= 0; i--) {
|
|
114
|
+
if (typeof value[i] !== type) {
|
|
115
|
+
errors.push(getError(ValidatorType.itemList, ['of items of the same type']));
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
|
|
121
|
+
};
|
|
122
|
+
const onFunction = (value) => {
|
|
123
|
+
const errors = [];
|
|
124
|
+
if (typeof value !== 'function') {
|
|
125
|
+
errors.push(ValidatorType.function);
|
|
126
|
+
}
|
|
127
|
+
return { value: value, isSet: true, isValid: !errors.length, errors };
|
|
128
|
+
};
|
|
129
|
+
const onFunctionWithXArguments = (argsCount) => (value) => {
|
|
130
|
+
const result = onFunction(value);
|
|
131
|
+
if (!result.isValid) {
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
value = result.value;
|
|
135
|
+
const errors = [];
|
|
136
|
+
if (value.length !== argsCount) {
|
|
137
|
+
errors.push(getError(ValidatorType.funcOfxArguments, [String(argsCount)]));
|
|
138
|
+
}
|
|
139
|
+
return { value: value, isSet: true, isValid: !errors.length, errors };
|
|
140
|
+
};
|
|
141
|
+
const onFunctionWithXAndMoreArguments = (argsCount) => (value) => {
|
|
142
|
+
const result = onFunction(value);
|
|
143
|
+
if (!result.isValid) {
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
value = result.value;
|
|
147
|
+
const errors = [];
|
|
148
|
+
if (value.length < argsCount) {
|
|
149
|
+
errors.push(getError(ValidatorType.funcOfxArguments, [String(argsCount)]));
|
|
150
|
+
}
|
|
151
|
+
return { value: value, isSet: true, isValid: !errors.length, errors };
|
|
152
|
+
};
|
|
153
|
+
const onFunctionWithXToYArguments = (from, to) => (value) => {
|
|
154
|
+
const result = onFunction(value);
|
|
155
|
+
if (!result.isValid) {
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
value = result.value;
|
|
159
|
+
const errors = [];
|
|
160
|
+
if (value.length < from || value.length > to) {
|
|
161
|
+
errors.push(getError(ValidatorType.funcOfXToYArguments, [String(from), String(to)]));
|
|
162
|
+
}
|
|
163
|
+
return { value: value, isSet: true, isValid: !errors.length, errors };
|
|
164
|
+
};
|
|
165
|
+
const onOneOf = (tokens, must) => (value, context) => {
|
|
166
|
+
const errors = [];
|
|
167
|
+
const isSet = value !== void 0;
|
|
168
|
+
let noOneIsPresent = !isSet;
|
|
169
|
+
const err = must ? ValidatorType.oneOfMust : ValidatorType.oneOfCan;
|
|
170
|
+
if (!Array.isArray(tokens) || !tokens.length) {
|
|
171
|
+
errors.push(getError(err, ['undefined']));
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
for (let i = tokens.length - 1; i >= 0; i--) {
|
|
175
|
+
const token = tokens[i];
|
|
176
|
+
if (typeof token !== 'string') {
|
|
177
|
+
errors.push(getError(err, [tokens.join('", "')]) + ' (non-string token)');
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
const isAnotherPresent = context && Object.prototype.hasOwnProperty.call(context, token);
|
|
181
|
+
if (isSet && isAnotherPresent) {
|
|
182
|
+
errors.push(getError(err, [tokens.join('", "')]) + ` (${token} is present)`);
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
if (noOneIsPresent && isAnotherPresent) {
|
|
186
|
+
noOneIsPresent = false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (must && noOneIsPresent) {
|
|
190
|
+
errors.push(getError(err, [tokens.join('", "')]));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return { value, isSet, isValid: !errors.length, errors };
|
|
194
|
+
};
|
|
195
|
+
const onOr = (validators) => (value) => {
|
|
196
|
+
const errors = [];
|
|
197
|
+
if (validators.every(validator => !validator.method(value).isValid)) {
|
|
198
|
+
errors.push(validators.map(v => v.type).join(' OR '));
|
|
199
|
+
}
|
|
200
|
+
return { value, isSet: true, isValid: !errors.length, errors };
|
|
201
|
+
};
|
|
202
|
+
const onEnum = (list) => (value) => {
|
|
203
|
+
const errors = [];
|
|
204
|
+
const values = Object.keys(list).filter(k => isNaN(Number(k))).map(k => list[k]);
|
|
205
|
+
if (!values.some(item => item === value)) {
|
|
206
|
+
errors.push(getError(ValidatorType.enum, ['[' + values.join(',') + ']']));
|
|
207
|
+
}
|
|
208
|
+
return { value, isSet: true, isValid: !errors.length, errors };
|
|
209
|
+
};
|
|
210
|
+
export const VALIDATORS = {
|
|
211
|
+
NUMBER: {
|
|
212
|
+
type: ValidatorType.number,
|
|
213
|
+
method: onNumber
|
|
214
|
+
},
|
|
215
|
+
INTEGER: {
|
|
216
|
+
type: ValidatorType.integer,
|
|
217
|
+
method: onInteger
|
|
218
|
+
},
|
|
219
|
+
INTEGER_UNLIMITED: {
|
|
220
|
+
type: ValidatorType.integerUnlimited,
|
|
221
|
+
method: onIntegerUnlimited
|
|
222
|
+
},
|
|
223
|
+
MORE_OR_EQUAL: (limit, fallback) => ({
|
|
224
|
+
type: ValidatorType.moreOrEqual,
|
|
225
|
+
method: onMoreOrEqual(limit, fallback)
|
|
226
|
+
}),
|
|
227
|
+
BOOLEAN: {
|
|
228
|
+
type: ValidatorType.boolean,
|
|
229
|
+
method: onBoolean
|
|
230
|
+
},
|
|
231
|
+
OBJECT: {
|
|
232
|
+
type: ValidatorType.object,
|
|
233
|
+
method: onObject
|
|
234
|
+
},
|
|
235
|
+
ITEM_LIST: {
|
|
236
|
+
type: ValidatorType.itemList,
|
|
237
|
+
method: onItemList
|
|
238
|
+
},
|
|
239
|
+
ELEMENT: {
|
|
240
|
+
type: ValidatorType.element,
|
|
241
|
+
method: onHtmlElement
|
|
242
|
+
},
|
|
243
|
+
FUNC: {
|
|
244
|
+
type: ValidatorType.function,
|
|
245
|
+
method: onFunction
|
|
246
|
+
},
|
|
247
|
+
FUNC_WITH_X_ARGUMENTS: (count) => ({
|
|
248
|
+
type: ValidatorType.funcOfxArguments,
|
|
249
|
+
method: onFunctionWithXArguments(count)
|
|
250
|
+
}),
|
|
251
|
+
FUNC_WITH_X_AND_MORE_ARGUMENTS: (count) => ({
|
|
252
|
+
type: ValidatorType.funcOfxAndMoreArguments,
|
|
253
|
+
method: onFunctionWithXAndMoreArguments(count)
|
|
254
|
+
}),
|
|
255
|
+
FUNC_WITH_X_TO_Y_ARGUMENTS: (from, to) => ({
|
|
256
|
+
type: ValidatorType.funcOfXToYArguments,
|
|
257
|
+
method: onFunctionWithXToYArguments(from, to)
|
|
258
|
+
}),
|
|
259
|
+
ONE_OF_CAN: (list) => ({
|
|
260
|
+
type: ValidatorType.oneOfCan,
|
|
261
|
+
method: onOneOf(list, false)
|
|
262
|
+
}),
|
|
263
|
+
ONE_OF_MUST: (list) => ({
|
|
264
|
+
type: ValidatorType.oneOfMust,
|
|
265
|
+
method: onOneOf(list, true)
|
|
266
|
+
}),
|
|
267
|
+
OR: (list) => ({
|
|
268
|
+
type: ValidatorType.or,
|
|
269
|
+
method: onOr(list)
|
|
270
|
+
}),
|
|
271
|
+
ENUM: (list) => ({
|
|
272
|
+
type: ValidatorType.enum,
|
|
273
|
+
method: onEnum(list)
|
|
274
|
+
})
|
|
275
|
+
};
|
|
276
|
+
export class ValidatedData {
|
|
277
|
+
constructor(context) {
|
|
278
|
+
this.params = {};
|
|
279
|
+
this.contextErrors = [];
|
|
280
|
+
this.errors = [];
|
|
281
|
+
this.isValid = true;
|
|
282
|
+
this.setContext(context);
|
|
283
|
+
}
|
|
284
|
+
setContext(context) {
|
|
285
|
+
if (!context || Object.prototype.toString.call(context) !== '[object Object]') {
|
|
286
|
+
this.setCommonError('context is not an object');
|
|
287
|
+
this.isValidContext = false;
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
this.isValidContext = true;
|
|
291
|
+
}
|
|
292
|
+
this.context = context;
|
|
293
|
+
}
|
|
294
|
+
setValidity() {
|
|
295
|
+
this.errors = Object.keys(this.params).reduce((acc, key) => [
|
|
296
|
+
...acc, ...this.params[key].errors
|
|
297
|
+
], []);
|
|
298
|
+
this.isValid = !this.errors.length;
|
|
299
|
+
}
|
|
300
|
+
setCommonError(error) {
|
|
301
|
+
this.contextErrors.push(error);
|
|
302
|
+
this.errors.push(error);
|
|
303
|
+
this.isValid = false;
|
|
304
|
+
}
|
|
305
|
+
setParam(token, value) {
|
|
306
|
+
if (!value.isValid) {
|
|
307
|
+
value.errors = !value.isSet
|
|
308
|
+
? [`"${token}" must be set`]
|
|
309
|
+
: value.errors.map((err) => `"${token}" ${err}`);
|
|
310
|
+
}
|
|
311
|
+
this.params[token] = value;
|
|
312
|
+
this.setValidity();
|
|
313
|
+
}
|
|
314
|
+
showErrors() {
|
|
315
|
+
return this.errors.length
|
|
316
|
+
? 'validation failed: ' + this.errors.join(', ')
|
|
317
|
+
: '';
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
export const runValidator = (current, validator, context) => {
|
|
321
|
+
const { value, errors } = current;
|
|
322
|
+
const result = validator.method(value, context);
|
|
323
|
+
const _errors = [...errors, ...result.errors];
|
|
324
|
+
return {
|
|
325
|
+
value: result.value,
|
|
326
|
+
isSet: result.isSet,
|
|
327
|
+
isValid: !_errors.length,
|
|
328
|
+
errors: _errors
|
|
329
|
+
};
|
|
330
|
+
};
|
|
331
|
+
const getDefault = (value, prop) => {
|
|
332
|
+
const empty = value === void 0;
|
|
333
|
+
const auto = !prop.mandatory && prop.defaultValue !== void 0;
|
|
334
|
+
return {
|
|
335
|
+
value: !empty ? value : (auto ? prop.defaultValue : void 0),
|
|
336
|
+
isSet: !empty || auto,
|
|
337
|
+
isValid: !empty || !prop.mandatory,
|
|
338
|
+
errors: []
|
|
339
|
+
};
|
|
340
|
+
};
|
|
341
|
+
export const validateOne = (context, name, prop) => {
|
|
342
|
+
const result = getDefault(context[name], prop);
|
|
343
|
+
if (!result.isSet) {
|
|
344
|
+
const oneOfMust = prop.validators.find(v => v.type === ValidatorType.oneOfMust);
|
|
345
|
+
if (oneOfMust) {
|
|
346
|
+
return runValidator(result, oneOfMust, context);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
for (const validator of Object.values(prop.validators)) {
|
|
351
|
+
const current = runValidator(result, validator, context);
|
|
352
|
+
if (!current.isValid && prop.defaultValue !== void 0) {
|
|
353
|
+
return {
|
|
354
|
+
value: prop.defaultValue,
|
|
355
|
+
isSet: true,
|
|
356
|
+
isValid: true,
|
|
357
|
+
errors: []
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
Object.assign(result, current);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return result;
|
|
364
|
+
};
|
|
365
|
+
export const validate = (context, params) => {
|
|
366
|
+
const data = new ValidatedData(context);
|
|
367
|
+
Object.entries(params).forEach(([key, prop]) => data.setParam(key, data.isValidContext
|
|
368
|
+
? validateOne(data.context, key, prop)
|
|
369
|
+
: getDefault(void 0, prop)));
|
|
370
|
+
return data;
|
|
371
|
+
};
|
|
375
372
|
//# sourceMappingURL=validation.js.map
|