adaptform 1.1.4 → 1.1.5
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/core/FieldOptions.d.ts +23 -0
- package/dist/core/Form.d.ts +13 -0
- package/dist/core/FormField.d.ts +23 -0
- package/dist/core/date/DateTimeField.d.ts +18 -0
- package/dist/core/number/NumberField.d.ts +13 -0
- package/dist/core/string/StringField.d.ts +18 -0
- package/dist/index.d.ts +6 -131
- package/dist/{index.es.js → index.mjs} +70 -82
- package/package.json +10 -14
- package/dist/index.cjs.js +0 -1
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
constructor(defaultValue?: DateValue, options?: DateTimeFieldOptions);
|
|
10
|
+
protected normalizeValue(value: any): Date | null;
|
|
11
|
+
get valueClear(): string | null;
|
|
12
|
+
private typeValidator;
|
|
13
|
+
private minValidator;
|
|
14
|
+
/** 🔹 Максимальная дата */
|
|
15
|
+
private maxValidator;
|
|
16
|
+
private formatDate;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,131 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
FieldOptions: typeof FieldOptions;
|
|
8
|
-
};
|
|
9
|
-
export default AdaptForm;
|
|
10
|
-
|
|
11
|
-
export declare class DateTimeField extends FormField<Date, string> {
|
|
12
|
-
mode: "date" | "datetime";
|
|
13
|
-
min: Date | null;
|
|
14
|
-
max: Date | null;
|
|
15
|
-
example: string;
|
|
16
|
-
constructor(defaultValue?: DateValue, options?: DateTimeFieldOptions);
|
|
17
|
-
protected normalizeValue(value: any): Date | null;
|
|
18
|
-
get valueClear(): string | null;
|
|
19
|
-
private typeValidator;
|
|
20
|
-
private minValidator;
|
|
21
|
-
/** 🔹 Максимальная дата */
|
|
22
|
-
private maxValidator;
|
|
23
|
-
private formatDate;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
declare interface DateTimeFieldOptions extends FormFieldOptions {
|
|
27
|
-
mode?: "date" | "datetime";
|
|
28
|
-
min?: Date | string | number;
|
|
29
|
-
max?: Date | string | number;
|
|
30
|
-
example?: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
declare type DateValue = Date | string | number | null;
|
|
34
|
-
|
|
35
|
-
declare namespace FieldOptions {
|
|
36
|
-
export {
|
|
37
|
-
FormFieldOptions,
|
|
38
|
-
StringFieldOptions,
|
|
39
|
-
NumberFieldOptions,
|
|
40
|
-
DateTimeFieldOptions
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
export { FieldOptions }
|
|
44
|
-
|
|
45
|
-
export declare abstract class Form<TFields extends Record<string, any> = any> {
|
|
46
|
-
private readonly _errors;
|
|
47
|
-
protected get declaredFields(): (keyof TFields)[];
|
|
48
|
-
checkValid(): boolean;
|
|
49
|
-
get valueObject(): Record<keyof TFields, any>;
|
|
50
|
-
get valueArray(): any[];
|
|
51
|
-
get errors(): Record<string, any>;
|
|
52
|
-
set errors(errors: Record<string, any>);
|
|
53
|
-
set fields(values: Partial<Record<keyof TFields, any>>);
|
|
54
|
-
setFieldValue<K extends keyof TFields>(key: K, value: any): void;
|
|
55
|
-
getFieldValue<K extends keyof TFields>(key: K): any;
|
|
56
|
-
reset(): this;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export declare abstract class FormField<Value = any, ClearValue = Value> {
|
|
60
|
-
protected _value: Value | null;
|
|
61
|
-
protected defaultValue: Value | null;
|
|
62
|
-
protected _error: string | null;
|
|
63
|
-
isRequired: boolean;
|
|
64
|
-
isValid: boolean | null;
|
|
65
|
-
validators: Array<(value: Value | null) => string | null>;
|
|
66
|
-
protected constructor(defaultValue?: Value | null, options?: FormFieldOptions);
|
|
67
|
-
get value(): Value | null;
|
|
68
|
-
set value(value: Value | null);
|
|
69
|
-
get error(): string;
|
|
70
|
-
set error(value: string | null);
|
|
71
|
-
set __valid_error(value: string | null);
|
|
72
|
-
reset(): void;
|
|
73
|
-
protected normalizeValue(value: Value | null): Value | null;
|
|
74
|
-
protected fieldIsEmpty(value: Value | null): boolean;
|
|
75
|
-
get isEmpty(): boolean;
|
|
76
|
-
protected fieldIsZero(value: Value | null): boolean;
|
|
77
|
-
get isZero(): boolean;
|
|
78
|
-
checkValid(): boolean;
|
|
79
|
-
abstract get valueClear(): ClearValue | null;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
declare interface FormFieldOptions {
|
|
83
|
-
isRequired?: boolean;
|
|
84
|
-
validators?: Array<(value: any) => string | null>;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export declare class NumberField extends FormField<number> {
|
|
88
|
-
min: number | null;
|
|
89
|
-
max: number | null;
|
|
90
|
-
example: string;
|
|
91
|
-
constructor(defaultValue?: number | null, options?: NumberFieldOptions);
|
|
92
|
-
protected normalizeValue(value: any): number | null;
|
|
93
|
-
get valueClear(): number | null;
|
|
94
|
-
private typeValidator;
|
|
95
|
-
private minValidator;
|
|
96
|
-
private maxValidator;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
declare interface NumberFieldOptions extends FormFieldOptions {
|
|
100
|
-
min?: number;
|
|
101
|
-
max?: number;
|
|
102
|
-
example?: string;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export declare class StringField extends FormField<string> {
|
|
106
|
-
minLength: number | null;
|
|
107
|
-
maxLength: number | null;
|
|
108
|
-
regex: RegExp;
|
|
109
|
-
example: string;
|
|
110
|
-
mask: string | null;
|
|
111
|
-
returnWithMask: boolean;
|
|
112
|
-
constructor(defaultValue?: string, options?: StringFieldOptions);
|
|
113
|
-
protected normalizeValue(value: string | null): string | null;
|
|
114
|
-
get valueClear(): string | null;
|
|
115
|
-
get value(): string | null;
|
|
116
|
-
set value(value: string | null);
|
|
117
|
-
private minLengthValidator;
|
|
118
|
-
private maxLengthValidator;
|
|
119
|
-
private regexValidator;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
declare interface StringFieldOptions extends FormFieldOptions {
|
|
123
|
-
minLength?: number;
|
|
124
|
-
maxLength?: number;
|
|
125
|
-
regex?: RegExp | string;
|
|
126
|
-
mask?: string;
|
|
127
|
-
example?: string;
|
|
128
|
-
returnWithMask?: boolean;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export { }
|
|
1
|
+
export * from './core/FieldOptions';
|
|
2
|
+
export * from './core/FormField';
|
|
3
|
+
export * from './core/string/StringField';
|
|
4
|
+
export * from './core/number/NumberField';
|
|
5
|
+
export * from './core/date/DateTimeField';
|
|
6
|
+
export * from './core/Form';
|
|
@@ -54,72 +54,7 @@ class r {
|
|
|
54
54
|
return this._error = null, this.isValid;
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
class m {
|
|
58
|
-
_errors = {};
|
|
59
|
-
get declaredFields() {
|
|
60
|
-
return Object.keys(this).filter(
|
|
61
|
-
(t) => this[t] instanceof r
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
checkValid() {
|
|
65
|
-
let t = !0;
|
|
66
|
-
for (const e of this.declaredFields) {
|
|
67
|
-
const i = this[e];
|
|
68
|
-
i instanceof r && (i.checkValid(), (i.isValid === !1 || i.isValid === null && i.isRequired) && (t = !1));
|
|
69
|
-
}
|
|
70
|
-
return t;
|
|
71
|
-
}
|
|
72
|
-
get valueObject() {
|
|
73
|
-
const t = {};
|
|
74
|
-
for (const e of this.declaredFields) {
|
|
75
|
-
const i = this[e];
|
|
76
|
-
t[e] = i instanceof r ? i.valueClear : i;
|
|
77
|
-
}
|
|
78
|
-
return t;
|
|
79
|
-
}
|
|
80
|
-
get valueArray() {
|
|
81
|
-
return this.declaredFields.map((t) => {
|
|
82
|
-
const e = this[t];
|
|
83
|
-
return e instanceof r ? e.valueClear : e;
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
get errors() {
|
|
87
|
-
return this._errors;
|
|
88
|
-
}
|
|
89
|
-
set errors(t) {
|
|
90
|
-
for (const e in t) {
|
|
91
|
-
const i = this[e];
|
|
92
|
-
i instanceof r ? i.error = t[e] : this._errors[e] = t[e];
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
set fields(t) {
|
|
96
|
-
for (const e in t) {
|
|
97
|
-
if (!this.declaredFields.includes(e))
|
|
98
|
-
throw new Error(
|
|
99
|
-
`Field "${e}" is not declared in form "${this.constructor.name}"`
|
|
100
|
-
);
|
|
101
|
-
const i = this[e], s = t[e];
|
|
102
|
-
i instanceof r ? i.value = s : this[e] = s;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
setFieldValue(t, e) {
|
|
106
|
-
const i = this[t];
|
|
107
|
-
i instanceof r ? i.value = e : this[t] = e;
|
|
108
|
-
}
|
|
109
|
-
getFieldValue(t) {
|
|
110
|
-
const e = this[t];
|
|
111
|
-
return e instanceof r ? e.valueClear : e;
|
|
112
|
-
}
|
|
113
|
-
reset() {
|
|
114
|
-
for (const t of this.declaredFields) {
|
|
115
|
-
const e = this[t];
|
|
116
|
-
e instanceof r && e.reset();
|
|
117
|
-
}
|
|
118
|
-
for (const t of Object.keys(this._errors)) delete this._errors[t];
|
|
119
|
-
return this;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
class o extends r {
|
|
57
|
+
class m extends r {
|
|
123
58
|
minLength;
|
|
124
59
|
maxLength;
|
|
125
60
|
regex;
|
|
@@ -197,7 +132,7 @@ class d extends r {
|
|
|
197
132
|
return t === null || this.max === null ? null : t > this.max ? `Maximum value: ${this.max}` : null;
|
|
198
133
|
}
|
|
199
134
|
}
|
|
200
|
-
class
|
|
135
|
+
class o extends r {
|
|
201
136
|
mode;
|
|
202
137
|
min;
|
|
203
138
|
max;
|
|
@@ -235,22 +170,75 @@ class c extends r {
|
|
|
235
170
|
return this.mode === "date" ? `${i}-${s}-${l}` : `${i}-${s}-${l} ${a}:${h}`;
|
|
236
171
|
}
|
|
237
172
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
173
|
+
class c {
|
|
174
|
+
_errors = {};
|
|
175
|
+
get declaredFields() {
|
|
176
|
+
return Object.keys(this).filter(
|
|
177
|
+
(t) => this[t] instanceof r
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
checkValid() {
|
|
181
|
+
let t = !0;
|
|
182
|
+
for (const e of this.declaredFields) {
|
|
183
|
+
const i = this[e];
|
|
184
|
+
i instanceof r && (i.checkValid(), (i.isValid === !1 || i.isValid === null && i.isRequired) && (t = !1));
|
|
185
|
+
}
|
|
186
|
+
return t;
|
|
187
|
+
}
|
|
188
|
+
get valueObject() {
|
|
189
|
+
const t = {};
|
|
190
|
+
for (const e of this.declaredFields) {
|
|
191
|
+
const i = this[e];
|
|
192
|
+
t[e] = i instanceof r ? i.valueClear : i;
|
|
193
|
+
}
|
|
194
|
+
return t;
|
|
195
|
+
}
|
|
196
|
+
get valueArray() {
|
|
197
|
+
return this.declaredFields.map((t) => {
|
|
198
|
+
const e = this[t];
|
|
199
|
+
return e instanceof r ? e.valueClear : e;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
get errors() {
|
|
203
|
+
return this._errors;
|
|
204
|
+
}
|
|
205
|
+
set errors(t) {
|
|
206
|
+
for (const e in t) {
|
|
207
|
+
const i = this[e];
|
|
208
|
+
i instanceof r ? i.error = t[e] : this._errors[e] = t[e];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
set fields(t) {
|
|
212
|
+
for (const e in t) {
|
|
213
|
+
if (!this.declaredFields.includes(e))
|
|
214
|
+
throw new Error(
|
|
215
|
+
`Field "${e}" is not declared in form "${this.constructor.name}"`
|
|
216
|
+
);
|
|
217
|
+
const i = this[e], s = t[e];
|
|
218
|
+
i instanceof r ? i.value = s : this[e] = s;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
setFieldValue(t, e) {
|
|
222
|
+
const i = this[t];
|
|
223
|
+
i instanceof r ? i.value = e : this[t] = e;
|
|
224
|
+
}
|
|
225
|
+
getFieldValue(t) {
|
|
226
|
+
const e = this[t];
|
|
227
|
+
return e instanceof r ? e.valueClear : e;
|
|
228
|
+
}
|
|
229
|
+
reset() {
|
|
230
|
+
for (const t of this.declaredFields) {
|
|
231
|
+
const e = this[t];
|
|
232
|
+
e instanceof r && e.reset();
|
|
233
|
+
}
|
|
234
|
+
for (const t of Object.keys(this._errors)) delete this._errors[t];
|
|
235
|
+
return this;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
248
238
|
export {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
m as Form,
|
|
239
|
+
o as DateTimeField,
|
|
240
|
+
c as Form,
|
|
252
241
|
r as FormField,
|
|
253
242
|
d as NumberField,
|
|
254
|
-
|
|
255
|
-
g as default
|
|
243
|
+
m as StringField
|
|
256
244
|
};
|
package/package.json
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adaptform",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "Library of adaptive forms of validation",
|
|
3
|
+
"version": "1.1.5",
|
|
5
4
|
"type": "module",
|
|
6
|
-
"main": "
|
|
7
|
-
"module": "
|
|
8
|
-
"types": "
|
|
5
|
+
"main": "dist/index.mjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
9
8
|
"exports": {
|
|
10
9
|
".": {
|
|
11
|
-
"import": "./dist/index.
|
|
12
|
-
"
|
|
13
|
-
}
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./*": "./dist/*"
|
|
14
14
|
},
|
|
15
|
-
"files": [
|
|
16
|
-
|
|
17
|
-
],
|
|
15
|
+
"files": ["dist/**/*"],
|
|
16
|
+
"sideEffects": false,
|
|
18
17
|
"scripts": {
|
|
19
18
|
"dev": "vite",
|
|
20
19
|
"build": "vite build",
|
|
21
20
|
"prepublishOnly": "npm run build"
|
|
22
21
|
},
|
|
23
|
-
"keywords": ["form", "validation", "typescript"],
|
|
24
|
-
"author": "ArhiMEN",
|
|
25
|
-
"license": "MIT",
|
|
26
22
|
"devDependencies": {
|
|
27
23
|
"vite": "^7.1.1",
|
|
28
24
|
"vite-plugin-dts": "^4.0.0",
|
package/dist/index.cjs.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class r{_value;defaultValue;_error=null;isRequired;isValid;validators;constructor(e,t={}){this._value=e??null,this.defaultValue=e??null,this.isValid=e===null?null:!0,this.isRequired=t.isRequired??!0,this.validators=t.validators??[]}get value(){return this._value}set value(e){this._value=this.normalizeValue(e),this.checkValid()}get error(){return this._error??""}set error(e){this._error=e}set __valid_error(e){const[t="",i=""]=(this.error||"").split(" | ");e?t||(this.error=i?`${e} | ${this.error}`:e):this.error=i||null}reset(){this._value=this.defaultValue??null,this._error=null,this.isValid=this.defaultValue===null?null:!0}normalizeValue(e){return e}fieldIsEmpty(e){return e==null||e===""?!0:typeof e=="object"&&Object.keys(e).length===0}get isEmpty(){return this.fieldIsEmpty(this._value)}fieldIsZero(e){return e===0||e==="0"}get isZero(){return this.fieldIsZero(this._value)}checkValid(){if(this.isEmpty&&!this.isZero)return this.isValid=!this.isRequired,this.isValid;this.isValid=!0;for(const e of this.validators){const t=e(this._value);if(t)return this._error=t,this.isValid=!1,!1}return this._error=null,this.isValid}}class u{_errors={};get declaredFields(){return Object.keys(this).filter(e=>this[e]instanceof r)}checkValid(){let e=!0;for(const t of this.declaredFields){const i=this[t];i instanceof r&&(i.checkValid(),(i.isValid===!1||i.isValid===null&&i.isRequired)&&(e=!1))}return e}get valueObject(){const e={};for(const t of this.declaredFields){const i=this[t];e[t]=i instanceof r?i.valueClear:i}return e}get valueArray(){return this.declaredFields.map(e=>{const t=this[e];return t instanceof r?t.valueClear:t})}get errors(){return this._errors}set errors(e){for(const t in e){const i=this[t];i instanceof r?i.error=e[t]:this._errors[t]=e[t]}}set fields(e){for(const t in e){if(!this.declaredFields.includes(t))throw new Error(`Field "${t}" is not declared in form "${this.constructor.name}"`);const i=this[t],s=e[t];i instanceof r?i.value=s:this[t]=s}}setFieldValue(e,t){const i=this[e];i instanceof r?i.value=t:this[e]=t}getFieldValue(e){const t=this[e];return t instanceof r?t.valueClear:t}reset(){for(const e of this.declaredFields){const t=this[e];t instanceof r&&t.reset()}for(const e of Object.keys(this._errors))delete this._errors[e];return this}}class h extends r{minLength;maxLength;regex;example;mask;returnWithMask;constructor(e="",t={}){super(e,t),this.minLength=t.minLength??null,this.maxLength=t.maxLength??null,this.regex=t.regex instanceof RegExp?t.regex:new RegExp(t.regex||".*"),this.example=t.example??"Enter a valid string",this.mask=t.mask??null,this.returnWithMask=t.returnWithMask??!1,this.validators.push(this.minLengthValidator.bind(this)),this.validators.push(this.maxLengthValidator.bind(this)),this.validators.push(this.regexValidator.bind(this))}normalizeValue(e){return this.fieldIsEmpty(e)?"":String(e)}get valueClear(){if(this.returnWithMask)return this._value===""?null:this._value;if(!this.mask)return this._value||null;const e=this.mask.split(/_/g).join("");return(this._value||"").replace(new RegExp(`[${e}]`,"g"),"")}get value(){return this._value}set value(e){if(!this.mask||!e){super.value=e;return}const t=this.mask.replace(/_/g,""),i=e.replace(new RegExp(`[${t}]`,"g"),"");if(!i){super.value="";return}let s=0;const l=this.mask.replace(/_/g,()=>i[s++]??"_"),a=l.indexOf("_");super.value=a===-1?l:l.slice(0,a)}minLengthValidator(e){return this.minLength===null||!e?null:e.length<this.minLength?`Minimum ${this.minLength} characters`:null}maxLengthValidator(e){return this.maxLength===null||!e?null:e.length>this.maxLength?`Maximum ${this.maxLength} characters`:null}regexValidator(e){return e?this.regex.test(e)?null:this.example?`Example: ${this.example}`:"Invalid format":null}}class d extends r{min;max;example;constructor(e=null,t={}){super(e,t),this.min=t.min??null,this.max=t.max??null,this.example=t.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))}normalizeValue(e){if(this.fieldIsEmpty(e))return null;if(typeof e=="number")return isNaN(e)?null:e;if(typeof e=="string"){const t=e.trim();if(t==="")return null;const i=Number(t);return isNaN(i)?null:i}return null}get valueClear(){return this._value}typeValidator(e){return e===null?this.isRequired?`Example: ${this.example}`:null:typeof e!="number"||isNaN(e)?this.example?`Example: ${this.example}`:"Invalid number":null}minValidator(e){return e===null||this.min===null?null:e<this.min?`Minimum value: ${this.min}`:null}maxValidator(e){return e===null||this.max===null?null:e>this.max?`Maximum value: ${this.max}`:null}}class o extends r{mode;min;max;example;constructor(e=null,t={}){const i=e instanceof Date?e:e?new Date(e):null;super(i,t),this.mode=t.mode??"datetime",this.min=t.min?new Date(t.min):null,this.max=t.max?new Date(t.max):null,this.example=t.example??(this.mode==="date"?"YYYY-MM-DD":"YYYY-MM-DD HH:mm:ss"),this.validators.push(this.typeValidator.bind(this)),this.validators.push(this.minValidator.bind(this)),this.validators.push(this.maxValidator.bind(this))}normalizeValue(e){if(this.fieldIsEmpty(e))return null;if(e instanceof Date)return isNaN(e.getTime())?null:e;if(typeof e=="string"||typeof e=="number"){const t=new Date(e);return isNaN(t.getTime())?null:t}return null}get valueClear(){if(!this._value)return null;const e=l=>String(l).padStart(2,"0"),t=this._value.getFullYear(),i=e(this._value.getMonth()+1),s=e(this._value.getDate());return this.mode==="date"?`${t}-${i}-${s}`:this._value.toISOString()}typeValidator(e){return e===null?this.isRequired?`Example: ${this.example}`:null:!(e instanceof Date)||isNaN(e.getTime())?this.example?`Example: ${this.example}`:"Invalid date format":null}minValidator(e){return!e||!this.min?null:e<this.min?`Date must be after ${this.formatDate(this.min)}`:null}maxValidator(e){return!e||!this.max?null:e>this.max?`Date must be before ${this.formatDate(this.max)}`:null}formatDate(e){const t=f=>String(f).padStart(2,"0"),i=e.getFullYear(),s=t(e.getMonth()+1),l=t(e.getDate()),a=t(e.getHours()),c=t(e.getMinutes());return this.mode==="date"?`${i}-${s}-${l}`:`${i}-${s}-${l} ${a}:${c}`}}const m=Object.freeze(Object.defineProperty({__proto__:null},Symbol.toStringTag,{value:"Module"})),g={Form:u,FormField:r,StringField:h,NumberField:d,DateTimeField:o,FieldOptions:m};exports.DateTimeField=o;exports.FieldOptions=m;exports.Form=u;exports.FormField=r;exports.NumberField=d;exports.StringField=h;exports.default=g;
|