@zajno/common 1.3.11 → 1.3.15
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/coverage/clover.xml +35 -32
- package/coverage/coverage-final.json +4 -4
- package/coverage/lcov-report/index.html +26 -26
- package/coverage/lcov-report/src/__tests__/helpers/index.html +1 -1
- package/coverage/lcov-report/src/__tests__/helpers/main.ts.html +1 -1
- package/coverage/lcov-report/src/async/arrays.ts.html +1 -1
- package/coverage/lcov-report/src/async/index.html +1 -1
- package/coverage/lcov-report/src/dates/calc.ts.html +1 -1
- package/coverage/lcov-report/src/dates/convert.ts.html +1 -1
- package/coverage/lcov-report/src/dates/datex.ts.html +1 -1
- package/coverage/lcov-report/src/dates/format.ts.html +1 -1
- package/coverage/lcov-report/src/dates/index.html +1 -1
- package/coverage/lcov-report/src/dates/index.ts.html +1 -1
- package/coverage/lcov-report/src/dates/parse.ts.html +1 -1
- package/coverage/lcov-report/src/dates/period.ts.html +1 -1
- package/coverage/lcov-report/src/dates/shift.ts.html +1 -1
- package/coverage/lcov-report/src/dates/types.ts.html +1 -1
- package/coverage/lcov-report/src/dates/yearDate.ts.html +1 -1
- package/coverage/lcov-report/src/enumHelper.ts.html +1 -1
- package/coverage/lcov-report/src/event.ts.html +1 -1
- package/coverage/lcov-report/src/fields/index.html +1 -1
- package/coverage/lcov-report/src/fields/update.ts.html +1 -1
- package/coverage/lcov-report/src/functions.ts.html +1 -1
- package/coverage/lcov-report/src/index.html +15 -15
- package/coverage/lcov-report/src/lazy.light.ts.html +33 -21
- package/coverage/lcov-report/src/logger/batch.ts.html +1 -1
- package/coverage/lcov-report/src/logger/console.ts.html +1 -1
- package/coverage/lcov-report/src/logger/index.html +1 -1
- package/coverage/lcov-report/src/logger/index.ts.html +1 -1
- package/coverage/lcov-report/src/logger/named.ts.html +1 -1
- package/coverage/lcov-report/src/logger/proxy.ts.html +1 -1
- package/coverage/lcov-report/src/math/arrays.ts.html +1 -1
- package/coverage/lcov-report/src/math/calc.ts.html +1 -1
- package/coverage/lcov-report/src/math/distribution.ts.html +1 -1
- package/coverage/lcov-report/src/math/index.html +1 -1
- package/coverage/lcov-report/src/math/index.ts.html +1 -1
- package/coverage/lcov-report/src/transitionObserver.ts.html +1 -1
- package/coverage/lcov-report/src/types.ts.html +1 -1
- package/coverage/lcov-report/src/validation/ValidationErrors.ts.html +1 -1
- package/coverage/lcov-report/src/validation/creditCard.ts.html +1 -1
- package/coverage/lcov-report/src/validation/helpers.ts.html +1 -1
- package/coverage/lcov-report/src/validation/index.html +1 -1
- package/coverage/lcov-report/src/validation/index.ts.html +1 -1
- package/coverage/lcov-report/src/validation/types.ts.html +6 -6
- package/coverage/lcov-report/src/validation/validators.ts.html +1 -1
- package/coverage/lcov-report/src/validation/wrappers.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/FlagModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/MultiSelectModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/NumberModel.ts.html +1 -1
- package/coverage/lcov-report/src/viewModels/SelectModel.ts.html +10 -10
- package/coverage/lcov-report/src/viewModels/Validatable.ts.html +44 -44
- package/coverage/lcov-report/src/viewModels/index.html +28 -28
- package/coverage/lcov-report/src/viewModels/wrappers.ts.html +1 -1
- package/coverage/lcov.info +62 -57
- package/lib/disposer.d.ts.map +1 -1
- package/lib/disposer.js +3 -1
- package/lib/disposer.js.map +1 -1
- package/lib/lazy.d.ts +6 -2
- package/lib/lazy.d.ts.map +1 -1
- package/lib/lazy.js +22 -12
- package/lib/lazy.js.map +1 -1
- package/lib/lazy.light.d.ts +2 -1
- package/lib/lazy.light.d.ts.map +1 -1
- package/lib/lazy.light.js +11 -9
- package/lib/lazy.light.js.map +1 -1
- package/lib/pool.d.ts +12 -4
- package/lib/pool.d.ts.map +1 -1
- package/lib/pool.js +28 -5
- package/lib/pool.js.map +1 -1
- package/lib/validation/types.d.ts +5 -5
- package/lib/validation/types.d.ts.map +1 -1
- package/lib/validation/types.js.map +1 -1
- package/lib/validation/wrappers.d.ts +4 -3
- package/lib/validation/wrappers.d.ts.map +1 -1
- package/lib/viewModels/TextModel.d.ts +2 -3
- package/lib/viewModels/TextModel.d.ts.map +1 -1
- package/lib/viewModels/TextModel.js +1 -1
- package/lib/viewModels/TextModel.js.map +1 -1
- package/lib/viewModels/Validatable.d.ts +8 -10
- package/lib/viewModels/Validatable.d.ts.map +1 -1
- package/lib/viewModels/Validatable.js +5 -6
- package/lib/viewModels/Validatable.js.map +1 -1
- package/package.json +5 -3
- package/src/disposer.ts +4 -1
- package/src/lazy.light.ts +15 -11
- package/src/lazy.ts +22 -15
- package/src/pool.ts +31 -8
- package/src/validation/types.ts +5 -5
- package/src/viewModels/TextModel.ts +3 -4
- package/src/viewModels/Validatable.ts +14 -14
- package/yarn-error.log +3709 -0
- package/yarn.lock +1 -1
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
import { ValidatorFunction, ValidatorFunctionAsync, ValidationErrors } from '../validation';
|
|
2
|
-
export declare type ValueValidator<T> = ValidatorFunction<T> | ValidatorFunctionAsync<T>;
|
|
3
|
-
export declare type ValidationErrorsStrings =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
validator: ValueValidator<Readonly<T>>;
|
|
8
|
-
errors: ValidationErrorsStrings;
|
|
2
|
+
export declare type ValueValidator<T, TErrors = ValidationErrors> = ValidatorFunction<T, TErrors> | ValidatorFunctionAsync<T, TErrors>;
|
|
3
|
+
export declare type ValidationErrorsStrings<TErrors extends string | number = number> = Record<TErrors, string>;
|
|
4
|
+
export declare type ValidationConfig<T, TErrors extends string | number = ValidationErrors> = {
|
|
5
|
+
validator: ValueValidator<Readonly<T>, TErrors>;
|
|
6
|
+
errors: ValidationErrorsStrings<TErrors>;
|
|
9
7
|
};
|
|
10
8
|
export declare abstract class ValidatableModel<T = string> {
|
|
11
9
|
private _validator;
|
|
12
10
|
private _strings;
|
|
13
11
|
private _error;
|
|
14
12
|
private _validationError;
|
|
15
|
-
constructor(
|
|
13
|
+
constructor();
|
|
16
14
|
protected abstract get valueToValidate(): Readonly<T>;
|
|
17
15
|
get isValid(): boolean;
|
|
18
16
|
get error(): string;
|
|
19
|
-
setValidationConfig(config?: ValidationConfig<T>): this;
|
|
20
|
-
testValidate(value: T): Promise<
|
|
17
|
+
setValidationConfig<TErrors extends string | number = ValidationErrors>(config?: ValidationConfig<T, TErrors>): this;
|
|
18
|
+
testValidate(value: T): Promise<any>;
|
|
21
19
|
validate(): Promise<boolean>;
|
|
22
20
|
getIsInvalid(): Promise<boolean>;
|
|
23
21
|
reset(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Validatable.d.ts","sourceRoot":"","sources":["../../src/viewModels/Validatable.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,gBAAgB,EAAmB,MAAM,eAAe,CAAC;AAG7G,oBAAY,cAAc,CAAC,CAAC,IAAI,iBAAiB,CAAC,CAAC,CAAC,GAAG,sBAAsB,CAAC,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"Validatable.d.ts","sourceRoot":"","sources":["../../src/viewModels/Validatable.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,gBAAgB,EAAmB,MAAM,eAAe,CAAC;AAG7G,oBAAY,cAAc,CAAC,CAAC,EAAE,OAAO,GAAG,gBAAgB,IAAI,iBAAiB,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,sBAAsB,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;AAC/H,oBAAY,uBAAuB,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,IAAI,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;AAExG,oBAAY,gBAAgB,CAAC,CAAC,EAAE,OAAO,SAAS,MAAM,GAAG,MAAM,GAAG,gBAAgB,IAAI;IAClF,SAAS,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IAChD,MAAM,EAAE,uBAAuB,CAAC,OAAO,CAAC,CAAC;CAC5C,CAAC;AAIF,8BAAsB,gBAAgB,CAAC,CAAC,GAAG,MAAM;IAE7C,OAAO,CAAC,UAAU,CAA0C;IAC5D,OAAO,CAAC,QAAQ,CAAsC;IAGtD,OAAO,CAAC,MAAM,CAAgB;IAE9B,OAAO,CAAC,gBAAgB,CAAyB;;IAMjD,SAAS,CAAC,QAAQ,KAAK,eAAe,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEtD,IAAI,OAAO,YAA2B;IAEtC,IAAI,KAAK,WAA0B;IAE5B,mBAAmB,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,GAAG,gBAAgB,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC,EAAE,OAAO,CAAC;IAMvG,YAAY,CAAC,KAAK,EAAE,CAAC;IAQ5B,QAAQ;IAuBR,YAAY;IAMlB,KAAK;WAKQ,aAAa,CAAC,YAAY,EAAE,aAAa,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC,EAAE,UAAU,UAAO;CAQxG"}
|
|
@@ -5,15 +5,14 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const mobx_1 = require("mobx");
|
|
6
6
|
const validation_1 = require("../validation");
|
|
7
7
|
const arrays_1 = require("../async/arrays");
|
|
8
|
-
const EmptyValidator = () =>
|
|
8
|
+
const EmptyValidator = () => 0;
|
|
9
9
|
class ValidatableModel {
|
|
10
|
-
constructor(
|
|
10
|
+
constructor() {
|
|
11
11
|
this._validator = null;
|
|
12
12
|
this._strings = null;
|
|
13
13
|
this._error = null;
|
|
14
14
|
this._validationError = null;
|
|
15
15
|
mobx_1.makeObservable(this);
|
|
16
|
-
this.setValidationConfig(config);
|
|
17
16
|
}
|
|
18
17
|
get isValid() { return !this._error; }
|
|
19
18
|
get error() { return this._error; }
|
|
@@ -34,10 +33,10 @@ class ValidatableModel {
|
|
|
34
33
|
return true;
|
|
35
34
|
}
|
|
36
35
|
try {
|
|
37
|
-
const
|
|
38
|
-
this._validationError =
|
|
36
|
+
const validationResult = await this._validator(this.valueToValidate);
|
|
37
|
+
this._validationError = !validationResult
|
|
39
38
|
? null
|
|
40
|
-
: new validation_1.ValidationError('Unknown error',
|
|
39
|
+
: new validation_1.ValidationError('Unknown error', validationResult);
|
|
41
40
|
}
|
|
42
41
|
catch (err) {
|
|
43
42
|
this._validationError = err;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Validatable.js","sourceRoot":"","sources":["../../src/viewModels/Validatable.ts"],"names":[],"mappings":";;;;AAAA,+BAA0D;AAC1D,8CAA6G;AAC7G,4CAA4C;AAU5C,MAAM,cAAc,GAAG,GAAG,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"Validatable.js","sourceRoot":"","sources":["../../src/viewModels/Validatable.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zajno/common",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.15",
|
|
4
4
|
"description": "Zajno's re-usable utilities for JS/TS projects",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"homepage": "https://github.com/Zajno/common-utils#readme",
|
|
15
15
|
"scripts": {
|
|
16
|
-
"build": "
|
|
16
|
+
"build": "rimraf ./lib && tsc",
|
|
17
17
|
"build:w": "yarn build --watch",
|
|
18
18
|
"publish:local": "yalc push --replace --update",
|
|
19
19
|
"prepare": "yarn build && yarn lint && yarn test",
|
|
@@ -21,7 +21,9 @@
|
|
|
21
21
|
"lint": "eslint \"src/**/*.ts?(x)\"",
|
|
22
22
|
"test": "yarn jest --clearCache && yarn jest"
|
|
23
23
|
},
|
|
24
|
-
"dependencies": {
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"rimraf": "^3"
|
|
26
|
+
},
|
|
25
27
|
"devDependencies": {
|
|
26
28
|
"@types/faker": "^5.5.9",
|
|
27
29
|
"@types/jest": "^27.0.2",
|
package/src/disposer.ts
CHANGED
|
@@ -59,9 +59,12 @@ export class Disposer {
|
|
|
59
59
|
);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
this._disposers.
|
|
62
|
+
const copy = this._disposers.slice().reverse();
|
|
63
63
|
this._disposers.length = 0;
|
|
64
64
|
this._map.clear();
|
|
65
|
+
|
|
66
|
+
// this should separate side effects
|
|
67
|
+
copy.forEach(d => d());
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
|
package/src/lazy.light.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { IDisposable } from './disposer';
|
|
1
2
|
|
|
2
|
-
export type LazyLight<T> = {
|
|
3
|
+
export type LazyLight<T> = IDisposable & {
|
|
3
4
|
readonly value: T;
|
|
4
5
|
readonly hasValue: boolean;
|
|
5
6
|
|
|
@@ -7,19 +8,22 @@ export type LazyLight<T> = {
|
|
|
7
8
|
};
|
|
8
9
|
|
|
9
10
|
export function createLazy<T>(factory: () => T) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const _factory = factory;
|
|
12
|
+
let _instance: T = undefined;
|
|
13
|
+
|
|
14
|
+
const res: LazyLight<T> = {
|
|
13
15
|
get value() {
|
|
14
|
-
if (
|
|
15
|
-
|
|
16
|
+
if (_instance === undefined) {
|
|
17
|
+
_instance = _factory();
|
|
16
18
|
}
|
|
17
|
-
return
|
|
19
|
+
return _instance;
|
|
18
20
|
},
|
|
19
|
-
get hasValue() { return
|
|
20
|
-
reset() {
|
|
21
|
-
|
|
21
|
+
get hasValue() { return _instance !== undefined; },
|
|
22
|
+
reset: () => {
|
|
23
|
+
_instance = undefined;
|
|
22
24
|
},
|
|
25
|
+
dispose: () => res.reset(),
|
|
26
|
+
};
|
|
23
27
|
|
|
24
|
-
|
|
28
|
+
return res;
|
|
25
29
|
}
|
package/src/lazy.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { IDisposable } from 'disposer';
|
|
2
|
+
import { observable, makeObservable, action } from 'mobx';
|
|
2
3
|
|
|
3
|
-
export default class Lazy<T> {
|
|
4
|
+
export default class Lazy<T> implements IDisposable {
|
|
4
5
|
|
|
5
6
|
@observable.ref
|
|
6
7
|
private _instance: T = null;
|
|
@@ -25,9 +26,11 @@ export default class Lazy<T> {
|
|
|
25
26
|
reset() {
|
|
26
27
|
this._instance = null;
|
|
27
28
|
}
|
|
29
|
+
|
|
30
|
+
dispose = () => this.reset();
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
export class LazyPromise<T> {
|
|
33
|
+
export class LazyPromise<T> implements IDisposable {
|
|
31
34
|
|
|
32
35
|
@observable.ref
|
|
33
36
|
private _instance: T = null;
|
|
@@ -50,23 +53,27 @@ export class LazyPromise<T> {
|
|
|
50
53
|
get() {
|
|
51
54
|
if (!this._loaded && !this._busy) {
|
|
52
55
|
this._busy = true;
|
|
53
|
-
this._factory().then(
|
|
54
|
-
transaction(() => {
|
|
55
|
-
this._busy = false;
|
|
56
|
-
this._loaded = true;
|
|
57
|
-
this._instance = res;
|
|
58
|
-
});
|
|
59
|
-
});
|
|
56
|
+
this._factory().then(this.setInstance);
|
|
60
57
|
}
|
|
61
58
|
|
|
62
59
|
return this._instance;
|
|
63
60
|
}
|
|
64
61
|
|
|
62
|
+
@action
|
|
63
|
+
private setInstance = (res: T) => {
|
|
64
|
+
const wasBusy = this._busy;
|
|
65
|
+
|
|
66
|
+
this._busy = false;
|
|
67
|
+
this._loaded = wasBusy;
|
|
68
|
+
this._instance = wasBusy ? res : null;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
@action
|
|
65
72
|
reset() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
this._loaded = false;
|
|
70
|
-
});
|
|
73
|
+
this._busy = false;
|
|
74
|
+
this._instance = this.initial;
|
|
75
|
+
this._loaded = false;
|
|
71
76
|
}
|
|
77
|
+
|
|
78
|
+
dispose = () => this.reset();
|
|
72
79
|
}
|
package/src/pool.ts
CHANGED
|
@@ -1,24 +1,47 @@
|
|
|
1
|
+
import { safeCall } from 'functions';
|
|
2
|
+
import type { IDisposable } from './disposer';
|
|
1
3
|
|
|
2
4
|
export class Pool<T> {
|
|
3
5
|
|
|
4
|
-
private _container: T[] = [];
|
|
6
|
+
private readonly _container: T[] = [];
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
constructor(readonly factory: () => T) { }
|
|
9
|
+
|
|
10
|
+
public get(): T {
|
|
7
11
|
if (this._container.length > 0) {
|
|
8
12
|
return this._container.pop();
|
|
9
13
|
} else {
|
|
10
|
-
return factory();
|
|
14
|
+
return this.factory();
|
|
11
15
|
}
|
|
12
16
|
}
|
|
13
17
|
|
|
14
|
-
release(e: T): void
|
|
15
|
-
release(e: T[]): void;
|
|
16
|
-
|
|
17
|
-
release(e: T | T[]) {
|
|
18
|
+
public release(e: T | T[]): void {
|
|
18
19
|
if (Array.isArray(e)) {
|
|
19
20
|
this._container.push(...e);
|
|
20
|
-
} else {
|
|
21
|
+
} else if (e) {
|
|
21
22
|
this._container.push(e);
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
}
|
|
26
|
+
|
|
27
|
+
export class PoolDisposable<T extends IDisposable> extends Pool<T> {
|
|
28
|
+
|
|
29
|
+
constructor(factory: () => T, readonly initializer: (item: T) => void = null) {
|
|
30
|
+
super(factory);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public get(): T {
|
|
34
|
+
const res = super.get();
|
|
35
|
+
safeCall(this.initializer, res);
|
|
36
|
+
return res;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public release(e: T | T[]): void {
|
|
40
|
+
if (Array.isArray(e)) {
|
|
41
|
+
e.forEach(ee => ee.dispose());
|
|
42
|
+
} else if (e) {
|
|
43
|
+
e.dispose();
|
|
44
|
+
}
|
|
45
|
+
return super.release(e);
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/validation/types.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { ValidationErrors } from './ValidationErrors';
|
|
2
2
|
|
|
3
|
-
export type ValidatorFunction<T = string> = (val: T) =>
|
|
4
|
-
export type ValidatorFunctionAsync<T = string> = (val: T) => Promise<
|
|
3
|
+
export type ValidatorFunction<T = string, TErrors = ValidationErrors> = (val: T) => TErrors;
|
|
4
|
+
export type ValidatorFunctionAsync<T = string, TErrors = ValidationErrors> = (val: T) => Promise<TErrors>;
|
|
5
5
|
|
|
6
6
|
export type WrapperFunction = (val: ValidatorFunction) => ValidatorFunction;
|
|
7
7
|
|
|
8
|
-
export class ValidationError extends Error {
|
|
9
|
-
readonly code:
|
|
8
|
+
export class ValidationError<TErrors = ValidationErrors> extends Error {
|
|
9
|
+
readonly code: TErrors = null;
|
|
10
10
|
|
|
11
|
-
constructor(message: string, code:
|
|
11
|
+
constructor(message: string, code: TErrors) {
|
|
12
12
|
super(message);
|
|
13
13
|
this.code = code;
|
|
14
14
|
}
|
|
@@ -2,7 +2,7 @@ import { observable, computed, makeObservable, reaction } from 'mobx';
|
|
|
2
2
|
import { Getter } from '../types';
|
|
3
3
|
import { IValueModel } from './types';
|
|
4
4
|
import logger from '../logger';
|
|
5
|
-
import { ValidatableModel
|
|
5
|
+
import { ValidatableModel } from './Validatable';
|
|
6
6
|
import { IResetableModel } from 'viewModels';
|
|
7
7
|
|
|
8
8
|
export type TextInputConfig = {
|
|
@@ -11,7 +11,6 @@ export type TextInputConfig = {
|
|
|
11
11
|
value?: Getter<string>;
|
|
12
12
|
async?: boolean;
|
|
13
13
|
|
|
14
|
-
validation?: ValidationConfig<string>;
|
|
15
14
|
noSubscribe?: boolean;
|
|
16
15
|
};
|
|
17
16
|
|
|
@@ -40,7 +39,7 @@ export class Text {
|
|
|
40
39
|
get value() { return this._value; }
|
|
41
40
|
}
|
|
42
41
|
|
|
43
|
-
export class TextInputVM extends ValidatableModel implements IValueModel<string>, IResetableModel {
|
|
42
|
+
export class TextInputVM extends ValidatableModel<string> implements IValueModel<string>, IResetableModel {
|
|
44
43
|
@observable
|
|
45
44
|
private _value = '';
|
|
46
45
|
|
|
@@ -56,7 +55,7 @@ export class TextInputVM extends ValidatableModel implements IValueModel<string>
|
|
|
56
55
|
private readonly _valueObserving: () => void = null;
|
|
57
56
|
|
|
58
57
|
constructor(config?: TextInputConfig) {
|
|
59
|
-
super(
|
|
58
|
+
super();
|
|
60
59
|
makeObservable(this);
|
|
61
60
|
config = config || {};
|
|
62
61
|
|
|
@@ -2,28 +2,28 @@ import { observable, makeObservable, action } from 'mobx';
|
|
|
2
2
|
import { ValidatorFunction, ValidatorFunctionAsync, ValidationErrors, ValidationError } from '../validation';
|
|
3
3
|
import { someAsync } from '../async/arrays';
|
|
4
4
|
|
|
5
|
-
export type ValueValidator<T> = ValidatorFunction<T> | ValidatorFunctionAsync<T>;
|
|
6
|
-
export type ValidationErrorsStrings
|
|
5
|
+
export type ValueValidator<T, TErrors = ValidationErrors> = ValidatorFunction<T, TErrors> | ValidatorFunctionAsync<T, TErrors>;
|
|
6
|
+
export type ValidationErrorsStrings<TErrors extends string | number = number> = Record<TErrors, string>;
|
|
7
7
|
|
|
8
|
-
export type ValidationConfig<T> = {
|
|
9
|
-
validator: ValueValidator<Readonly<T
|
|
10
|
-
errors: ValidationErrorsStrings
|
|
8
|
+
export type ValidationConfig<T, TErrors extends string | number = ValidationErrors> = {
|
|
9
|
+
validator: ValueValidator<Readonly<T>, TErrors>,
|
|
10
|
+
errors: ValidationErrorsStrings<TErrors>,
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
const EmptyValidator = () =>
|
|
13
|
+
const EmptyValidator = () => 0;
|
|
14
|
+
|
|
14
15
|
export abstract class ValidatableModel<T = string> {
|
|
15
16
|
|
|
16
|
-
private _validator: ValueValidator<Readonly<T
|
|
17
|
-
private _strings: ValidationErrorsStrings = null;
|
|
17
|
+
private _validator: ValueValidator<Readonly<T>, any> = null;
|
|
18
|
+
private _strings: ValidationErrorsStrings<any> = null;
|
|
18
19
|
|
|
19
20
|
@observable
|
|
20
21
|
private _error: string = null;
|
|
21
22
|
|
|
22
23
|
private _validationError: ValidationError = null;
|
|
23
24
|
|
|
24
|
-
constructor(
|
|
25
|
+
constructor() {
|
|
25
26
|
makeObservable(this);
|
|
26
|
-
this.setValidationConfig(config);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
protected abstract get valueToValidate(): Readonly<T>;
|
|
@@ -32,7 +32,7 @@ export abstract class ValidatableModel<T = string> {
|
|
|
32
32
|
|
|
33
33
|
get error() { return this._error; }
|
|
34
34
|
|
|
35
|
-
public setValidationConfig(config?: ValidationConfig<T>) {
|
|
35
|
+
public setValidationConfig<TErrors extends string | number = ValidationErrors>(config?: ValidationConfig<T, TErrors>) {
|
|
36
36
|
this._validator = config?.validator || EmptyValidator;
|
|
37
37
|
this._strings = config?.errors;
|
|
38
38
|
return this;
|
|
@@ -52,10 +52,10 @@ export abstract class ValidatableModel<T = string> {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
try {
|
|
55
|
-
const
|
|
56
|
-
this._validationError =
|
|
55
|
+
const validationResult = await this._validator(this.valueToValidate);
|
|
56
|
+
this._validationError = !validationResult
|
|
57
57
|
? null
|
|
58
|
-
: new ValidationError('Unknown error',
|
|
58
|
+
: new ValidationError('Unknown error', validationResult);
|
|
59
59
|
} catch (err) {
|
|
60
60
|
this._validationError = err as ValidationError;
|
|
61
61
|
}
|