adaptform 1.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/README.md +0 -0
- package/dist/adaptform.es.js +220 -0
- package/dist/adaptform.umd.js +1 -0
- package/dist/core/FieldOptions.d.ts +20 -0
- package/dist/core/FieldOptions.js +1 -0
- package/dist/core/Form.d.ts +14 -0
- package/dist/core/Form.js +91 -0
- package/dist/core/FormField.d.ts +19 -0
- package/dist/core/FormField.js +54 -0
- package/dist/core/SimpleField.d.ts +19 -0
- package/dist/core/SimpleField.js +41 -0
- package/dist/core/common/regex.d.ts +0 -0
- package/dist/core/common/regex.js +1 -0
- package/dist/core/date/DateTimeField.d.ts +14 -0
- package/dist/core/date/DateTimeField.js +49 -0
- package/dist/core/number/NumberField.d.ts +10 -0
- package/dist/core/number/NumberField.js +53 -0
- package/dist/core/string/StringField.d.ts +14 -0
- package/dist/core/string/StringField.js +64 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/package.json +27 -0
package/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
class n {
|
|
2
|
+
_type;
|
|
3
|
+
_value;
|
|
4
|
+
_error = null;
|
|
5
|
+
_validator;
|
|
6
|
+
isValid = null;
|
|
7
|
+
constructor(e, i, t) {
|
|
8
|
+
this._type = e, this._value = i ?? null, this._validator = t;
|
|
9
|
+
}
|
|
10
|
+
__simple_validator(e) {
|
|
11
|
+
const i = typeof e === this._type;
|
|
12
|
+
return [i, i ? "" : "Invalid format"];
|
|
13
|
+
}
|
|
14
|
+
get __validator() {
|
|
15
|
+
return this._validator ?? this.__simple_validator;
|
|
16
|
+
}
|
|
17
|
+
checkValid() {
|
|
18
|
+
const [e, i] = this.__validator(this._value);
|
|
19
|
+
return e || (this._error = i), this.isValid = e;
|
|
20
|
+
}
|
|
21
|
+
get value() {
|
|
22
|
+
return this._value;
|
|
23
|
+
}
|
|
24
|
+
set value(e) {
|
|
25
|
+
this._value = e, this.checkValid();
|
|
26
|
+
}
|
|
27
|
+
get error() {
|
|
28
|
+
return this._error ?? "";
|
|
29
|
+
}
|
|
30
|
+
set error(e) {
|
|
31
|
+
this._error = e;
|
|
32
|
+
}
|
|
33
|
+
get valueClear() {
|
|
34
|
+
return this._value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
class s {
|
|
38
|
+
_value;
|
|
39
|
+
defaultValue;
|
|
40
|
+
_error = null;
|
|
41
|
+
isRequired;
|
|
42
|
+
isValid;
|
|
43
|
+
constructor(e, i = {}) {
|
|
44
|
+
this._value = e ?? null, this.defaultValue = e ?? null, this.isValid = e === null ? null : !0, this.isRequired = i.isRequired ?? !0;
|
|
45
|
+
}
|
|
46
|
+
get value() {
|
|
47
|
+
return this._value;
|
|
48
|
+
}
|
|
49
|
+
set value(e) {
|
|
50
|
+
this._value = e, this.checkValid();
|
|
51
|
+
}
|
|
52
|
+
get error() {
|
|
53
|
+
return this._error ?? "";
|
|
54
|
+
}
|
|
55
|
+
set error(e) {
|
|
56
|
+
this._error = e;
|
|
57
|
+
}
|
|
58
|
+
set __valid_error(e) {
|
|
59
|
+
const [i = "", t = ""] = (this.error || "").split(" | ");
|
|
60
|
+
e ? i || (this.error = t ? `${e} | ${this.error}` : e) : this.error = t || null;
|
|
61
|
+
}
|
|
62
|
+
reset() {
|
|
63
|
+
this._value = this.defaultValue ?? null, this._error = null, this.isValid = this.defaultValue === null ? null : !0;
|
|
64
|
+
}
|
|
65
|
+
fieldIsEmpty(e) {
|
|
66
|
+
return e == null || e === "" ? !0 : typeof e == "object" && Object.keys(e).length === 0;
|
|
67
|
+
}
|
|
68
|
+
get isEmpty() {
|
|
69
|
+
return this.fieldIsEmpty(this._value);
|
|
70
|
+
}
|
|
71
|
+
fieldIsZero(e) {
|
|
72
|
+
return e === 0 || e === "0";
|
|
73
|
+
}
|
|
74
|
+
get isZero() {
|
|
75
|
+
return this.fieldIsZero(this._value);
|
|
76
|
+
}
|
|
77
|
+
checkValid() {
|
|
78
|
+
return this.isEmpty && !this.isZero ? this.isValid = !this.isRequired : this.isValid = !0, this.isValid;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
class u extends s {
|
|
82
|
+
minLength;
|
|
83
|
+
maxLength;
|
|
84
|
+
regex;
|
|
85
|
+
example;
|
|
86
|
+
mask;
|
|
87
|
+
returnWithMask;
|
|
88
|
+
constructor(e = "", i = {}) {
|
|
89
|
+
super(e, i), this.minLength = i.minLength ?? null, this.maxLength = i.maxLength ?? null, this.regex = i.regex instanceof RegExp ? i.regex : new RegExp(i.regex || ".*"), this.example = i.example ?? "", this.mask = i.mask ?? "", this.returnWithMask = i.returnWithMask ?? !1;
|
|
90
|
+
}
|
|
91
|
+
get valueClear() {
|
|
92
|
+
if (this.returnWithMask)
|
|
93
|
+
return this._value === "" ? null : this._value;
|
|
94
|
+
const e = this.mask.split(/_/g).join("");
|
|
95
|
+
return (this._value || "").replace(new RegExp(`[${e}]`, "g"), "");
|
|
96
|
+
}
|
|
97
|
+
get value() {
|
|
98
|
+
return this._value;
|
|
99
|
+
}
|
|
100
|
+
set value(e) {
|
|
101
|
+
if (this.mask === "" || !e) {
|
|
102
|
+
super.value = e;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (this._value && this._value.length > e.length) {
|
|
106
|
+
super.value = e;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
let i = 0;
|
|
110
|
+
const t = this.mask.split(/_/g).join(""), l = e.replace(new RegExp(`[${t}]`, "g"), "");
|
|
111
|
+
if (l.length === 0) {
|
|
112
|
+
super.value = l;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const h = this, a = this.mask.replace(/_/g, function() {
|
|
116
|
+
return i < l.length ? l.charAt(i++) || h.mask.charAt(i) : "_";
|
|
117
|
+
});
|
|
118
|
+
if (i = a.indexOf("_"), i !== -1) {
|
|
119
|
+
super.value = a.slice(0, i);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
super.value = a;
|
|
123
|
+
}
|
|
124
|
+
checkValid() {
|
|
125
|
+
return super.checkValid(), this.isValid === !1 ? this.isValid : this.isEmpty && !this.isRequired ? (this.isValid = !0, this._value = "", this.isValid) : this.minLength !== null && this.minLength > this._value.length ? (this.isValid = !1, this.__valid_error = `Minimum ${this.minLength} characters`, this.isValid) : (this.__valid_error = null, this.maxLength !== null && this.maxLength < this._value.length ? (this.isValid = !1, this.__valid_error = `Maximum ${this.maxLength} characters`, this.isValid) : (this.__valid_error = null, this.regex.test(this._value) ? (this.isValid = !0, this._error = null, this.isValid) : (this.isValid = !1, this._error = this.example ? `Example: ${this.example}` : "Invalid format", this.isValid)));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
class d extends s {
|
|
129
|
+
min;
|
|
130
|
+
max;
|
|
131
|
+
example;
|
|
132
|
+
constructor(e = null, i = {}) {
|
|
133
|
+
super(e, i), this.min = i.min ?? null, this.max = i.max ?? null, this.example = i.example || "Whole number";
|
|
134
|
+
}
|
|
135
|
+
set valueFromInput(e) {
|
|
136
|
+
if (typeof e == "string") {
|
|
137
|
+
const i = e.trim();
|
|
138
|
+
i.length === 0 ? e = null : e = Number(i);
|
|
139
|
+
}
|
|
140
|
+
this.value = e;
|
|
141
|
+
}
|
|
142
|
+
get valueClear() {
|
|
143
|
+
return this._value;
|
|
144
|
+
}
|
|
145
|
+
checkValid() {
|
|
146
|
+
return super.checkValid(), this.isValid === !1 ? this.isValid : this.isEmpty && !this.isRequired ? (this.isValid = !0, this._value = null, this.isValid) : typeof this._value != "number" || isNaN(this._value) ? (this.isValid = !1, this._error = this.example ? `Example: ${this.example}` : "Invalid format", this.isValid) : this.min !== null && this._value < this.min ? (this.isValid = !1, this._error = `Minimum value: ${this.min}`, this.isValid) : this.max !== null && this._value > this.max ? (this.isValid = !1, this._error = `Maximum value: ${this.max}`, this.isValid) : (this.isValid = !0, this._error = null, this.isValid);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
class o {
|
|
150
|
+
_declaredFields;
|
|
151
|
+
_errors;
|
|
152
|
+
constructor() {
|
|
153
|
+
this._declaredFields = [], this._errors = {};
|
|
154
|
+
}
|
|
155
|
+
init() {
|
|
156
|
+
for (const e of Object.keys(this)) {
|
|
157
|
+
const i = this[e];
|
|
158
|
+
(i instanceof s || i instanceof n) && this._declaredFields.push(e);
|
|
159
|
+
}
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
checkValid() {
|
|
163
|
+
let e = !0;
|
|
164
|
+
for (const i of Object.keys(this)) {
|
|
165
|
+
const t = this[i];
|
|
166
|
+
t instanceof s && (t.checkValid(), (t.isValid === !1 || t.isValid === null && t.isRequired) && (e = !1));
|
|
167
|
+
}
|
|
168
|
+
return e;
|
|
169
|
+
}
|
|
170
|
+
get valueObject() {
|
|
171
|
+
const e = {};
|
|
172
|
+
for (const i of this._declaredFields) {
|
|
173
|
+
const t = this[i];
|
|
174
|
+
e[i] = t instanceof s ? t.valueClear : t;
|
|
175
|
+
}
|
|
176
|
+
return e;
|
|
177
|
+
}
|
|
178
|
+
get valueArray() {
|
|
179
|
+
const e = [];
|
|
180
|
+
for (const i of this._declaredFields) {
|
|
181
|
+
const t = this[i];
|
|
182
|
+
e.push(t.valueClear);
|
|
183
|
+
}
|
|
184
|
+
return e;
|
|
185
|
+
}
|
|
186
|
+
get errors() {
|
|
187
|
+
return this._errors;
|
|
188
|
+
}
|
|
189
|
+
set errors(e) {
|
|
190
|
+
for (const i in e) {
|
|
191
|
+
const t = this[i];
|
|
192
|
+
this._declaredFields.includes(i) ? t.error = e[i] : this._errors[i] = e[i];
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
set fieldsMapping(e) {
|
|
196
|
+
if (typeof e != "function")
|
|
197
|
+
throw new Error("fieldsMapping must be a function returning field values");
|
|
198
|
+
const i = e();
|
|
199
|
+
if (!i || typeof i != "object" || Array.isArray(i))
|
|
200
|
+
throw new Error("fieldsMapping function must return a plain object");
|
|
201
|
+
}
|
|
202
|
+
set fields(e) {
|
|
203
|
+
for (const i in e) {
|
|
204
|
+
if (!this._declaredFields.includes(i)) throw new Error(`Field "${i}" does not declared in the form "${this.constructor.name}"`);
|
|
205
|
+
const t = this[i];
|
|
206
|
+
t instanceof s ? t.value = e[i] : this[i] = e[i];
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
reset() {
|
|
210
|
+
for (const e of this._declaredFields)
|
|
211
|
+
this[e].reset();
|
|
212
|
+
return this.errors = {}, this;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
export {
|
|
216
|
+
o as Form,
|
|
217
|
+
d as NumberField,
|
|
218
|
+
n as SimpleField,
|
|
219
|
+
u as StringField
|
|
220
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(s,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(s=typeof globalThis<"u"?globalThis:s||self,r(s.AdaptForm={}))})(this,function(s){"use strict";class r{_type;_value;_error=null;_validator;isValid=null;constructor(e,i,t){this._type=e,this._value=i??null,this._validator=t}__simple_validator(e){const i=typeof e===this._type;return[i,i?"":"Invalid format"]}get __validator(){return this._validator??this.__simple_validator}checkValid(){const[e,i]=this.__validator(this._value);return e||(this._error=i),this.isValid=e}get value(){return this._value}set value(e){this._value=e,this.checkValid()}get error(){return this._error??""}set error(e){this._error=e}get valueClear(){return this._value}}class l{_value;defaultValue;_error=null;isRequired;isValid;constructor(e,i={}){this._value=e??null,this.defaultValue=e??null,this.isValid=e===null?null:!0,this.isRequired=i.isRequired??!0}get value(){return this._value}set value(e){this._value=e,this.checkValid()}get error(){return this._error??""}set error(e){this._error=e}set __valid_error(e){const[i="",t=""]=(this.error||"").split(" | ");e?i||(this.error=t?`${e} | ${this.error}`:e):this.error=t||null}reset(){this._value=this.defaultValue??null,this._error=null,this.isValid=this.defaultValue===null?null:!0}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(){return this.isEmpty&&!this.isZero?this.isValid=!this.isRequired:this.isValid=!0,this.isValid}}class u extends l{minLength;maxLength;regex;example;mask;returnWithMask;constructor(e="",i={}){super(e,i),this.minLength=i.minLength??null,this.maxLength=i.maxLength??null,this.regex=i.regex instanceof RegExp?i.regex:new RegExp(i.regex||".*"),this.example=i.example??"",this.mask=i.mask??"",this.returnWithMask=i.returnWithMask??!1}get valueClear(){if(this.returnWithMask)return this._value===""?null:this._value;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}if(this._value&&this._value.length>e.length){super.value=e;return}let i=0;const t=this.mask.split(/_/g).join(""),h=e.replace(new RegExp(`[${t}]`,"g"),"");if(h.length===0){super.value=h;return}const c=this,n=this.mask.replace(/_/g,function(){return i<h.length?h.charAt(i++)||c.mask.charAt(i):"_"});if(i=n.indexOf("_"),i!==-1){super.value=n.slice(0,i);return}super.value=n}checkValid(){return super.checkValid(),this.isValid===!1?this.isValid:this.isEmpty&&!this.isRequired?(this.isValid=!0,this._value="",this.isValid):this.minLength!==null&&this.minLength>this._value.length?(this.isValid=!1,this.__valid_error=`Minimum ${this.minLength} characters`,this.isValid):(this.__valid_error=null,this.maxLength!==null&&this.maxLength<this._value.length?(this.isValid=!1,this.__valid_error=`Maximum ${this.maxLength} characters`,this.isValid):(this.__valid_error=null,this.regex.test(this._value)?(this.isValid=!0,this._error=null,this.isValid):(this.isValid=!1,this._error=this.example?`Example: ${this.example}`:"Invalid format",this.isValid)))}}class d extends l{min;max;example;constructor(e=null,i={}){super(e,i),this.min=i.min??null,this.max=i.max??null,this.example=i.example||"Whole number"}set valueFromInput(e){if(typeof e=="string"){const i=e.trim();i.length===0?e=null:e=Number(i)}this.value=e}get valueClear(){return this._value}checkValid(){return super.checkValid(),this.isValid===!1?this.isValid:this.isEmpty&&!this.isRequired?(this.isValid=!0,this._value=null,this.isValid):typeof this._value!="number"||isNaN(this._value)?(this.isValid=!1,this._error=this.example?`Example: ${this.example}`:"Invalid format",this.isValid):this.min!==null&&this._value<this.min?(this.isValid=!1,this._error=`Minimum value: ${this.min}`,this.isValid):this.max!==null&&this._value>this.max?(this.isValid=!1,this._error=`Maximum value: ${this.max}`,this.isValid):(this.isValid=!0,this._error=null,this.isValid)}}class o{_declaredFields;_errors;constructor(){this._declaredFields=[],this._errors={}}init(){for(const e of Object.keys(this)){const i=this[e];(i instanceof l||i instanceof r)&&this._declaredFields.push(e)}return this}checkValid(){let e=!0;for(const i of Object.keys(this)){const t=this[i];t instanceof l&&(t.checkValid(),(t.isValid===!1||t.isValid===null&&t.isRequired)&&(e=!1))}return e}get valueObject(){const e={};for(const i of this._declaredFields){const t=this[i];e[i]=t instanceof l?t.valueClear:t}return e}get valueArray(){const e=[];for(const i of this._declaredFields){const t=this[i];e.push(t.valueClear)}return e}get errors(){return this._errors}set errors(e){for(const i in e){const t=this[i];this._declaredFields.includes(i)?t.error=e[i]:this._errors[i]=e[i]}}set fieldsMapping(e){if(typeof e!="function")throw new Error("fieldsMapping must be a function returning field values");const i=e();if(!i||typeof i!="object"||Array.isArray(i))throw new Error("fieldsMapping function must return a plain object")}set fields(e){for(const i in e){if(!this._declaredFields.includes(i))throw new Error(`Field "${i}" does not declared in the form "${this.constructor.name}"`);const t=this[i];t instanceof l?t.value=e[i]:this[i]=e[i]}}reset(){for(const e of this._declaredFields)this[e].reset();return this.errors={},this}}s.Form=o,s.NumberField=d,s.SimpleField=r,s.StringField=u,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface FormFieldOptions {
|
|
2
|
+
isRequired?: boolean;
|
|
3
|
+
}
|
|
4
|
+
interface StringFieldOptions extends FormFieldOptions {
|
|
5
|
+
minLength?: number;
|
|
6
|
+
maxLength?: number;
|
|
7
|
+
regex?: RegExp | string;
|
|
8
|
+
mask?: string;
|
|
9
|
+
example?: string;
|
|
10
|
+
}
|
|
11
|
+
interface NumberFieldOptions extends FormFieldOptions {
|
|
12
|
+
min?: number;
|
|
13
|
+
max?: number;
|
|
14
|
+
example?: string;
|
|
15
|
+
}
|
|
16
|
+
interface DateTimeFieldOptions extends FormFieldOptions {
|
|
17
|
+
min?: Date | string;
|
|
18
|
+
max?: Date | string;
|
|
19
|
+
example?: string;
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class Form {
|
|
2
|
+
private readonly _declaredFields;
|
|
3
|
+
private readonly _errors;
|
|
4
|
+
constructor();
|
|
5
|
+
init(): this;
|
|
6
|
+
checkValid(): boolean;
|
|
7
|
+
get valueObject(): Record<string, any>;
|
|
8
|
+
get valueArray(): any[];
|
|
9
|
+
get errors(): Record<string, any>;
|
|
10
|
+
set errors(errors: Record<string, any>);
|
|
11
|
+
set fieldsMapping(mapping: (() => Record<string, any>));
|
|
12
|
+
set fields(fields: Record<string, any>);
|
|
13
|
+
reset(): this;
|
|
14
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { FormField } from "./FormField";
|
|
2
|
+
import { SimpleField } from "./SimpleField";
|
|
3
|
+
export class Form {
|
|
4
|
+
_declaredFields;
|
|
5
|
+
_errors;
|
|
6
|
+
constructor() {
|
|
7
|
+
this._declaredFields = [];
|
|
8
|
+
this._errors = {};
|
|
9
|
+
}
|
|
10
|
+
init() {
|
|
11
|
+
for (const key of Object.keys(this)) {
|
|
12
|
+
const field = this[key];
|
|
13
|
+
if (field instanceof FormField || field instanceof SimpleField)
|
|
14
|
+
this._declaredFields.push(key);
|
|
15
|
+
}
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
checkValid() {
|
|
19
|
+
let isValid = true;
|
|
20
|
+
for (const key of Object.keys(this)) {
|
|
21
|
+
const field = this[key];
|
|
22
|
+
if (field instanceof FormField) {
|
|
23
|
+
field.checkValid();
|
|
24
|
+
if (field.isValid === false || (field.isValid === null && field.isRequired)) {
|
|
25
|
+
isValid = false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return isValid;
|
|
30
|
+
}
|
|
31
|
+
get valueObject() {
|
|
32
|
+
const fields = {};
|
|
33
|
+
for (const key of this._declaredFields) {
|
|
34
|
+
const val = this[key];
|
|
35
|
+
fields[key] = val.valueClear;
|
|
36
|
+
}
|
|
37
|
+
return fields;
|
|
38
|
+
}
|
|
39
|
+
get valueArray() {
|
|
40
|
+
const values = [];
|
|
41
|
+
for (const key of this._declaredFields) {
|
|
42
|
+
const val = this[key];
|
|
43
|
+
values.push(val.valueClear);
|
|
44
|
+
}
|
|
45
|
+
return values;
|
|
46
|
+
}
|
|
47
|
+
get errors() {
|
|
48
|
+
return this._errors;
|
|
49
|
+
}
|
|
50
|
+
set errors(errors) {
|
|
51
|
+
for (const key in errors) {
|
|
52
|
+
const field = this[key];
|
|
53
|
+
if (this._declaredFields.includes(key)) {
|
|
54
|
+
field.error = errors[key];
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
this._errors[key] = errors[key];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
set fieldsMapping(mapping) {
|
|
62
|
+
if (typeof mapping !== 'function') {
|
|
63
|
+
throw new Error('fieldsMapping must be a function returning field values');
|
|
64
|
+
}
|
|
65
|
+
const result = mapping();
|
|
66
|
+
if (!result || typeof result !== 'object' || Array.isArray(result)) {
|
|
67
|
+
throw new Error('fieldsMapping function must return a plain object');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
set fields(fields) {
|
|
71
|
+
for (const key in fields) {
|
|
72
|
+
if (!this._declaredFields.includes(key))
|
|
73
|
+
throw new Error(`Field "${key}" does not declared in the form "${this.constructor.name}"`);
|
|
74
|
+
const field = this[key];
|
|
75
|
+
if (field instanceof FormField) {
|
|
76
|
+
field.value = fields[key];
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this[key] = fields[key];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
reset() {
|
|
84
|
+
for (const key of this._declaredFields) {
|
|
85
|
+
const val = this[key];
|
|
86
|
+
val.reset();
|
|
87
|
+
}
|
|
88
|
+
this.errors = {};
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare abstract class FormField<Value = any> {
|
|
2
|
+
protected _value: Value | null;
|
|
3
|
+
protected defaultValue: Value | null;
|
|
4
|
+
protected _error: string | null;
|
|
5
|
+
isRequired: boolean;
|
|
6
|
+
isValid: boolean | null;
|
|
7
|
+
protected constructor(defaultValue?: Value | null, options?: FormFieldOptions);
|
|
8
|
+
get value(): Value | null;
|
|
9
|
+
set value(value: Value | null);
|
|
10
|
+
get error(): string;
|
|
11
|
+
set error(value: string | null);
|
|
12
|
+
reset(): void;
|
|
13
|
+
fieldIsEmpty(value: Value | null): boolean;
|
|
14
|
+
get isEmpty(): boolean;
|
|
15
|
+
fieldIsZero(value: Value | null): boolean;
|
|
16
|
+
get isZero(): boolean;
|
|
17
|
+
checkValid(): boolean;
|
|
18
|
+
abstract get valueClear(): Value | null;
|
|
19
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export class FormField {
|
|
2
|
+
_value;
|
|
3
|
+
defaultValue;
|
|
4
|
+
_error = null;
|
|
5
|
+
isRequired;
|
|
6
|
+
isValid;
|
|
7
|
+
constructor(defaultValue, options = {}) {
|
|
8
|
+
this._value = defaultValue ?? null;
|
|
9
|
+
this.defaultValue = defaultValue ?? null;
|
|
10
|
+
this.isValid = defaultValue === null ? null : true;
|
|
11
|
+
this.isRequired = options.isRequired ?? true;
|
|
12
|
+
}
|
|
13
|
+
get value() {
|
|
14
|
+
return this._value;
|
|
15
|
+
}
|
|
16
|
+
set value(value) {
|
|
17
|
+
this._value = value;
|
|
18
|
+
this.checkValid();
|
|
19
|
+
}
|
|
20
|
+
get error() {
|
|
21
|
+
return this._error ?? '';
|
|
22
|
+
}
|
|
23
|
+
set error(value) {
|
|
24
|
+
this._error = value;
|
|
25
|
+
}
|
|
26
|
+
reset() {
|
|
27
|
+
this._value = this.defaultValue ?? null;
|
|
28
|
+
this._error = null;
|
|
29
|
+
this.isValid = this.defaultValue === null ? null : true;
|
|
30
|
+
}
|
|
31
|
+
fieldIsEmpty(value) {
|
|
32
|
+
if (value === null || value === undefined || value === '')
|
|
33
|
+
return true;
|
|
34
|
+
return typeof value === 'object' && Object.keys(value).length === 0;
|
|
35
|
+
}
|
|
36
|
+
get isEmpty() {
|
|
37
|
+
return this.fieldIsEmpty(this._value);
|
|
38
|
+
}
|
|
39
|
+
fieldIsZero(value) {
|
|
40
|
+
return value === 0 || value === "0";
|
|
41
|
+
}
|
|
42
|
+
get isZero() {
|
|
43
|
+
return this.fieldIsZero(this._value);
|
|
44
|
+
}
|
|
45
|
+
checkValid() {
|
|
46
|
+
if (this.isEmpty && !this.isZero) {
|
|
47
|
+
this.isValid = !this.isRequired;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.isValid = true;
|
|
51
|
+
}
|
|
52
|
+
return this.isValid;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type SimpleType = 'boolean' | 'number' | 'string' | 'bigint' | 'symbol';
|
|
2
|
+
type SimpleValue = boolean | number | string | bigint | symbol;
|
|
3
|
+
export declare class SimpleField {
|
|
4
|
+
private readonly _type;
|
|
5
|
+
protected _value: SimpleValue | null;
|
|
6
|
+
protected _error: string | null;
|
|
7
|
+
protected _validator?: (value: SimpleValue | null) => [boolean, string];
|
|
8
|
+
isValid: boolean | null;
|
|
9
|
+
constructor(type: SimpleType, defaultValue?: SimpleValue, validator?: (value: SimpleValue | null) => [boolean, string]);
|
|
10
|
+
protected __simple_validator(value: SimpleValue | null): [boolean, string];
|
|
11
|
+
protected get __validator(): (value: SimpleValue | null) => [boolean, string];
|
|
12
|
+
checkValid(): boolean;
|
|
13
|
+
get value(): SimpleValue | null;
|
|
14
|
+
set value(value: SimpleValue | null);
|
|
15
|
+
get error(): string;
|
|
16
|
+
set error(value: string | null);
|
|
17
|
+
get valueClear(): SimpleValue | null;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export class SimpleField {
|
|
2
|
+
_type;
|
|
3
|
+
_value;
|
|
4
|
+
_error = null;
|
|
5
|
+
_validator;
|
|
6
|
+
isValid = null;
|
|
7
|
+
constructor(type, defaultValue, validator) {
|
|
8
|
+
this._type = type;
|
|
9
|
+
this._value = defaultValue ?? null;
|
|
10
|
+
this._validator = validator;
|
|
11
|
+
}
|
|
12
|
+
__simple_validator(value) {
|
|
13
|
+
const isValid = typeof value === this._type;
|
|
14
|
+
return [isValid, 'Invalid format'];
|
|
15
|
+
}
|
|
16
|
+
get __validator() {
|
|
17
|
+
return this._validator ?? this.__simple_validator;
|
|
18
|
+
}
|
|
19
|
+
checkValid() {
|
|
20
|
+
const [isValid, error] = this.__validator(this._value);
|
|
21
|
+
if (!isValid)
|
|
22
|
+
this._error = error;
|
|
23
|
+
return this.isValid = isValid;
|
|
24
|
+
}
|
|
25
|
+
get value() {
|
|
26
|
+
return this._value;
|
|
27
|
+
}
|
|
28
|
+
set value(value) {
|
|
29
|
+
this._value = value;
|
|
30
|
+
this.checkValid();
|
|
31
|
+
}
|
|
32
|
+
get error() {
|
|
33
|
+
return this._error ?? '';
|
|
34
|
+
}
|
|
35
|
+
set error(value) {
|
|
36
|
+
this._error = value;
|
|
37
|
+
}
|
|
38
|
+
get valueClear() {
|
|
39
|
+
return this._value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FormField } from "../FormField";
|
|
2
|
+
type DateTimeValue = Date | string;
|
|
3
|
+
export declare class DateTimeField extends FormField<DateTimeValue> {
|
|
4
|
+
min: DateTimeValue | null;
|
|
5
|
+
max: DateTimeValue | null;
|
|
6
|
+
example: string;
|
|
7
|
+
constructor(defaultValue?: DateTimeValue | null, options?: DateTimeFieldOptions);
|
|
8
|
+
set valueFromInput(value: string | null);
|
|
9
|
+
get value(): DateTimeValue | null;
|
|
10
|
+
set value(value: DateTimeValue | null);
|
|
11
|
+
get valueClear(): DateTimeValue | null;
|
|
12
|
+
checkValid(): boolean;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { FormField } from "../FormField";
|
|
2
|
+
export class DateTimeField extends FormField {
|
|
3
|
+
min;
|
|
4
|
+
max;
|
|
5
|
+
example;
|
|
6
|
+
constructor(defaultValue = null, options = {}) {
|
|
7
|
+
if (defaultValue)
|
|
8
|
+
defaultValue = defaultValue instanceof Date ? defaultValue : new Date(defaultValue);
|
|
9
|
+
super(defaultValue, options);
|
|
10
|
+
this.min = options.min ?? null;
|
|
11
|
+
this.max = options.max ?? null;
|
|
12
|
+
this.example = options.example ?? 'Correctly date';
|
|
13
|
+
}
|
|
14
|
+
set valueFromInput(value) {
|
|
15
|
+
if (typeof value === 'string')
|
|
16
|
+
this.value = new Date(value);
|
|
17
|
+
else
|
|
18
|
+
this.value = value;
|
|
19
|
+
}
|
|
20
|
+
get value() {
|
|
21
|
+
return this._value;
|
|
22
|
+
}
|
|
23
|
+
set value(value) {
|
|
24
|
+
super.value = value;
|
|
25
|
+
}
|
|
26
|
+
get valueClear() {
|
|
27
|
+
return this._value;
|
|
28
|
+
}
|
|
29
|
+
checkValid() {
|
|
30
|
+
super.checkValid();
|
|
31
|
+
if (this.isValid === false)
|
|
32
|
+
return this.isValid;
|
|
33
|
+
if (this.isEmpty && !this.isRequired) {
|
|
34
|
+
this.isValid = true;
|
|
35
|
+
this._value = null;
|
|
36
|
+
return this.isValid;
|
|
37
|
+
}
|
|
38
|
+
console.log('if (this.isEmpty', this.isValid);
|
|
39
|
+
if (!(this._value instanceof Date) || isNaN(this._value.getTime())) {
|
|
40
|
+
this.isValid = false;
|
|
41
|
+
this._error = this.example ? `Example: ${this.example}` : 'Invalid format';
|
|
42
|
+
return this.isValid;
|
|
43
|
+
}
|
|
44
|
+
console.log('if (!(this._value instanceof', this.isValid);
|
|
45
|
+
this.isValid = true;
|
|
46
|
+
this._error = null;
|
|
47
|
+
return this.isValid;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FormField } from "../FormField";
|
|
2
|
+
export declare class NumberField extends FormField<number> {
|
|
3
|
+
min: number | null;
|
|
4
|
+
max: number | null;
|
|
5
|
+
example: string;
|
|
6
|
+
constructor(defaultValue?: number | null, options?: NumberFieldOptions);
|
|
7
|
+
set valueFromInput(value: string | number | null);
|
|
8
|
+
get valueClear(): number | null;
|
|
9
|
+
checkValid(): boolean;
|
|
10
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { FormField } from "../FormField";
|
|
2
|
+
export class NumberField extends FormField {
|
|
3
|
+
min;
|
|
4
|
+
max;
|
|
5
|
+
example;
|
|
6
|
+
constructor(defaultValue = null, options = {}) {
|
|
7
|
+
super(defaultValue, options);
|
|
8
|
+
this.min = options.min ?? null;
|
|
9
|
+
this.max = options.max ?? null;
|
|
10
|
+
this.example = options.example || 'Whole number';
|
|
11
|
+
}
|
|
12
|
+
set valueFromInput(value) {
|
|
13
|
+
if (typeof value === 'string') {
|
|
14
|
+
const trimmed_value = value.trim();
|
|
15
|
+
if (trimmed_value.length === 0)
|
|
16
|
+
value = null;
|
|
17
|
+
else
|
|
18
|
+
value = Number(trimmed_value);
|
|
19
|
+
}
|
|
20
|
+
this.value = value;
|
|
21
|
+
}
|
|
22
|
+
get valueClear() {
|
|
23
|
+
return this._value;
|
|
24
|
+
}
|
|
25
|
+
checkValid() {
|
|
26
|
+
super.checkValid();
|
|
27
|
+
if (this.isValid === false)
|
|
28
|
+
return this.isValid;
|
|
29
|
+
if (this.isEmpty && !this.isRequired) {
|
|
30
|
+
this.isValid = true;
|
|
31
|
+
this._value = null;
|
|
32
|
+
return this.isValid;
|
|
33
|
+
}
|
|
34
|
+
if (typeof this._value !== 'number' || isNaN(this._value)) {
|
|
35
|
+
this.isValid = false;
|
|
36
|
+
this._error = this.example ? `Example: ${this.example}` : 'Invalid format';
|
|
37
|
+
return this.isValid;
|
|
38
|
+
}
|
|
39
|
+
if (this.min !== null && this._value < this.min) {
|
|
40
|
+
this.isValid = false;
|
|
41
|
+
this._error = `Minimum value: ${this.min}`;
|
|
42
|
+
return this.isValid;
|
|
43
|
+
}
|
|
44
|
+
if (this.max !== null && this._value > this.max) {
|
|
45
|
+
this.isValid = false;
|
|
46
|
+
this._error = `Maximum value: ${this.max}`;
|
|
47
|
+
return this.isValid;
|
|
48
|
+
}
|
|
49
|
+
this.isValid = true;
|
|
50
|
+
this._error = null;
|
|
51
|
+
return this.isValid;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FormField } from "../FormField";
|
|
2
|
+
export declare class StringField extends FormField<string> {
|
|
3
|
+
minLength: number | null;
|
|
4
|
+
maxLength: number | null;
|
|
5
|
+
regex: RegExp;
|
|
6
|
+
example: string;
|
|
7
|
+
mask: string;
|
|
8
|
+
constructor(defaultValue?: string, options?: StringFieldOptions);
|
|
9
|
+
protected __add_mask(): string | null;
|
|
10
|
+
get valueClear(): string | null;
|
|
11
|
+
get value(): string | null;
|
|
12
|
+
set value(value: string);
|
|
13
|
+
checkValid(): boolean;
|
|
14
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { FormField } from "../FormField";
|
|
2
|
+
export class StringField extends FormField {
|
|
3
|
+
minLength;
|
|
4
|
+
maxLength;
|
|
5
|
+
regex;
|
|
6
|
+
example;
|
|
7
|
+
mask;
|
|
8
|
+
constructor(defaultValue = '', options = {}) {
|
|
9
|
+
super(defaultValue, options);
|
|
10
|
+
this.minLength = options.minLength ?? null;
|
|
11
|
+
this.maxLength = options.maxLength ?? null;
|
|
12
|
+
this.regex = options.regex instanceof RegExp ? options.regex : new RegExp(options.regex || '.*');
|
|
13
|
+
this.example = options.example ?? '';
|
|
14
|
+
this.mask = options.mask ?? '';
|
|
15
|
+
}
|
|
16
|
+
__add_mask() {
|
|
17
|
+
let i = 0;
|
|
18
|
+
let value = this._value ?? '';
|
|
19
|
+
return this.mask.replace(/_/g, function () {
|
|
20
|
+
return i < value.length ? value.charAt(i++) || value.charAt(i) : '_';
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
get valueClear() {
|
|
24
|
+
if (!this.mask)
|
|
25
|
+
return this._value;
|
|
26
|
+
return this.__add_mask();
|
|
27
|
+
}
|
|
28
|
+
get value() {
|
|
29
|
+
return this._value;
|
|
30
|
+
}
|
|
31
|
+
set value(value) {
|
|
32
|
+
super.value = value;
|
|
33
|
+
}
|
|
34
|
+
checkValid() {
|
|
35
|
+
super.checkValid();
|
|
36
|
+
if (this.isValid === false)
|
|
37
|
+
return this.isValid;
|
|
38
|
+
if (this.isEmpty && !this.isRequired) {
|
|
39
|
+
this.isValid = true;
|
|
40
|
+
this._value = '';
|
|
41
|
+
return this.isValid;
|
|
42
|
+
}
|
|
43
|
+
if (this.minLength !== null && this.minLength > this._value.length) {
|
|
44
|
+
this.isValid = false;
|
|
45
|
+
this._error = `Minimum ${this.minLength} characters`;
|
|
46
|
+
return this.isValid;
|
|
47
|
+
}
|
|
48
|
+
this._error = null;
|
|
49
|
+
if (this.maxLength !== null && this.maxLength < this._value.length) {
|
|
50
|
+
this.isValid = false;
|
|
51
|
+
this._error = `Maximum ${this.maxLength} characters`;
|
|
52
|
+
return this.isValid;
|
|
53
|
+
}
|
|
54
|
+
this._error = null;
|
|
55
|
+
if (!this.regex.test(this._value)) {
|
|
56
|
+
this.isValid = false;
|
|
57
|
+
this._error = this.example ? `Example: ${this.example}` : 'Invalid format';
|
|
58
|
+
return this.isValid;
|
|
59
|
+
}
|
|
60
|
+
this.isValid = true;
|
|
61
|
+
this._error = null;
|
|
62
|
+
return this.isValid;
|
|
63
|
+
}
|
|
64
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "adaptform",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Library of adaptive forms of validation",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"prepublishOnly": "npm run build",
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"forms",
|
|
15
|
+
"validation",
|
|
16
|
+
"typescript"
|
|
17
|
+
],
|
|
18
|
+
"author": "ArhiMEN",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"typescript": "^5.9.2"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
]
|
|
27
|
+
}
|