adaptform 1.1.5 → 1.1.7
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/LICENSE.md +21 -0
- package/README.md +1 -0
- package/dist/core/FieldOptions.d.ts +1 -0
- package/dist/core/date/DateTimeField.d.ts +2 -0
- package/dist/index.mjs +64 -28
- package/package.json +16 -2
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ArhiMEN (Stanislav Orlov)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Library of adaptive forms of validation
|
|
@@ -6,8 +6,10 @@ export declare class DateTimeField extends FormField<Date, string> {
|
|
|
6
6
|
min: Date | null;
|
|
7
7
|
max: Date | null;
|
|
8
8
|
example: string;
|
|
9
|
+
format: string | null;
|
|
9
10
|
constructor(defaultValue?: DateValue, options?: DateTimeFieldOptions);
|
|
10
11
|
protected normalizeValue(value: any): Date | null;
|
|
12
|
+
protected parseByFormat(value: string, format: string): Date | null;
|
|
11
13
|
get valueClear(): string | null;
|
|
12
14
|
private typeValidator;
|
|
13
15
|
private minValidator;
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class l {
|
|
2
2
|
_value;
|
|
3
3
|
defaultValue;
|
|
4
4
|
_error = null;
|
|
@@ -54,7 +54,7 @@ class r {
|
|
|
54
54
|
return this._error = null, this.isValid;
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
class
|
|
57
|
+
class x extends l {
|
|
58
58
|
minLength;
|
|
59
59
|
maxLength;
|
|
60
60
|
regex;
|
|
@@ -87,9 +87,9 @@ class m extends r {
|
|
|
87
87
|
super.value = "";
|
|
88
88
|
return;
|
|
89
89
|
}
|
|
90
|
-
let
|
|
91
|
-
const
|
|
92
|
-
super.value =
|
|
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);
|
|
93
93
|
}
|
|
94
94
|
minLengthValidator(t) {
|
|
95
95
|
return this.minLength === null || !t ? null : t.length < this.minLength ? `Minimum ${this.minLength} characters` : null;
|
|
@@ -101,7 +101,7 @@ class m extends r {
|
|
|
101
101
|
return t ? this.regex.test(t) ? null : this.example ? `Example: ${this.example}` : "Invalid format" : null;
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
-
class
|
|
104
|
+
class p extends l {
|
|
105
105
|
min;
|
|
106
106
|
max;
|
|
107
107
|
example;
|
|
@@ -132,28 +132,64 @@ class d extends r {
|
|
|
132
132
|
return t === null || this.max === null ? null : t > this.max ? `Maximum value: ${this.max}` : null;
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
|
-
class
|
|
135
|
+
class _ extends l {
|
|
136
136
|
mode;
|
|
137
137
|
min;
|
|
138
138
|
max;
|
|
139
139
|
example;
|
|
140
|
+
format;
|
|
140
141
|
constructor(t = null, e = {}) {
|
|
141
142
|
const i = t instanceof Date ? t : t ? new Date(t) : null;
|
|
142
|
-
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
|
|
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));
|
|
143
144
|
}
|
|
144
145
|
normalizeValue(t) {
|
|
145
146
|
if (this.fieldIsEmpty(t)) return null;
|
|
146
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);
|
|
147
150
|
if (typeof t == "string" || typeof t == "number") {
|
|
148
151
|
const e = new Date(t);
|
|
149
152
|
return isNaN(e.getTime()) ? null : e;
|
|
150
153
|
}
|
|
151
154
|
return null;
|
|
152
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
|
+
}
|
|
153
189
|
get valueClear() {
|
|
154
190
|
if (!this._value) return null;
|
|
155
|
-
const t = (
|
|
156
|
-
return this.mode === "date" ? `${e}-${i}-${
|
|
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();
|
|
157
193
|
}
|
|
158
194
|
typeValidator(t) {
|
|
159
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;
|
|
@@ -166,22 +202,22 @@ class o extends r {
|
|
|
166
202
|
return !t || !this.max ? null : t > this.max ? `Date must be before ${this.formatDate(this.max)}` : null;
|
|
167
203
|
}
|
|
168
204
|
formatDate(t) {
|
|
169
|
-
const e = (
|
|
170
|
-
return this.mode === "date" ? `${i}-${
|
|
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}`;
|
|
171
207
|
}
|
|
172
208
|
}
|
|
173
|
-
class
|
|
209
|
+
class V {
|
|
174
210
|
_errors = {};
|
|
175
211
|
get declaredFields() {
|
|
176
212
|
return Object.keys(this).filter(
|
|
177
|
-
(t) => this[t] instanceof
|
|
213
|
+
(t) => this[t] instanceof l
|
|
178
214
|
);
|
|
179
215
|
}
|
|
180
216
|
checkValid() {
|
|
181
217
|
let t = !0;
|
|
182
218
|
for (const e of this.declaredFields) {
|
|
183
219
|
const i = this[e];
|
|
184
|
-
i instanceof
|
|
220
|
+
i instanceof l && (i.checkValid(), (i.isValid === !1 || i.isValid === null && i.isRequired) && (t = !1));
|
|
185
221
|
}
|
|
186
222
|
return t;
|
|
187
223
|
}
|
|
@@ -189,14 +225,14 @@ class c {
|
|
|
189
225
|
const t = {};
|
|
190
226
|
for (const e of this.declaredFields) {
|
|
191
227
|
const i = this[e];
|
|
192
|
-
t[e] = i instanceof
|
|
228
|
+
t[e] = i instanceof l ? i.valueClear : i;
|
|
193
229
|
}
|
|
194
230
|
return t;
|
|
195
231
|
}
|
|
196
232
|
get valueArray() {
|
|
197
233
|
return this.declaredFields.map((t) => {
|
|
198
234
|
const e = this[t];
|
|
199
|
-
return e instanceof
|
|
235
|
+
return e instanceof l ? e.valueClear : e;
|
|
200
236
|
});
|
|
201
237
|
}
|
|
202
238
|
get errors() {
|
|
@@ -205,7 +241,7 @@ class c {
|
|
|
205
241
|
set errors(t) {
|
|
206
242
|
for (const e in t) {
|
|
207
243
|
const i = this[e];
|
|
208
|
-
i instanceof
|
|
244
|
+
i instanceof l ? i.error = t[e] : this._errors[e] = t[e];
|
|
209
245
|
}
|
|
210
246
|
}
|
|
211
247
|
set fields(t) {
|
|
@@ -214,31 +250,31 @@ class c {
|
|
|
214
250
|
throw new Error(
|
|
215
251
|
`Field "${e}" is not declared in form "${this.constructor.name}"`
|
|
216
252
|
);
|
|
217
|
-
const i = this[e],
|
|
218
|
-
i instanceof
|
|
253
|
+
const i = this[e], r = t[e];
|
|
254
|
+
i instanceof l ? i.value = r : this[e] = r;
|
|
219
255
|
}
|
|
220
256
|
}
|
|
221
257
|
setFieldValue(t, e) {
|
|
222
258
|
const i = this[t];
|
|
223
|
-
i instanceof
|
|
259
|
+
i instanceof l ? i.value = e : this[t] = e;
|
|
224
260
|
}
|
|
225
261
|
getFieldValue(t) {
|
|
226
262
|
const e = this[t];
|
|
227
|
-
return e instanceof
|
|
263
|
+
return e instanceof l ? e.valueClear : e;
|
|
228
264
|
}
|
|
229
265
|
reset() {
|
|
230
266
|
for (const t of this.declaredFields) {
|
|
231
267
|
const e = this[t];
|
|
232
|
-
e instanceof
|
|
268
|
+
e instanceof l && e.reset();
|
|
233
269
|
}
|
|
234
270
|
for (const t of Object.keys(this._errors)) delete this._errors[t];
|
|
235
271
|
return this;
|
|
236
272
|
}
|
|
237
273
|
}
|
|
238
274
|
export {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
275
|
+
_ as DateTimeField,
|
|
276
|
+
V as Form,
|
|
277
|
+
l as FormField,
|
|
278
|
+
p as NumberField,
|
|
279
|
+
x as StringField
|
|
244
280
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adaptform",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.7",
|
|
4
|
+
"description": "Library of adaptive forms of validation",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"form",
|
|
7
|
+
"validation",
|
|
8
|
+
"typescript"
|
|
9
|
+
],
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "Stanislav Orlov",
|
|
12
|
+
"nickname": "ArhiMEN",
|
|
13
|
+
"email": "stanislav1426@gmai.com",
|
|
14
|
+
"url": "https://github.com/ArhiMEN"
|
|
15
|
+
},
|
|
4
16
|
"type": "module",
|
|
5
17
|
"main": "dist/index.mjs",
|
|
6
18
|
"module": "dist/index.mjs",
|
|
@@ -12,7 +24,9 @@
|
|
|
12
24
|
},
|
|
13
25
|
"./*": "./dist/*"
|
|
14
26
|
},
|
|
15
|
-
"files": [
|
|
27
|
+
"files": [
|
|
28
|
+
"dist/**/*"
|
|
29
|
+
],
|
|
16
30
|
"sideEffects": false,
|
|
17
31
|
"scripts": {
|
|
18
32
|
"dev": "vite",
|