adaptform 1.1.4 → 1.1.6

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.
@@ -0,0 +1,24 @@
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
+ }
@@ -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,20 @@
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;
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 {};
@@ -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
- declare const AdaptForm: {
2
- Form: typeof Form;
3
- FormField: typeof FormField;
4
- StringField: typeof StringField;
5
- NumberField: typeof NumberField;
6
- DateTimeField: typeof DateTimeField;
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';
@@ -1,4 +1,4 @@
1
- class r {
1
+ class l {
2
2
  _value;
3
3
  defaultValue;
4
4
  _error = null;
@@ -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 x extends l {
123
58
  minLength;
124
59
  maxLength;
125
60
  regex;
@@ -152,9 +87,9 @@ class o extends r {
152
87
  super.value = "";
153
88
  return;
154
89
  }
155
- let s = 0;
156
- const l = this.mask.replace(/_/g, () => i[s++] ?? "_"), a = l.indexOf("_");
157
- super.value = a === -1 ? l : l.slice(0, a);
90
+ let r = 0;
91
+ const s = this.mask.replace(/_/g, () => i[r++] ?? "_"), n = s.indexOf("_");
92
+ super.value = n === -1 ? s : s.slice(0, n);
158
93
  }
159
94
  minLengthValidator(t) {
160
95
  return this.minLength === null || !t ? null : t.length < this.minLength ? `Minimum ${this.minLength} characters` : null;
@@ -166,7 +101,7 @@ class o extends r {
166
101
  return t ? this.regex.test(t) ? null : this.example ? `Example: ${this.example}` : "Invalid format" : null;
167
102
  }
168
103
  }
169
- class d extends r {
104
+ class p extends l {
170
105
  min;
171
106
  max;
172
107
  example;
@@ -197,28 +132,64 @@ 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 c extends r {
135
+ class _ extends l {
201
136
  mode;
202
137
  min;
203
138
  max;
204
139
  example;
140
+ format;
205
141
  constructor(t = null, e = {}) {
206
142
  const i = t instanceof Date ? t : t ? new Date(t) : null;
207
- 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:ss"), this.validators.push(this.typeValidator.bind(this)), this.validators.push(this.minValidator.bind(this)), this.validators.push(this.maxValidator.bind(this));
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 ?? (this.mode === "date" ? "YYYY-MM-DD" : "YYYY-MM-DD HH:mm"), this.validators.push(this.typeValidator.bind(this)), this.validators.push(this.minValidator.bind(this)), this.validators.push(this.maxValidator.bind(this));
208
144
  }
209
145
  normalizeValue(t) {
210
146
  if (this.fieldIsEmpty(t)) return null;
211
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);
212
150
  if (typeof t == "string" || typeof t == "number") {
213
151
  const e = new Date(t);
214
152
  return isNaN(e.getTime()) ? null : e;
215
153
  }
216
154
  return null;
217
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
+ }
185
+ }
186
+ const g = new Date(n, u, m, d, c, f);
187
+ return isNaN(g.getTime()) ? null : g;
188
+ }
218
189
  get valueClear() {
219
190
  if (!this._value) return null;
220
- const t = (l) => String(l).padStart(2, "0"), e = this._value.getFullYear(), i = t(this._value.getMonth() + 1), s = t(this._value.getDate());
221
- return this.mode === "date" ? `${e}-${i}-${s}` : this._value.toISOString();
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();
222
193
  }
223
194
  typeValidator(t) {
224
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;
@@ -231,26 +202,79 @@ class c extends r {
231
202
  return !t || !this.max ? null : t > this.max ? `Date must be before ${this.formatDate(this.max)}` : null;
232
203
  }
233
204
  formatDate(t) {
234
- const e = (u) => String(u).padStart(2, "0"), i = t.getFullYear(), s = e(t.getMonth() + 1), l = e(t.getDate()), a = e(t.getHours()), h = e(t.getMinutes());
235
- return this.mode === "date" ? `${i}-${s}-${l}` : `${i}-${s}-${l} ${a}:${h}`;
205
+ const e = (m) => String(m).padStart(2, "0"), i = t.getFullYear(), r = e(t.getMonth() + 1), s = e(t.getDate()), n = e(t.getHours()), u = e(t.getMinutes());
206
+ return this.mode === "date" ? `${i}-${r}-${s}` : `${i}-${r}-${s} ${n}:${u}`;
207
+ }
208
+ }
209
+ class V {
210
+ _errors = {};
211
+ get declaredFields() {
212
+ return Object.keys(this).filter(
213
+ (t) => this[t] instanceof l
214
+ );
215
+ }
216
+ checkValid() {
217
+ let t = !0;
218
+ for (const e of this.declaredFields) {
219
+ const i = this[e];
220
+ i instanceof l && (i.checkValid(), (i.isValid === !1 || i.isValid === null && i.isRequired) && (t = !1));
221
+ }
222
+ return t;
223
+ }
224
+ get valueObject() {
225
+ const t = {};
226
+ for (const e of this.declaredFields) {
227
+ const i = this[e];
228
+ t[e] = i instanceof l ? i.valueClear : i;
229
+ }
230
+ return t;
231
+ }
232
+ get valueArray() {
233
+ return this.declaredFields.map((t) => {
234
+ const e = this[t];
235
+ return e instanceof l ? e.valueClear : e;
236
+ });
237
+ }
238
+ get errors() {
239
+ return this._errors;
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();
269
+ }
270
+ for (const t of Object.keys(this._errors)) delete this._errors[t];
271
+ return this;
236
272
  }
237
273
  }
238
- const f = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
239
- __proto__: null
240
- }, Symbol.toStringTag, { value: "Module" })), g = {
241
- Form: m,
242
- FormField: r,
243
- StringField: o,
244
- NumberField: d,
245
- DateTimeField: c,
246
- FieldOptions: f
247
- };
248
274
  export {
249
- c as DateTimeField,
250
- f as FieldOptions,
251
- m as Form,
252
- r as FormField,
253
- d as NumberField,
254
- o as StringField,
255
- g as default
275
+ _ as DateTimeField,
276
+ V as Form,
277
+ l as FormField,
278
+ p as NumberField,
279
+ x as StringField
256
280
  };
package/package.json CHANGED
@@ -1,28 +1,24 @@
1
1
  {
2
2
  "name": "adaptform",
3
- "version": "1.1.4",
4
- "description": "Library of adaptive forms of validation",
3
+ "version": "1.1.6",
5
4
  "type": "module",
6
- "main": "./dist/index.cjs.js",
7
- "module": "./dist/index.es.js",
8
- "types": "./dist/index.d.ts",
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.es.js",
12
- "require": "./dist/index.cjs.js"
13
- }
10
+ "import": "./dist/index.mjs",
11
+ "types": "./dist/index.d.ts"
12
+ },
13
+ "./*": "./dist/*"
14
14
  },
15
- "files": [
16
- "dist/**/*"
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;