adaptform 1.1.8 → 2.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/CHANGELOG.md +0 -0
- package/dist/index.cjs +118 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +239 -0
- package/dist/index.d.ts +239 -6
- package/dist/index.mjs +97 -266
- package/dist/index.mjs.map +1 -0
- package/package.json +46 -16
- package/dist/core/FieldOptions.d.ts +0 -24
- package/dist/core/Form.d.ts +0 -13
- package/dist/core/FormField.d.ts +0 -23
- package/dist/core/date/DateTimeField.d.ts +0 -20
- package/dist/core/number/NumberField.d.ts +0 -13
- package/dist/core/string/StringField.d.ts +0 -18
package/CHANGELOG.md
ADDED
|
File without changes
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/core/plugins/FieldPlugin.ts
|
|
4
|
+
var BasePlugin = class {
|
|
5
|
+
constructor(pluginOptions = {}) {
|
|
6
|
+
this.pluginOptions = {
|
|
7
|
+
priority: 0,
|
|
8
|
+
...pluginOptions
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/core/types/FieldType.ts
|
|
14
|
+
var FieldType = class {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.options = {
|
|
17
|
+
isRequired: true,
|
|
18
|
+
messages: {},
|
|
19
|
+
...options
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/core/types/BooleanType.ts
|
|
25
|
+
var BooleanType = class extends FieldType {
|
|
26
|
+
cast(rawValue) {
|
|
27
|
+
if (rawValue === null || rawValue === void 0) return null;
|
|
28
|
+
if (typeof rawValue === "boolean") return rawValue;
|
|
29
|
+
if (typeof rawValue === "string") {
|
|
30
|
+
if (rawValue === "true" || rawValue === "1") return true;
|
|
31
|
+
if (rawValue === "false" || rawValue === "0") return false;
|
|
32
|
+
}
|
|
33
|
+
if (typeof rawValue === "number") {
|
|
34
|
+
if (rawValue === 1) return true;
|
|
35
|
+
if (rawValue === 0) return false;
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
validate(value) {
|
|
40
|
+
const errors = [];
|
|
41
|
+
if (this.options.mustBeTrue && value !== true) {
|
|
42
|
+
errors.push(this.options.messages?.mustBeTrue ?? "\u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 \u0434\u043E\u043B\u0436\u043D\u043E \u0431\u044B\u0442\u044C true");
|
|
43
|
+
}
|
|
44
|
+
if (this.options.mustBeFalse && value !== false) {
|
|
45
|
+
errors.push(this.options.messages?.mustBeFalse ?? "\u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 \u0434\u043E\u043B\u0436\u043D\u043E \u0431\u044B\u0442\u044C false");
|
|
46
|
+
}
|
|
47
|
+
return errors;
|
|
48
|
+
}
|
|
49
|
+
getTypeName() {
|
|
50
|
+
return "\u043B\u043E\u0433\u0438\u0447\u0435\u0441\u043A\u043E\u0435 \u0437\u043D\u0430\u0447\u0435\u043D\u0438\u0435";
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/core/utils/luxon-loader.ts
|
|
55
|
+
var DateTime = null;
|
|
56
|
+
var isLoading = false;
|
|
57
|
+
var loadError = null;
|
|
58
|
+
var loadPromise = null;
|
|
59
|
+
function getDateTime() {
|
|
60
|
+
if (DateTime) {
|
|
61
|
+
return DateTime;
|
|
62
|
+
}
|
|
63
|
+
if (loadError) {
|
|
64
|
+
throw loadError;
|
|
65
|
+
}
|
|
66
|
+
if (isLoading) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
"Luxon is still loading. Please await initLuxon() first."
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
throw new Error(
|
|
72
|
+
"Luxon is not initialized. Call initLuxon() first or install: npm install luxon"
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
function initLuxon() {
|
|
76
|
+
if (DateTime) {
|
|
77
|
+
return Promise.resolve(DateTime);
|
|
78
|
+
}
|
|
79
|
+
if (loadPromise) {
|
|
80
|
+
return loadPromise;
|
|
81
|
+
}
|
|
82
|
+
isLoading = true;
|
|
83
|
+
loadPromise = (async () => {
|
|
84
|
+
try {
|
|
85
|
+
const luxon = await import('luxon');
|
|
86
|
+
DateTime = luxon.DateTime;
|
|
87
|
+
return DateTime;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
loadError = new Error(
|
|
90
|
+
'DateLuxonType requires the "luxon" package to be installed.\nPlease install it by running:\n\n npm install luxon\n # or\n yarn add luxon\n # or\n pnpm add luxon\n'
|
|
91
|
+
);
|
|
92
|
+
throw loadError;
|
|
93
|
+
} finally {
|
|
94
|
+
isLoading = false;
|
|
95
|
+
}
|
|
96
|
+
})();
|
|
97
|
+
return loadPromise;
|
|
98
|
+
}
|
|
99
|
+
function isLuxonAvailable() {
|
|
100
|
+
return DateTime !== null;
|
|
101
|
+
}
|
|
102
|
+
function resetLuxon() {
|
|
103
|
+
DateTime = null;
|
|
104
|
+
isLoading = false;
|
|
105
|
+
loadError = null;
|
|
106
|
+
loadPromise = null;
|
|
107
|
+
}
|
|
108
|
+
initLuxon().catch(() => {
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
exports.BasePlugin = BasePlugin;
|
|
112
|
+
exports.BooleanType = BooleanType;
|
|
113
|
+
exports.getDateTime = getDateTime;
|
|
114
|
+
exports.initLuxon = initLuxon;
|
|
115
|
+
exports.isLuxonAvailable = isLuxonAvailable;
|
|
116
|
+
exports.resetLuxon = resetLuxon;
|
|
117
|
+
//# sourceMappingURL=index.cjs.map
|
|
118
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/plugins/FieldPlugin.ts","../src/core/types/FieldType.ts","../src/core/types/BooleanType.ts","../src/core/utils/luxon-loader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AAAA,OAOF;AACA,MAAA,MAAM,SAAA;AAAA,IACR,CAAA,SAAE;AACA,MAAA,SAAA,GAAY,KAAA;AAAA,IACd;AAAA,EACF,CAAA,GAAG;AAEH,EAAA,OAAO,WAAA;AACT;AAEO,SAAS,gBAAA,GAA4B;AAC1C,EAAA,OAAO,QAAA,KAAa,IAAA;AACtB;AAEO,SAAS,UAAA,GAAmB;AACjC,EAAA,QAAA,GAAW,IAAA;AACX,EAAA,SAAA,GAAY,KAAA;AACZ,EAAA,SAAA,GAAY,IAAA;AACZ,EAAA,WAAA,GAAc,IAAA;AAChB;AAEA,SAAA,EAAU,CAAE,MAAM,MAAM;AACxB,CAAC,CAAA","file":"index.cjs","sourcesContent":["import {PluginOptions} from \"@core/options\";\nimport Field from \"@core/fields/Field\";\n\nexport interface FieldPlugin<T = any> {\n pluginOptions?: PluginOptions\n\n init?(field: Field<T>): void\n\n toRawValue?(value: any, field: Field<T>): any\n\n toValueClear?(value: any, field: Field<T>): any\n}\n\nexport abstract class BasePlugin<T = any> implements FieldPlugin<T> {\n pluginOptions: PluginOptions\n\n constructor(pluginOptions: PluginOptions = {}) {\n this.pluginOptions = {\n priority: 0,\n ...pluginOptions\n }\n }\n}","import {FieldOptions} from \"@core/options\";\nimport Form from \"@core/Form\";\n\n\nexport default abstract class FieldType<T, O extends FieldOptions = FieldOptions> {\n options: O\n\n constructor(options: O) {\n this.options = {\n isRequired: true,\n messages: {},\n ...options\n } as O\n }\n\n abstract cast(rawValue: any): T | null\n\n abstract validate(value: T, form?: Form): string[]\n\n abstract getTypeName(): string\n}","import FieldType from \"@core/types/FieldType\";\nimport {BooleanOptions} from \"@core/options\";\n\nexport class BooleanType extends FieldType<boolean, BooleanOptions> {\n cast(rawValue: any): boolean | null {\n if (rawValue === null || rawValue === undefined) return null\n if (typeof rawValue === 'boolean') return rawValue\n\n if (typeof rawValue === 'string') {\n if (rawValue === 'true' || rawValue === '1') return true\n if (rawValue === 'false' || rawValue === '0') return false\n }\n\n if (typeof rawValue === 'number') {\n if (rawValue === 1) return true\n if (rawValue === 0) return false\n }\n\n return null\n }\n\n validate(value: boolean): string[] {\n const errors: string[] = []\n\n if (this.options.mustBeTrue && value !== true) {\n errors.push(this.options.messages?.mustBeTrue ?? 'Значение должно быть true')\n }\n\n if (this.options.mustBeFalse && value !== false) {\n errors.push(this.options.messages?.mustBeFalse ?? 'Значение должно быть false')\n }\n\n return errors\n }\n\n getTypeName(): string {\n return 'логическое значение'\n }\n}","import type { DateTime as LuxonDateTime } from 'luxon';\n\nlet DateTime: typeof LuxonDateTime | null = null;\nlet isLoading = false;\nlet loadError: Error | null = null;\nlet loadPromise: Promise<typeof LuxonDateTime> | null = null;\n\nexport function getDateTime(): typeof LuxonDateTime {\n if (DateTime) {\n return DateTime;\n }\n\n if (loadError) {\n throw loadError;\n }\n\n if (isLoading) {\n throw new Error(\n 'Luxon is still loading. Please await initLuxon() first.'\n );\n }\n\n throw new Error(\n 'Luxon is not initialized. Call initLuxon() first or install: npm install luxon'\n );\n}\n\nexport function initLuxon(): Promise<typeof LuxonDateTime> {\n if (DateTime) {\n return Promise.resolve(DateTime);\n }\n\n if (loadPromise) {\n return loadPromise;\n }\n\n isLoading = true;\n loadPromise = (async () => {\n try {\n // Пробуем загрузить через ESM (работает везде)\n const luxon = await import('luxon');\n DateTime = luxon.DateTime;\n return DateTime!;\n } catch (error) {\n loadError = new Error(\n 'DateLuxonType requires the \"luxon\" package to be installed.\\n' +\n 'Please install it by running:\\n\\n' +\n ' npm install luxon\\n' +\n ' # or\\n' +\n ' yarn add luxon\\n' +\n ' # or\\n' +\n ' pnpm add luxon\\n'\n );\n throw loadError;\n } finally {\n isLoading = false;\n }\n })();\n\n return loadPromise;\n}\n\nexport function isLuxonAvailable(): boolean {\n return DateTime !== null;\n}\n\nexport function resetLuxon(): void {\n DateTime = null;\n isLoading = false;\n loadError = null;\n loadPromise = null;\n}\n\ninitLuxon().catch(() => {\n});\n\nexport type { LuxonDateTime };"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
export { DateTime as LuxonDateTime } from 'luxon';
|
|
3
|
+
|
|
4
|
+
interface FieldPlugin<T = any> {
|
|
5
|
+
pluginOptions?: PluginOptions;
|
|
6
|
+
init?(field: Field<T>): void;
|
|
7
|
+
toRawValue?(value: any, field: Field<T>): any;
|
|
8
|
+
toValueClear?(value: any, field: Field<T>): any;
|
|
9
|
+
}
|
|
10
|
+
declare abstract class BasePlugin<T = any> implements FieldPlugin<T> {
|
|
11
|
+
pluginOptions: PluginOptions;
|
|
12
|
+
constructor(pluginOptions?: PluginOptions);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface messagesOptions {
|
|
16
|
+
isRequired?: string;
|
|
17
|
+
validate?: string;
|
|
18
|
+
type?: string;
|
|
19
|
+
invalid?: string;
|
|
20
|
+
}
|
|
21
|
+
interface messagesStringOptions extends messagesOptions {
|
|
22
|
+
minLength?: string;
|
|
23
|
+
maxLength?: string;
|
|
24
|
+
pattern?: string;
|
|
25
|
+
}
|
|
26
|
+
interface messagesNumberOptions extends messagesOptions {
|
|
27
|
+
gt?: string;
|
|
28
|
+
ge?: string;
|
|
29
|
+
lt?: string;
|
|
30
|
+
le?: string;
|
|
31
|
+
}
|
|
32
|
+
interface messagesBooleanOptions extends messagesOptions {
|
|
33
|
+
mustBeTrue?: string;
|
|
34
|
+
mustBeFalse?: string;
|
|
35
|
+
}
|
|
36
|
+
interface messagesDateOptions extends messagesOptions {
|
|
37
|
+
minDate?: string;
|
|
38
|
+
maxDate?: string;
|
|
39
|
+
}
|
|
40
|
+
interface messagesDateLuxonOptions extends messagesOptions {
|
|
41
|
+
minDate?: string;
|
|
42
|
+
maxDate?: string;
|
|
43
|
+
}
|
|
44
|
+
interface messagesDecimalOptions extends messagesOptions {
|
|
45
|
+
gt?: string;
|
|
46
|
+
ge?: string;
|
|
47
|
+
lt?: string;
|
|
48
|
+
le?: string;
|
|
49
|
+
decimalPlaces?: string;
|
|
50
|
+
maxDigits?: string;
|
|
51
|
+
}
|
|
52
|
+
interface messagesArrayFieldOptions extends messagesOptions {
|
|
53
|
+
minItems?: string;
|
|
54
|
+
maxItems?: string;
|
|
55
|
+
}
|
|
56
|
+
interface messagesArrayFormFieldOptions extends messagesOptions {
|
|
57
|
+
minForms?: string;
|
|
58
|
+
maxForms?: string;
|
|
59
|
+
}
|
|
60
|
+
interface FieldOptions {
|
|
61
|
+
isRequired?: boolean | ((form?: any) => boolean);
|
|
62
|
+
messages?: messagesOptions;
|
|
63
|
+
validate?: (value: any, form?: any) => boolean | string;
|
|
64
|
+
plugins?: FieldPlugin[];
|
|
65
|
+
}
|
|
66
|
+
interface StringOptions extends FieldOptions {
|
|
67
|
+
minLength?: number;
|
|
68
|
+
maxLength?: number;
|
|
69
|
+
pattern?: RegExp;
|
|
70
|
+
messages?: messagesStringOptions;
|
|
71
|
+
}
|
|
72
|
+
interface NumberOptions extends FieldOptions {
|
|
73
|
+
gt?: number;
|
|
74
|
+
ge?: number;
|
|
75
|
+
lt?: number;
|
|
76
|
+
le?: number;
|
|
77
|
+
messages?: messagesNumberOptions;
|
|
78
|
+
}
|
|
79
|
+
interface BooleanOptions extends FieldOptions {
|
|
80
|
+
mustBeTrue?: boolean;
|
|
81
|
+
mustBeFalse?: boolean;
|
|
82
|
+
messages?: messagesBooleanOptions;
|
|
83
|
+
}
|
|
84
|
+
interface DateOptions extends FieldOptions {
|
|
85
|
+
minDate?: Date;
|
|
86
|
+
maxDate?: Date;
|
|
87
|
+
isDatetime?: boolean;
|
|
88
|
+
messages?: messagesDateOptions;
|
|
89
|
+
}
|
|
90
|
+
interface DateLuxonOptions extends FieldOptions {
|
|
91
|
+
minDate?: DateTime;
|
|
92
|
+
maxDate?: DateTime;
|
|
93
|
+
inputFormat?: string;
|
|
94
|
+
outputFormat?: string;
|
|
95
|
+
isDatetime?: boolean;
|
|
96
|
+
messages?: messagesDateLuxonOptions;
|
|
97
|
+
}
|
|
98
|
+
interface DecimalOptions extends FieldOptions {
|
|
99
|
+
gt?: number;
|
|
100
|
+
ge?: number;
|
|
101
|
+
lt?: number;
|
|
102
|
+
le?: number;
|
|
103
|
+
decimalPlaces?: number;
|
|
104
|
+
maxDigits?: number;
|
|
105
|
+
messages?: messagesDecimalOptions;
|
|
106
|
+
}
|
|
107
|
+
interface ArrayFieldOptions {
|
|
108
|
+
isRequired?: boolean | ((form?: any) => boolean);
|
|
109
|
+
messages?: messagesArrayFieldOptions;
|
|
110
|
+
validate?: (value: any[], form?: any) => boolean | string;
|
|
111
|
+
minItems?: number;
|
|
112
|
+
maxItems?: number;
|
|
113
|
+
}
|
|
114
|
+
interface ArrayFormFieldOptions {
|
|
115
|
+
isRequired?: boolean | ((form?: any) => boolean);
|
|
116
|
+
messages?: messagesArrayFormFieldOptions;
|
|
117
|
+
validate?: (value: any[], form?: any) => boolean | string;
|
|
118
|
+
minForms?: number;
|
|
119
|
+
maxForms?: number;
|
|
120
|
+
}
|
|
121
|
+
interface PluginOptions {
|
|
122
|
+
priority?: number;
|
|
123
|
+
}
|
|
124
|
+
interface MaskPluginOptions {
|
|
125
|
+
maskFormat: string;
|
|
126
|
+
maskPlaceholder: string;
|
|
127
|
+
digitPattern: RegExp;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
declare abstract class FieldType<T, O extends FieldOptions = FieldOptions> {
|
|
131
|
+
options: O;
|
|
132
|
+
constructor(options: O);
|
|
133
|
+
abstract cast(rawValue: any): T | null;
|
|
134
|
+
abstract validate(value: T, form?: Form): string[];
|
|
135
|
+
abstract getTypeName(): string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type FieldTypeConstructor<T, O extends FieldOptions = FieldOptions> = new (options: O) => FieldType<T, O>;
|
|
139
|
+
declare class Field<T = any, O extends FieldOptions = FieldOptions> {
|
|
140
|
+
protected _rawValue: any;
|
|
141
|
+
protected _valueClear: T | null;
|
|
142
|
+
protected filed_type: FieldType<T, O>;
|
|
143
|
+
protected defaultValue: T | null;
|
|
144
|
+
protected _errors: string[];
|
|
145
|
+
isValid: boolean | null;
|
|
146
|
+
isTouched: boolean;
|
|
147
|
+
constructor(TypeConstructor: FieldTypeConstructor<T, O>, defaultValue?: T | null, options?: O | null);
|
|
148
|
+
protected get plugins(): FieldPlugin<any>[];
|
|
149
|
+
get options(): O;
|
|
150
|
+
set options(options: O);
|
|
151
|
+
get rawValue(): T | null;
|
|
152
|
+
set rawValue(input: any);
|
|
153
|
+
get errors(): string[];
|
|
154
|
+
get error(): string;
|
|
155
|
+
set error(message: string | null);
|
|
156
|
+
get valueClear(): any;
|
|
157
|
+
private isRequired;
|
|
158
|
+
checkValid(form?: Form): boolean;
|
|
159
|
+
checkAndSetValid(form?: Form): boolean;
|
|
160
|
+
get isEmpty(): boolean;
|
|
161
|
+
reset(): void;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
declare class ArrayField<T = any> {
|
|
165
|
+
protected factory: (value?: T) => Field<T>;
|
|
166
|
+
protected options: ArrayFieldOptions;
|
|
167
|
+
protected _items: Field<T>[];
|
|
168
|
+
protected _errors: string[];
|
|
169
|
+
isTouched: boolean;
|
|
170
|
+
constructor(factory: (value?: T) => Field<T>, options?: ArrayFieldOptions, initial?: T[]);
|
|
171
|
+
set itemsValue(items: T[]);
|
|
172
|
+
get itemsValue(): T[];
|
|
173
|
+
get items(): Field<T>[];
|
|
174
|
+
add(value?: T, force_check?: boolean): Field<T>;
|
|
175
|
+
remove(index: number): void;
|
|
176
|
+
clear(): void;
|
|
177
|
+
private isRequired;
|
|
178
|
+
checkValid(form?: Form): boolean;
|
|
179
|
+
checkAndSetValid(form?: Form): boolean;
|
|
180
|
+
get isValid(): boolean;
|
|
181
|
+
get error(): string;
|
|
182
|
+
set error(message: string | null);
|
|
183
|
+
get errors(): string[];
|
|
184
|
+
reset(): void;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
declare class ArrayFormField<T extends Form = Form> {
|
|
188
|
+
private FormConstructor;
|
|
189
|
+
private options;
|
|
190
|
+
private _forms;
|
|
191
|
+
private _errors;
|
|
192
|
+
isTouched: boolean;
|
|
193
|
+
constructor(FormConstructor: new () => T, options?: ArrayFormFieldOptions, initial?: any[]);
|
|
194
|
+
set forms(forms: any[]);
|
|
195
|
+
get forms(): Form[];
|
|
196
|
+
get formsFieldsValue(): any[];
|
|
197
|
+
add(data?: any, force_check?: boolean): T;
|
|
198
|
+
remove(index: number): void;
|
|
199
|
+
clear(): void;
|
|
200
|
+
reset(): void;
|
|
201
|
+
private isRequired;
|
|
202
|
+
checkValid(form?: Form): boolean;
|
|
203
|
+
checkAndSetValid(form?: Form): boolean;
|
|
204
|
+
get isValid(): boolean;
|
|
205
|
+
get errors(): string[];
|
|
206
|
+
get error(): string;
|
|
207
|
+
set error(message: string | null);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
declare abstract class Form {
|
|
211
|
+
private _globalErrors;
|
|
212
|
+
protected get _fields(): Record<string, Field<any> | ArrayField<any> | ArrayFormField<any>>;
|
|
213
|
+
checkValid(form?: this): boolean;
|
|
214
|
+
get fieldsValue(): Record<string, any>;
|
|
215
|
+
set fields(data: Record<string, any>);
|
|
216
|
+
get allErrors(): Record<string, string[]>;
|
|
217
|
+
get globalErrors(): Record<string, string>;
|
|
218
|
+
set errors(errors: Record<string, string>);
|
|
219
|
+
setGlobalError(key: string, message: string): void;
|
|
220
|
+
clearGlobalErrors(): void;
|
|
221
|
+
reset(): void;
|
|
222
|
+
get isTouched(): boolean;
|
|
223
|
+
get isValid(): boolean;
|
|
224
|
+
toFormData(): FormData;
|
|
225
|
+
toJSON(): Record<string, any>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
declare class BooleanType extends FieldType<boolean, BooleanOptions> {
|
|
229
|
+
cast(rawValue: any): boolean | null;
|
|
230
|
+
validate(value: boolean): string[];
|
|
231
|
+
getTypeName(): string;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
declare function getDateTime(): typeof DateTime;
|
|
235
|
+
declare function initLuxon(): Promise<typeof DateTime>;
|
|
236
|
+
declare function isLuxonAvailable(): boolean;
|
|
237
|
+
declare function resetLuxon(): void;
|
|
238
|
+
|
|
239
|
+
export { type ArrayFieldOptions, type ArrayFormFieldOptions, BasePlugin, type BooleanOptions, BooleanType, type DateLuxonOptions, type DateOptions, type DecimalOptions, type FieldOptions, type FieldPlugin, type MaskPluginOptions, type NumberOptions, type PluginOptions, type StringOptions, getDateTime, initLuxon, isLuxonAvailable, type messagesArrayFieldOptions, type messagesArrayFormFieldOptions, type messagesBooleanOptions, type messagesDateLuxonOptions, type messagesDateOptions, type messagesDecimalOptions, type messagesNumberOptions, type messagesOptions, type messagesStringOptions, resetLuxon };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,239 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
export { DateTime as LuxonDateTime } from 'luxon';
|
|
3
|
+
|
|
4
|
+
interface FieldPlugin<T = any> {
|
|
5
|
+
pluginOptions?: PluginOptions;
|
|
6
|
+
init?(field: Field<T>): void;
|
|
7
|
+
toRawValue?(value: any, field: Field<T>): any;
|
|
8
|
+
toValueClear?(value: any, field: Field<T>): any;
|
|
9
|
+
}
|
|
10
|
+
declare abstract class BasePlugin<T = any> implements FieldPlugin<T> {
|
|
11
|
+
pluginOptions: PluginOptions;
|
|
12
|
+
constructor(pluginOptions?: PluginOptions);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface messagesOptions {
|
|
16
|
+
isRequired?: string;
|
|
17
|
+
validate?: string;
|
|
18
|
+
type?: string;
|
|
19
|
+
invalid?: string;
|
|
20
|
+
}
|
|
21
|
+
interface messagesStringOptions extends messagesOptions {
|
|
22
|
+
minLength?: string;
|
|
23
|
+
maxLength?: string;
|
|
24
|
+
pattern?: string;
|
|
25
|
+
}
|
|
26
|
+
interface messagesNumberOptions extends messagesOptions {
|
|
27
|
+
gt?: string;
|
|
28
|
+
ge?: string;
|
|
29
|
+
lt?: string;
|
|
30
|
+
le?: string;
|
|
31
|
+
}
|
|
32
|
+
interface messagesBooleanOptions extends messagesOptions {
|
|
33
|
+
mustBeTrue?: string;
|
|
34
|
+
mustBeFalse?: string;
|
|
35
|
+
}
|
|
36
|
+
interface messagesDateOptions extends messagesOptions {
|
|
37
|
+
minDate?: string;
|
|
38
|
+
maxDate?: string;
|
|
39
|
+
}
|
|
40
|
+
interface messagesDateLuxonOptions extends messagesOptions {
|
|
41
|
+
minDate?: string;
|
|
42
|
+
maxDate?: string;
|
|
43
|
+
}
|
|
44
|
+
interface messagesDecimalOptions extends messagesOptions {
|
|
45
|
+
gt?: string;
|
|
46
|
+
ge?: string;
|
|
47
|
+
lt?: string;
|
|
48
|
+
le?: string;
|
|
49
|
+
decimalPlaces?: string;
|
|
50
|
+
maxDigits?: string;
|
|
51
|
+
}
|
|
52
|
+
interface messagesArrayFieldOptions extends messagesOptions {
|
|
53
|
+
minItems?: string;
|
|
54
|
+
maxItems?: string;
|
|
55
|
+
}
|
|
56
|
+
interface messagesArrayFormFieldOptions extends messagesOptions {
|
|
57
|
+
minForms?: string;
|
|
58
|
+
maxForms?: string;
|
|
59
|
+
}
|
|
60
|
+
interface FieldOptions {
|
|
61
|
+
isRequired?: boolean | ((form?: any) => boolean);
|
|
62
|
+
messages?: messagesOptions;
|
|
63
|
+
validate?: (value: any, form?: any) => boolean | string;
|
|
64
|
+
plugins?: FieldPlugin[];
|
|
65
|
+
}
|
|
66
|
+
interface StringOptions extends FieldOptions {
|
|
67
|
+
minLength?: number;
|
|
68
|
+
maxLength?: number;
|
|
69
|
+
pattern?: RegExp;
|
|
70
|
+
messages?: messagesStringOptions;
|
|
71
|
+
}
|
|
72
|
+
interface NumberOptions extends FieldOptions {
|
|
73
|
+
gt?: number;
|
|
74
|
+
ge?: number;
|
|
75
|
+
lt?: number;
|
|
76
|
+
le?: number;
|
|
77
|
+
messages?: messagesNumberOptions;
|
|
78
|
+
}
|
|
79
|
+
interface BooleanOptions extends FieldOptions {
|
|
80
|
+
mustBeTrue?: boolean;
|
|
81
|
+
mustBeFalse?: boolean;
|
|
82
|
+
messages?: messagesBooleanOptions;
|
|
83
|
+
}
|
|
84
|
+
interface DateOptions extends FieldOptions {
|
|
85
|
+
minDate?: Date;
|
|
86
|
+
maxDate?: Date;
|
|
87
|
+
isDatetime?: boolean;
|
|
88
|
+
messages?: messagesDateOptions;
|
|
89
|
+
}
|
|
90
|
+
interface DateLuxonOptions extends FieldOptions {
|
|
91
|
+
minDate?: DateTime;
|
|
92
|
+
maxDate?: DateTime;
|
|
93
|
+
inputFormat?: string;
|
|
94
|
+
outputFormat?: string;
|
|
95
|
+
isDatetime?: boolean;
|
|
96
|
+
messages?: messagesDateLuxonOptions;
|
|
97
|
+
}
|
|
98
|
+
interface DecimalOptions extends FieldOptions {
|
|
99
|
+
gt?: number;
|
|
100
|
+
ge?: number;
|
|
101
|
+
lt?: number;
|
|
102
|
+
le?: number;
|
|
103
|
+
decimalPlaces?: number;
|
|
104
|
+
maxDigits?: number;
|
|
105
|
+
messages?: messagesDecimalOptions;
|
|
106
|
+
}
|
|
107
|
+
interface ArrayFieldOptions {
|
|
108
|
+
isRequired?: boolean | ((form?: any) => boolean);
|
|
109
|
+
messages?: messagesArrayFieldOptions;
|
|
110
|
+
validate?: (value: any[], form?: any) => boolean | string;
|
|
111
|
+
minItems?: number;
|
|
112
|
+
maxItems?: number;
|
|
113
|
+
}
|
|
114
|
+
interface ArrayFormFieldOptions {
|
|
115
|
+
isRequired?: boolean | ((form?: any) => boolean);
|
|
116
|
+
messages?: messagesArrayFormFieldOptions;
|
|
117
|
+
validate?: (value: any[], form?: any) => boolean | string;
|
|
118
|
+
minForms?: number;
|
|
119
|
+
maxForms?: number;
|
|
120
|
+
}
|
|
121
|
+
interface PluginOptions {
|
|
122
|
+
priority?: number;
|
|
123
|
+
}
|
|
124
|
+
interface MaskPluginOptions {
|
|
125
|
+
maskFormat: string;
|
|
126
|
+
maskPlaceholder: string;
|
|
127
|
+
digitPattern: RegExp;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
declare abstract class FieldType<T, O extends FieldOptions = FieldOptions> {
|
|
131
|
+
options: O;
|
|
132
|
+
constructor(options: O);
|
|
133
|
+
abstract cast(rawValue: any): T | null;
|
|
134
|
+
abstract validate(value: T, form?: Form): string[];
|
|
135
|
+
abstract getTypeName(): string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type FieldTypeConstructor<T, O extends FieldOptions = FieldOptions> = new (options: O) => FieldType<T, O>;
|
|
139
|
+
declare class Field<T = any, O extends FieldOptions = FieldOptions> {
|
|
140
|
+
protected _rawValue: any;
|
|
141
|
+
protected _valueClear: T | null;
|
|
142
|
+
protected filed_type: FieldType<T, O>;
|
|
143
|
+
protected defaultValue: T | null;
|
|
144
|
+
protected _errors: string[];
|
|
145
|
+
isValid: boolean | null;
|
|
146
|
+
isTouched: boolean;
|
|
147
|
+
constructor(TypeConstructor: FieldTypeConstructor<T, O>, defaultValue?: T | null, options?: O | null);
|
|
148
|
+
protected get plugins(): FieldPlugin<any>[];
|
|
149
|
+
get options(): O;
|
|
150
|
+
set options(options: O);
|
|
151
|
+
get rawValue(): T | null;
|
|
152
|
+
set rawValue(input: any);
|
|
153
|
+
get errors(): string[];
|
|
154
|
+
get error(): string;
|
|
155
|
+
set error(message: string | null);
|
|
156
|
+
get valueClear(): any;
|
|
157
|
+
private isRequired;
|
|
158
|
+
checkValid(form?: Form): boolean;
|
|
159
|
+
checkAndSetValid(form?: Form): boolean;
|
|
160
|
+
get isEmpty(): boolean;
|
|
161
|
+
reset(): void;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
declare class ArrayField<T = any> {
|
|
165
|
+
protected factory: (value?: T) => Field<T>;
|
|
166
|
+
protected options: ArrayFieldOptions;
|
|
167
|
+
protected _items: Field<T>[];
|
|
168
|
+
protected _errors: string[];
|
|
169
|
+
isTouched: boolean;
|
|
170
|
+
constructor(factory: (value?: T) => Field<T>, options?: ArrayFieldOptions, initial?: T[]);
|
|
171
|
+
set itemsValue(items: T[]);
|
|
172
|
+
get itemsValue(): T[];
|
|
173
|
+
get items(): Field<T>[];
|
|
174
|
+
add(value?: T, force_check?: boolean): Field<T>;
|
|
175
|
+
remove(index: number): void;
|
|
176
|
+
clear(): void;
|
|
177
|
+
private isRequired;
|
|
178
|
+
checkValid(form?: Form): boolean;
|
|
179
|
+
checkAndSetValid(form?: Form): boolean;
|
|
180
|
+
get isValid(): boolean;
|
|
181
|
+
get error(): string;
|
|
182
|
+
set error(message: string | null);
|
|
183
|
+
get errors(): string[];
|
|
184
|
+
reset(): void;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
declare class ArrayFormField<T extends Form = Form> {
|
|
188
|
+
private FormConstructor;
|
|
189
|
+
private options;
|
|
190
|
+
private _forms;
|
|
191
|
+
private _errors;
|
|
192
|
+
isTouched: boolean;
|
|
193
|
+
constructor(FormConstructor: new () => T, options?: ArrayFormFieldOptions, initial?: any[]);
|
|
194
|
+
set forms(forms: any[]);
|
|
195
|
+
get forms(): Form[];
|
|
196
|
+
get formsFieldsValue(): any[];
|
|
197
|
+
add(data?: any, force_check?: boolean): T;
|
|
198
|
+
remove(index: number): void;
|
|
199
|
+
clear(): void;
|
|
200
|
+
reset(): void;
|
|
201
|
+
private isRequired;
|
|
202
|
+
checkValid(form?: Form): boolean;
|
|
203
|
+
checkAndSetValid(form?: Form): boolean;
|
|
204
|
+
get isValid(): boolean;
|
|
205
|
+
get errors(): string[];
|
|
206
|
+
get error(): string;
|
|
207
|
+
set error(message: string | null);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
declare abstract class Form {
|
|
211
|
+
private _globalErrors;
|
|
212
|
+
protected get _fields(): Record<string, Field<any> | ArrayField<any> | ArrayFormField<any>>;
|
|
213
|
+
checkValid(form?: this): boolean;
|
|
214
|
+
get fieldsValue(): Record<string, any>;
|
|
215
|
+
set fields(data: Record<string, any>);
|
|
216
|
+
get allErrors(): Record<string, string[]>;
|
|
217
|
+
get globalErrors(): Record<string, string>;
|
|
218
|
+
set errors(errors: Record<string, string>);
|
|
219
|
+
setGlobalError(key: string, message: string): void;
|
|
220
|
+
clearGlobalErrors(): void;
|
|
221
|
+
reset(): void;
|
|
222
|
+
get isTouched(): boolean;
|
|
223
|
+
get isValid(): boolean;
|
|
224
|
+
toFormData(): FormData;
|
|
225
|
+
toJSON(): Record<string, any>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
declare class BooleanType extends FieldType<boolean, BooleanOptions> {
|
|
229
|
+
cast(rawValue: any): boolean | null;
|
|
230
|
+
validate(value: boolean): string[];
|
|
231
|
+
getTypeName(): string;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
declare function getDateTime(): typeof DateTime;
|
|
235
|
+
declare function initLuxon(): Promise<typeof DateTime>;
|
|
236
|
+
declare function isLuxonAvailable(): boolean;
|
|
237
|
+
declare function resetLuxon(): void;
|
|
238
|
+
|
|
239
|
+
export { type ArrayFieldOptions, type ArrayFormFieldOptions, BasePlugin, type BooleanOptions, BooleanType, type DateLuxonOptions, type DateOptions, type DecimalOptions, type FieldOptions, type FieldPlugin, type MaskPluginOptions, type NumberOptions, type PluginOptions, type StringOptions, getDateTime, initLuxon, isLuxonAvailable, type messagesArrayFieldOptions, type messagesArrayFormFieldOptions, type messagesBooleanOptions, type messagesDateLuxonOptions, type messagesDateOptions, type messagesDecimalOptions, type messagesNumberOptions, type messagesOptions, type messagesStringOptions, resetLuxon };
|
package/dist/index.mjs
CHANGED
|
@@ -1,280 +1,111 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
constructor(t, e = {}) {
|
|
9
|
-
this._value = t ?? null, this.defaultValue = t ?? null, this.isValid = t === null ? null : !0, this.isRequired = e.isRequired ?? !0, this.validators = e.validators ?? [];
|
|
1
|
+
// src/core/plugins/FieldPlugin.ts
|
|
2
|
+
var BasePlugin = class {
|
|
3
|
+
constructor(pluginOptions = {}) {
|
|
4
|
+
this.pluginOptions = {
|
|
5
|
+
priority: 0,
|
|
6
|
+
...pluginOptions
|
|
7
|
+
};
|
|
10
8
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this._error = t;
|
|
22
|
-
}
|
|
23
|
-
set __valid_error(t) {
|
|
24
|
-
const [e = "", i = ""] = (this.error || "").split(" | ");
|
|
25
|
-
t ? e || (this.error = i ? `${t} | ${this.error}` : t) : this.error = i || null;
|
|
26
|
-
}
|
|
27
|
-
reset() {
|
|
28
|
-
this._value = this.defaultValue ?? null, this._error = null, this.isValid = this.defaultValue === null ? null : !0;
|
|
29
|
-
}
|
|
30
|
-
normalizeValue(t) {
|
|
31
|
-
return t;
|
|
32
|
-
}
|
|
33
|
-
fieldIsEmpty(t) {
|
|
34
|
-
return t == null || t === "" ? !0 : typeof t == "object" && Object.keys(t).length === 0;
|
|
35
|
-
}
|
|
36
|
-
get isEmpty() {
|
|
37
|
-
return this.fieldIsEmpty(this._value);
|
|
38
|
-
}
|
|
39
|
-
fieldIsZero(t) {
|
|
40
|
-
return t === 0 || t === "0";
|
|
41
|
-
}
|
|
42
|
-
get isZero() {
|
|
43
|
-
return this.fieldIsZero(this._value);
|
|
44
|
-
}
|
|
45
|
-
checkValid() {
|
|
46
|
-
if (this.isEmpty && !this.isZero)
|
|
47
|
-
return this.isValid = !this.isRequired, this.isValid;
|
|
48
|
-
this.isValid = !0;
|
|
49
|
-
for (const t of this.validators) {
|
|
50
|
-
const e = t(this._value);
|
|
51
|
-
if (e)
|
|
52
|
-
return this._error = e, this.isValid = !1, !1;
|
|
53
|
-
}
|
|
54
|
-
return this._error = null, this.isValid;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
class x extends l {
|
|
58
|
-
minLength;
|
|
59
|
-
maxLength;
|
|
60
|
-
regex;
|
|
61
|
-
example;
|
|
62
|
-
mask;
|
|
63
|
-
returnWithMask;
|
|
64
|
-
constructor(t = "", e = {}) {
|
|
65
|
-
super(t, e), this.minLength = e.minLength ?? null, this.maxLength = e.maxLength ?? null, this.regex = e.regex instanceof RegExp ? e.regex : new RegExp(e.regex || ".*"), this.example = e.example ?? "Enter a valid string", this.mask = e.mask ?? null, this.returnWithMask = e.returnWithMask ?? !1, this.validators.push(this.minLengthValidator.bind(this)), this.validators.push(this.maxLengthValidator.bind(this)), this.validators.push(this.regexValidator.bind(this));
|
|
66
|
-
}
|
|
67
|
-
normalizeValue(t) {
|
|
68
|
-
return this.fieldIsEmpty(t) ? "" : String(t);
|
|
69
|
-
}
|
|
70
|
-
get valueClear() {
|
|
71
|
-
if (this.returnWithMask)
|
|
72
|
-
return this._value === "" ? null : this._value;
|
|
73
|
-
if (!this.mask) return this._value || null;
|
|
74
|
-
const t = this.mask.split(/_/g).join("");
|
|
75
|
-
return (this._value || "").replace(new RegExp(`[${t}]`, "g"), "");
|
|
76
|
-
}
|
|
77
|
-
get value() {
|
|
78
|
-
return this._value;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/core/types/FieldType.ts
|
|
12
|
+
var FieldType = class {
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.options = {
|
|
15
|
+
isRequired: true,
|
|
16
|
+
messages: {},
|
|
17
|
+
...options
|
|
18
|
+
};
|
|
79
19
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
return;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/core/types/BooleanType.ts
|
|
23
|
+
var BooleanType = class extends FieldType {
|
|
24
|
+
cast(rawValue) {
|
|
25
|
+
if (rawValue === null || rawValue === void 0) return null;
|
|
26
|
+
if (typeof rawValue === "boolean") return rawValue;
|
|
27
|
+
if (typeof rawValue === "string") {
|
|
28
|
+
if (rawValue === "true" || rawValue === "1") return true;
|
|
29
|
+
if (rawValue === "false" || rawValue === "0") return false;
|
|
89
30
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
minLengthValidator(t) {
|
|
95
|
-
return this.minLength === null || !t ? null : t.length < this.minLength ? `Minimum ${this.minLength} characters` : null;
|
|
96
|
-
}
|
|
97
|
-
maxLengthValidator(t) {
|
|
98
|
-
return this.maxLength === null || !t ? null : t.length > this.maxLength ? `Maximum ${this.maxLength} characters` : null;
|
|
99
|
-
}
|
|
100
|
-
regexValidator(t) {
|
|
101
|
-
return t ? this.regex.test(t) ? null : this.example ? `Example: ${this.example}` : "Invalid format" : null;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
class p extends l {
|
|
105
|
-
min;
|
|
106
|
-
max;
|
|
107
|
-
example;
|
|
108
|
-
constructor(t = null, e = {}) {
|
|
109
|
-
super(t, e), this.min = e.min ?? null, this.max = e.max ?? null, this.example = e.example ?? "Enter a valid number", this.validators.push(this.typeValidator.bind(this)), this.validators.push(this.minValidator.bind(this)), this.validators.push(this.maxValidator.bind(this));
|
|
110
|
-
}
|
|
111
|
-
normalizeValue(t) {
|
|
112
|
-
if (this.fieldIsEmpty(t)) return null;
|
|
113
|
-
if (typeof t == "number") return isNaN(t) ? null : t;
|
|
114
|
-
if (typeof t == "string") {
|
|
115
|
-
const e = t.trim();
|
|
116
|
-
if (e === "") return null;
|
|
117
|
-
const i = Number(e);
|
|
118
|
-
return isNaN(i) ? null : i;
|
|
31
|
+
if (typeof rawValue === "number") {
|
|
32
|
+
if (rawValue === 1) return true;
|
|
33
|
+
if (rawValue === 0) return false;
|
|
119
34
|
}
|
|
120
35
|
return null;
|
|
121
36
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return t === null ? this.isRequired ? `Example: ${this.example}` : null : typeof t != "number" || isNaN(t) ? this.example ? `Example: ${this.example}` : "Invalid number" : null;
|
|
127
|
-
}
|
|
128
|
-
minValidator(t) {
|
|
129
|
-
return t === null || this.min === null ? null : t < this.min ? `Minimum value: ${this.min}` : null;
|
|
130
|
-
}
|
|
131
|
-
maxValidator(t) {
|
|
132
|
-
return t === null || this.max === null ? null : t > this.max ? `Maximum value: ${this.max}` : null;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
class _ extends l {
|
|
136
|
-
mode;
|
|
137
|
-
min;
|
|
138
|
-
max;
|
|
139
|
-
example;
|
|
140
|
-
format;
|
|
141
|
-
constructor(t = null, e = {}) {
|
|
142
|
-
const i = t instanceof Date ? t : t ? new Date(t) : null;
|
|
143
|
-
super(i, e), this.mode = e.mode ?? "datetime", this.min = e.min ? new Date(e.min) : null, this.max = e.max ? new Date(e.max) : null, this.example = e.example ?? (this.mode === "date" ? "YYYY-MM-DD" : "YYYY-MM-DD HH:mm"), this.format = e.format ?? null, this.validators.push(this.typeValidator.bind(this)), this.validators.push(this.minValidator.bind(this)), this.validators.push(this.maxValidator.bind(this));
|
|
144
|
-
}
|
|
145
|
-
normalizeValue(t) {
|
|
146
|
-
if (this.fieldIsEmpty(t)) return null;
|
|
147
|
-
if (t instanceof Date) return isNaN(t.getTime()) ? null : t;
|
|
148
|
-
if (typeof t == "string" && this.format)
|
|
149
|
-
return this.parseByFormat(t, this.format);
|
|
150
|
-
if (typeof t == "string" || typeof t == "number") {
|
|
151
|
-
const e = new Date(t);
|
|
152
|
-
return isNaN(e.getTime()) ? null : e;
|
|
37
|
+
validate(value) {
|
|
38
|
+
const errors = [];
|
|
39
|
+
if (this.options.mustBeTrue && value !== true) {
|
|
40
|
+
errors.push(this.options.messages?.mustBeTrue ?? "\u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 \u0434\u043E\u043B\u0436\u043D\u043E \u0431\u044B\u0442\u044C true");
|
|
153
41
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
parseByFormat(t, e) {
|
|
157
|
-
const i = e.match(/[^A-Z]/gi);
|
|
158
|
-
if (!i) return null;
|
|
159
|
-
const r = t.split(new RegExp(`[${i.join("")}]`)), s = e.split(new RegExp("[^A-Z]+"));
|
|
160
|
-
if (r.length !== s.length) return null;
|
|
161
|
-
let n = 0, u = 0, m = 0, d = 0, c = 0, f = 0;
|
|
162
|
-
for (let o = 0; o < s.length; o++) {
|
|
163
|
-
const a = parseInt(r[o], 10);
|
|
164
|
-
if (isNaN(a)) return null;
|
|
165
|
-
switch (s[o]) {
|
|
166
|
-
case "YYYY":
|
|
167
|
-
n = a;
|
|
168
|
-
break;
|
|
169
|
-
case "MM":
|
|
170
|
-
u = a - 1;
|
|
171
|
-
break;
|
|
172
|
-
case "DD":
|
|
173
|
-
m = a;
|
|
174
|
-
break;
|
|
175
|
-
case "HH":
|
|
176
|
-
d = a;
|
|
177
|
-
break;
|
|
178
|
-
case "mm":
|
|
179
|
-
c = a;
|
|
180
|
-
break;
|
|
181
|
-
case "ss":
|
|
182
|
-
f = a;
|
|
183
|
-
break;
|
|
184
|
-
}
|
|
42
|
+
if (this.options.mustBeFalse && value !== false) {
|
|
43
|
+
errors.push(this.options.messages?.mustBeFalse ?? "\u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 \u0434\u043E\u043B\u0436\u043D\u043E \u0431\u044B\u0442\u044C false");
|
|
185
44
|
}
|
|
186
|
-
|
|
187
|
-
return isNaN(g.getTime()) ? null : g;
|
|
188
|
-
}
|
|
189
|
-
get valueClear() {
|
|
190
|
-
if (!this._value) return null;
|
|
191
|
-
const t = (s) => String(s).padStart(2, "0"), e = this._value.getFullYear(), i = t(this._value.getMonth() + 1), r = t(this._value.getDate());
|
|
192
|
-
return this.mode === "date" ? `${e}-${i}-${r}` : this._value.toISOString();
|
|
193
|
-
}
|
|
194
|
-
typeValidator(t) {
|
|
195
|
-
return t === null ? this.isRequired ? `Example: ${this.example}` : null : !(t instanceof Date) || isNaN(t.getTime()) ? this.example ? `Example: ${this.example}` : "Invalid date format" : null;
|
|
45
|
+
return errors;
|
|
196
46
|
}
|
|
197
|
-
|
|
198
|
-
return
|
|
47
|
+
getTypeName() {
|
|
48
|
+
return "\u043B\u043E\u0433\u0438\u0447\u0435\u0441\u043A\u043E\u0435 \u0437\u043D\u0430\u0447\u0435\u043D\u0438\u0435";
|
|
199
49
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// src/core/utils/luxon-loader.ts
|
|
53
|
+
var DateTime = null;
|
|
54
|
+
var isLoading = false;
|
|
55
|
+
var loadError = null;
|
|
56
|
+
var loadPromise = null;
|
|
57
|
+
function getDateTime() {
|
|
58
|
+
if (DateTime) {
|
|
59
|
+
return DateTime;
|
|
60
|
+
}
|
|
61
|
+
if (loadError) {
|
|
62
|
+
throw loadError;
|
|
63
|
+
}
|
|
64
|
+
if (isLoading) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
"Luxon is still loading. Please await initLuxon() first."
|
|
214
67
|
);
|
|
215
68
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
set errors(t) {
|
|
242
|
-
for (const e in t) {
|
|
243
|
-
const i = this[e];
|
|
244
|
-
i instanceof l ? i.error = t[e] : this._errors[e] = t[e];
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
set fields(t) {
|
|
248
|
-
for (const e in t) {
|
|
249
|
-
if (!this.declaredFields.includes(e))
|
|
250
|
-
throw new Error(
|
|
251
|
-
`Field "${e}" is not declared in form "${this.constructor.name}"`
|
|
252
|
-
);
|
|
253
|
-
const i = this[e], r = t[e];
|
|
254
|
-
i instanceof l ? i.value = r : this[e] = r;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
setFieldValue(t, e) {
|
|
258
|
-
const i = this[t];
|
|
259
|
-
i instanceof l ? i.value = e : this[t] = e;
|
|
260
|
-
}
|
|
261
|
-
getFieldValue(t) {
|
|
262
|
-
const e = this[t];
|
|
263
|
-
return e instanceof l ? e.valueClear : e;
|
|
264
|
-
}
|
|
265
|
-
reset() {
|
|
266
|
-
for (const t of this.declaredFields) {
|
|
267
|
-
const e = this[t];
|
|
268
|
-
e instanceof l && e.reset();
|
|
69
|
+
throw new Error(
|
|
70
|
+
"Luxon is not initialized. Call initLuxon() first or install: npm install luxon"
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
function initLuxon() {
|
|
74
|
+
if (DateTime) {
|
|
75
|
+
return Promise.resolve(DateTime);
|
|
76
|
+
}
|
|
77
|
+
if (loadPromise) {
|
|
78
|
+
return loadPromise;
|
|
79
|
+
}
|
|
80
|
+
isLoading = true;
|
|
81
|
+
loadPromise = (async () => {
|
|
82
|
+
try {
|
|
83
|
+
const luxon = await import('luxon');
|
|
84
|
+
DateTime = luxon.DateTime;
|
|
85
|
+
return DateTime;
|
|
86
|
+
} catch (error) {
|
|
87
|
+
loadError = new Error(
|
|
88
|
+
'DateLuxonType requires the "luxon" package to be installed.\nPlease install it by running:\n\n npm install luxon\n # or\n yarn add luxon\n # or\n pnpm add luxon\n'
|
|
89
|
+
);
|
|
90
|
+
throw loadError;
|
|
91
|
+
} finally {
|
|
92
|
+
isLoading = false;
|
|
269
93
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
94
|
+
})();
|
|
95
|
+
return loadPromise;
|
|
273
96
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
97
|
+
function isLuxonAvailable() {
|
|
98
|
+
return DateTime !== null;
|
|
99
|
+
}
|
|
100
|
+
function resetLuxon() {
|
|
101
|
+
DateTime = null;
|
|
102
|
+
isLoading = false;
|
|
103
|
+
loadError = null;
|
|
104
|
+
loadPromise = null;
|
|
105
|
+
}
|
|
106
|
+
initLuxon().catch(() => {
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
export { BasePlugin, BooleanType, getDateTime, initLuxon, isLuxonAvailable, resetLuxon };
|
|
110
|
+
//# sourceMappingURL=index.mjs.map
|
|
111
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/plugins/FieldPlugin.ts","../src/core/types/FieldType.ts","../src/core/types/BooleanType.ts","../src/core/utils/luxon-loader.ts"],"names":[],"mappings":";AAaO,IAAe,aAAf,MAA6D;AAAA,EAGlE,WAAA,CAAY,aAAA,GAA+B,EAAC,EAAG;AAC7C,IAAA,IAAA,CAAK,aAAA,GAAgB;AAAA,MACnB,QAAA,EAAU,CAAA;AAAA,MACV,GAAG;AAAA,KACL;AAAA,EACF;AACF;;;AClBA,IAA8B,YAA9B,MAAkF;AAAA,EAGhF,YAAY,OAAA,EAAY;AACtB,IAAA,IAAA,CAAK,OAAA,GAAU;AAAA,MACb,UAAA,EAAY,IAAA;AAAA,MACZ,UAAU,EAAC;AAAA,MACX,GAAG;AAAA,KACL;AAAA,EACF;AAOF,CAAA;;;ACjBO,IAAM,WAAA,GAAN,cAA0B,SAAA,CAAm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\"@core/options\";\nimport Field from \"@core/fields/Field\";\n\nexport interface FieldPlugin<T = any> {\n pluginOptions?: PluginOptions\n\n init?(field: Field<T>): void\n\n toRawValue?(value: any, field: Field<T>): any\n\n toValueClear?(value: any, field: Field<T>): any\n}\n\nexport abstract class BasePlugin<T = any> implements FieldPlugin<T> {\n pluginOptions: PluginOptions\n\n constructor(pluginOptions: PluginOptions = {}) {\n this.pluginOptions = {\n priority: 0,\n ...pluginOptions\n }\n }\n}","import {FieldOptions} from \"@core/options\";\nimport Form from \"@core/Form\";\n\n\nexport default abstract class FieldType<T, O extends FieldOptions = FieldOptions> {\n options: O\n\n constructor(options: O) {\n this.options = {\n isRequired: true,\n messages: {},\n ...options\n } as O\n }\n\n abstract cast(rawValue: any): T | null\n\n abstract validate(value: T, form?: Form): string[]\n\n abstract getTypeName(): string\n}","import FieldType from \"@core/types/FieldType\";\nimport {BooleanOptions} from \"@core/options\";\n\nexport class BooleanType extends FieldType<boolean, BooleanOptions> {\n cast(rawValue: any): boolean | null {\n if (rawValue === null || rawValue === undefined) return null\n if (typeof rawValue === 'boolean') return rawValue\n\n if (typeof rawValue === 'string') {\n if (rawValue === 'true' || rawValue === '1') return true\n if (rawValue === 'false' || rawValue === '0') return false\n }\n\n if (typeof rawValue === 'number') {\n if (rawValue === 1) return true\n if (rawValue === 0) return false\n }\n\n return null\n }\n\n validate(value: boolean): string[] {\n const errors: string[] = []\n\n if (this.options.mustBeTrue && value !== true) {\n errors.push(this.options.messages?.mustBeTrue ?? 'Значение должно быть true')\n }\n\n if (this.options.mustBeFalse && value !== false) {\n errors.push(this.options.messages?.mustBeFalse ?? 'Значение должно быть false')\n }\n\n return errors\n }\n\n getTypeName(): string {\n return 'логическое значение'\n }\n}","import type { DateTime as LuxonDateTime } from 'luxon';\n\nlet DateTime: typeof LuxonDateTime | null = null;\nlet isLoading = false;\nlet loadError: Error | null = null;\nlet loadPromise: Promise<typeof LuxonDateTime> | null = null;\n\nexport function getDateTime(): typeof LuxonDateTime {\n if (DateTime) {\n return DateTime;\n }\n\n if (loadError) {\n throw loadError;\n }\n\n if (isLoading) {\n throw new Error(\n 'Luxon is still loading. Please await initLuxon() first.'\n );\n }\n\n throw new Error(\n 'Luxon is not initialized. Call initLuxon() first or install: npm install luxon'\n );\n}\n\nexport function initLuxon(): Promise<typeof LuxonDateTime> {\n if (DateTime) {\n return Promise.resolve(DateTime);\n }\n\n if (loadPromise) {\n return loadPromise;\n }\n\n isLoading = true;\n loadPromise = (async () => {\n try {\n // Пробуем загрузить через ESM (работает везде)\n const luxon = await import('luxon');\n DateTime = luxon.DateTime;\n return DateTime!;\n } catch (error) {\n loadError = new Error(\n 'DateLuxonType requires the \"luxon\" package to be installed.\\n' +\n 'Please install it by running:\\n\\n' +\n ' npm install luxon\\n' +\n ' # or\\n' +\n ' yarn add luxon\\n' +\n ' # or\\n' +\n ' pnpm add luxon\\n'\n );\n throw loadError;\n } finally {\n isLoading = false;\n }\n })();\n\n return loadPromise;\n}\n\nexport function isLuxonAvailable(): boolean {\n return DateTime !== null;\n}\n\nexport function resetLuxon(): void {\n DateTime = null;\n isLoading = false;\n loadError = null;\n loadPromise = null;\n}\n\ninitLuxon().catch(() => {\n});\n\nexport type { LuxonDateTime };"]}
|
package/package.json
CHANGED
|
@@ -1,43 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adaptform",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Library of adaptive forms of validation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"form",
|
|
7
7
|
"validation",
|
|
8
8
|
"typescript",
|
|
9
|
-
"
|
|
9
|
+
"pydantic",
|
|
10
|
+
"luxon"
|
|
10
11
|
],
|
|
11
12
|
"author": {
|
|
12
13
|
"name": "Stanislav Orlov",
|
|
13
|
-
"
|
|
14
|
-
"email": "stanislav1426@gmail.com",
|
|
14
|
+
"email": "stasioniks@gmail.com",
|
|
15
15
|
"url": "https://github.com/ArhiMEN"
|
|
16
16
|
},
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"type": "module",
|
|
19
|
-
"main": "dist/index.
|
|
20
|
-
"module": "dist/index.mjs",
|
|
21
|
-
"types": "dist/index.d.ts",
|
|
19
|
+
"main": "./dist/index.cjs",
|
|
20
|
+
"module": "./dist/index.mjs",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|
|
24
|
+
"types": {
|
|
25
|
+
"import": "./dist/index.d.ts",
|
|
26
|
+
"require": "./dist/index.d.cts"
|
|
27
|
+
},
|
|
24
28
|
"import": "./dist/index.mjs",
|
|
25
|
-
"
|
|
26
|
-
}
|
|
27
|
-
"./*": "./dist/*"
|
|
29
|
+
"require": "./dist/index.cjs"
|
|
30
|
+
}
|
|
28
31
|
},
|
|
29
32
|
"files": [
|
|
30
|
-
"dist
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE.md",
|
|
36
|
+
"CHANGELOG.md"
|
|
31
37
|
],
|
|
32
38
|
"sideEffects": false,
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/ArhiMEN/adaptform.git"
|
|
42
|
+
},
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/ArhiMEN/adaptform/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/ArhiMEN/adaptform#readme",
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=16.0.0"
|
|
49
|
+
},
|
|
33
50
|
"scripts": {
|
|
34
51
|
"dev": "vite",
|
|
35
|
-
"build": "
|
|
52
|
+
"build": "tsup",
|
|
53
|
+
"test": "vitest",
|
|
54
|
+
"lint": "eslint src --ext .ts",
|
|
36
55
|
"prepublishOnly": "npm run build"
|
|
37
56
|
},
|
|
38
57
|
"devDependencies": {
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
58
|
+
"@types/luxon": "^3.5.0",
|
|
59
|
+
"@types/node": "^20.0.0",
|
|
60
|
+
"luxon": "^3.5.0",
|
|
61
|
+
"tsup": "^8.0.0",
|
|
62
|
+
"typescript": "^5.0.0",
|
|
63
|
+
"vitest": "^1.0.0"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"luxon": ">=3.5.0"
|
|
67
|
+
},
|
|
68
|
+
"peerDependenciesMeta": {
|
|
69
|
+
"luxon": {
|
|
70
|
+
"optional": true
|
|
71
|
+
}
|
|
42
72
|
}
|
|
43
|
-
}
|
|
73
|
+
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export interface FormFieldOptions {
|
|
2
|
-
isRequired?: boolean;
|
|
3
|
-
validators?: Array<(value: any) => string | null>;
|
|
4
|
-
}
|
|
5
|
-
export interface StringFieldOptions extends FormFieldOptions {
|
|
6
|
-
minLength?: number;
|
|
7
|
-
maxLength?: number;
|
|
8
|
-
regex?: RegExp | string;
|
|
9
|
-
mask?: string;
|
|
10
|
-
example?: string;
|
|
11
|
-
returnWithMask?: boolean;
|
|
12
|
-
}
|
|
13
|
-
export interface NumberFieldOptions extends FormFieldOptions {
|
|
14
|
-
min?: number;
|
|
15
|
-
max?: number;
|
|
16
|
-
example?: string;
|
|
17
|
-
}
|
|
18
|
-
export interface DateTimeFieldOptions extends FormFieldOptions {
|
|
19
|
-
mode?: "date" | "datetime";
|
|
20
|
-
min?: Date | string | number;
|
|
21
|
-
max?: Date | string | number;
|
|
22
|
-
example?: string;
|
|
23
|
-
format?: string;
|
|
24
|
-
}
|
package/dist/core/Form.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare abstract class Form<TFields extends Record<string, any> = any> {
|
|
2
|
-
private readonly _errors;
|
|
3
|
-
protected get declaredFields(): (keyof TFields)[];
|
|
4
|
-
checkValid(): boolean;
|
|
5
|
-
get valueObject(): Record<keyof TFields, any>;
|
|
6
|
-
get valueArray(): any[];
|
|
7
|
-
get errors(): Record<string, any>;
|
|
8
|
-
set errors(errors: Record<string, any>);
|
|
9
|
-
set fields(values: Partial<Record<keyof TFields, any>>);
|
|
10
|
-
setFieldValue<K extends keyof TFields>(key: K, value: any): void;
|
|
11
|
-
getFieldValue<K extends keyof TFields>(key: K): any;
|
|
12
|
-
reset(): this;
|
|
13
|
-
}
|
package/dist/core/FormField.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { FormFieldOptions } from './FieldOptions';
|
|
2
|
-
export declare abstract class FormField<Value = any, ClearValue = Value> {
|
|
3
|
-
protected _value: Value | null;
|
|
4
|
-
protected defaultValue: Value | null;
|
|
5
|
-
protected _error: string | null;
|
|
6
|
-
isRequired: boolean;
|
|
7
|
-
isValid: boolean | null;
|
|
8
|
-
validators: Array<(value: Value | null) => string | null>;
|
|
9
|
-
protected constructor(defaultValue?: Value | null, options?: FormFieldOptions);
|
|
10
|
-
get value(): Value | null;
|
|
11
|
-
set value(value: Value | null);
|
|
12
|
-
get error(): string;
|
|
13
|
-
set error(value: string | null);
|
|
14
|
-
set __valid_error(value: string | null);
|
|
15
|
-
reset(): void;
|
|
16
|
-
protected normalizeValue(value: Value | null): Value | null;
|
|
17
|
-
protected fieldIsEmpty(value: Value | null): boolean;
|
|
18
|
-
get isEmpty(): boolean;
|
|
19
|
-
protected fieldIsZero(value: Value | null): boolean;
|
|
20
|
-
get isZero(): boolean;
|
|
21
|
-
checkValid(): boolean;
|
|
22
|
-
abstract get valueClear(): ClearValue | null;
|
|
23
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { FormField } from '../FormField';
|
|
2
|
-
import { DateTimeFieldOptions } from '../FieldOptions';
|
|
3
|
-
type DateValue = Date | string | number | null;
|
|
4
|
-
export declare class DateTimeField extends FormField<Date, string> {
|
|
5
|
-
mode: "date" | "datetime";
|
|
6
|
-
min: Date | null;
|
|
7
|
-
max: Date | null;
|
|
8
|
-
example: string;
|
|
9
|
-
format: string | null;
|
|
10
|
-
constructor(defaultValue?: DateValue, options?: DateTimeFieldOptions);
|
|
11
|
-
protected normalizeValue(value: any): Date | null;
|
|
12
|
-
protected parseByFormat(value: string, format: string): Date | null;
|
|
13
|
-
get valueClear(): string | null;
|
|
14
|
-
private typeValidator;
|
|
15
|
-
private minValidator;
|
|
16
|
-
/** 🔹 Максимальная дата */
|
|
17
|
-
private maxValidator;
|
|
18
|
-
private formatDate;
|
|
19
|
-
}
|
|
20
|
-
export {};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { FormField } from '../FormField';
|
|
2
|
-
import { NumberFieldOptions } from '../FieldOptions';
|
|
3
|
-
export declare class NumberField extends FormField<number> {
|
|
4
|
-
min: number | null;
|
|
5
|
-
max: number | null;
|
|
6
|
-
example: string;
|
|
7
|
-
constructor(defaultValue?: number | null, options?: NumberFieldOptions);
|
|
8
|
-
protected normalizeValue(value: any): number | null;
|
|
9
|
-
get valueClear(): number | null;
|
|
10
|
-
private typeValidator;
|
|
11
|
-
private minValidator;
|
|
12
|
-
private maxValidator;
|
|
13
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { FormField } from '../FormField';
|
|
2
|
-
import { StringFieldOptions } from '../FieldOptions';
|
|
3
|
-
export declare class StringField extends FormField<string> {
|
|
4
|
-
minLength: number | null;
|
|
5
|
-
maxLength: number | null;
|
|
6
|
-
regex: RegExp;
|
|
7
|
-
example: string;
|
|
8
|
-
mask: string | null;
|
|
9
|
-
returnWithMask: boolean;
|
|
10
|
-
constructor(defaultValue?: string, options?: StringFieldOptions);
|
|
11
|
-
protected normalizeValue(value: string | null): string | null;
|
|
12
|
-
get valueClear(): string | null;
|
|
13
|
-
get value(): string | null;
|
|
14
|
-
set value(value: string | null);
|
|
15
|
-
private minLengthValidator;
|
|
16
|
-
private maxLengthValidator;
|
|
17
|
-
private regexValidator;
|
|
18
|
-
}
|