adaptform 1.0.0 → 1.1.1
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 +9 -6
- package/dist/core/Form.d.ts +6 -7
- package/dist/core/FormField.d.ts +8 -4
- package/dist/core/date/DateTimeField.d.ts +15 -11
- package/dist/core/number/NumberField.d.ts +6 -3
- package/dist/core/string/StringField.d.ts +9 -5
- package/dist/index.cjs.js +1 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.es.js +244 -0
- package/package.json +22 -18
- package/dist/adaptform.es.js +0 -220
- package/dist/adaptform.umd.js +0 -1
- package/dist/core/FieldOptions.js +0 -1
- package/dist/core/Form.js +0 -91
- package/dist/core/FormField.js +0 -54
- package/dist/core/SimpleField.d.ts +0 -19
- package/dist/core/SimpleField.js +0 -41
- package/dist/core/common/regex.d.ts +0 -0
- package/dist/core/common/regex.js +0 -1
- package/dist/core/date/DateTimeField.js +0 -49
- package/dist/core/number/NumberField.js +0 -53
- package/dist/core/string/StringField.js +0 -64
- package/dist/index.js +0 -5
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
interface FormFieldOptions {
|
|
1
|
+
export interface FormFieldOptions {
|
|
2
2
|
isRequired?: boolean;
|
|
3
|
+
validators?: Array<(value: any) => string | null>;
|
|
3
4
|
}
|
|
4
|
-
interface StringFieldOptions extends FormFieldOptions {
|
|
5
|
+
export interface StringFieldOptions extends FormFieldOptions {
|
|
5
6
|
minLength?: number;
|
|
6
7
|
maxLength?: number;
|
|
7
8
|
regex?: RegExp | string;
|
|
8
9
|
mask?: string;
|
|
9
10
|
example?: string;
|
|
11
|
+
returnWithMask?: boolean;
|
|
10
12
|
}
|
|
11
|
-
interface NumberFieldOptions extends FormFieldOptions {
|
|
13
|
+
export interface NumberFieldOptions extends FormFieldOptions {
|
|
12
14
|
min?: number;
|
|
13
15
|
max?: number;
|
|
14
16
|
example?: string;
|
|
15
17
|
}
|
|
16
|
-
interface DateTimeFieldOptions extends FormFieldOptions {
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
export interface DateTimeFieldOptions extends FormFieldOptions {
|
|
19
|
+
mode?: "date" | "datetime";
|
|
20
|
+
min?: Date | string | number;
|
|
21
|
+
max?: Date | string | number;
|
|
19
22
|
example?: string;
|
|
20
23
|
}
|
package/dist/core/Form.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
export declare class Form {
|
|
2
|
-
private readonly _declaredFields;
|
|
1
|
+
export declare abstract class Form<TFields extends Record<string, any> = any> {
|
|
3
2
|
private readonly _errors;
|
|
4
|
-
|
|
5
|
-
init(): this;
|
|
3
|
+
protected get declaredFields(): (keyof TFields)[];
|
|
6
4
|
checkValid(): boolean;
|
|
7
|
-
get valueObject(): Record<
|
|
5
|
+
get valueObject(): Record<keyof TFields, any>;
|
|
8
6
|
get valueArray(): any[];
|
|
9
7
|
get errors(): Record<string, any>;
|
|
10
8
|
set errors(errors: Record<string, any>);
|
|
11
|
-
set
|
|
12
|
-
|
|
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;
|
|
13
12
|
reset(): this;
|
|
14
13
|
}
|
package/dist/core/FormField.d.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
import { FormFieldOptions } from './FieldOptions';
|
|
2
|
+
export declare abstract class FormField<Value = any, ClearValue = Value> {
|
|
2
3
|
protected _value: Value | null;
|
|
3
4
|
protected defaultValue: Value | null;
|
|
4
5
|
protected _error: string | null;
|
|
5
6
|
isRequired: boolean;
|
|
6
7
|
isValid: boolean | null;
|
|
8
|
+
validators: Array<(value: Value | null) => string | null>;
|
|
7
9
|
protected constructor(defaultValue?: Value | null, options?: FormFieldOptions);
|
|
8
10
|
get value(): Value | null;
|
|
9
11
|
set value(value: Value | null);
|
|
10
12
|
get error(): string;
|
|
11
13
|
set error(value: string | null);
|
|
14
|
+
set __valid_error(value: string | null);
|
|
12
15
|
reset(): void;
|
|
13
|
-
|
|
16
|
+
protected normalizeValue(value: Value | null): Value | null;
|
|
17
|
+
protected fieldIsEmpty(value: Value | null): boolean;
|
|
14
18
|
get isEmpty(): boolean;
|
|
15
|
-
fieldIsZero(value: Value | null): boolean;
|
|
19
|
+
protected fieldIsZero(value: Value | null): boolean;
|
|
16
20
|
get isZero(): boolean;
|
|
17
21
|
checkValid(): boolean;
|
|
18
|
-
abstract get valueClear():
|
|
22
|
+
abstract get valueClear(): ClearValue | null;
|
|
19
23
|
}
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import { FormField } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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;
|
|
6
8
|
example: string;
|
|
7
|
-
constructor(defaultValue?:
|
|
8
|
-
|
|
9
|
-
get
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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;
|
|
13
17
|
}
|
|
14
18
|
export {};
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { FormField } from
|
|
1
|
+
import { FormField } from '../FormField';
|
|
2
|
+
import { NumberFieldOptions } from '../FieldOptions';
|
|
2
3
|
export declare class NumberField extends FormField<number> {
|
|
3
4
|
min: number | null;
|
|
4
5
|
max: number | null;
|
|
5
6
|
example: string;
|
|
6
7
|
constructor(defaultValue?: number | null, options?: NumberFieldOptions);
|
|
7
|
-
|
|
8
|
+
protected normalizeValue(value: any): number | null;
|
|
8
9
|
get valueClear(): number | null;
|
|
9
|
-
|
|
10
|
+
private typeValidator;
|
|
11
|
+
private minValidator;
|
|
12
|
+
private maxValidator;
|
|
10
13
|
}
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import { FormField } from
|
|
1
|
+
import { FormField } from '../FormField';
|
|
2
|
+
import { StringFieldOptions } from '../FieldOptions';
|
|
2
3
|
export declare class StringField extends FormField<string> {
|
|
3
4
|
minLength: number | null;
|
|
4
5
|
maxLength: number | null;
|
|
5
6
|
regex: RegExp;
|
|
6
7
|
example: string;
|
|
7
|
-
mask: string;
|
|
8
|
+
mask: string | null;
|
|
9
|
+
returnWithMask: boolean;
|
|
8
10
|
constructor(defaultValue?: string, options?: StringFieldOptions);
|
|
9
|
-
protected
|
|
11
|
+
protected normalizeValue(value: string | null): string | null;
|
|
10
12
|
get valueClear(): string | null;
|
|
11
13
|
get value(): string | null;
|
|
12
|
-
set value(value: string);
|
|
13
|
-
|
|
14
|
+
set value(value: string | null);
|
|
15
|
+
private minLengthValidator;
|
|
16
|
+
private maxLengthValidator;
|
|
17
|
+
private regexValidator;
|
|
14
18
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,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 m 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=u=>String(u).padStart(2,"0"),i=e.getFullYear(),s=t(e.getMonth()+1),l=t(e.getDate()),a=t(e.getHours()),h=t(e.getMinutes());return this.mode==="date"?`${i}-${s}-${l}`:`${i}-${s}-${l} ${a}:${h}`}}class c{_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}}exports.DateTimeField=o;exports.Form=c;exports.FormField=r;exports.NumberField=d;exports.StringField=m;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
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';
|
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
class r {
|
|
2
|
+
_value;
|
|
3
|
+
defaultValue;
|
|
4
|
+
_error = null;
|
|
5
|
+
isRequired;
|
|
6
|
+
isValid;
|
|
7
|
+
validators;
|
|
8
|
+
constructor(t, e = {}) {
|
|
9
|
+
this._value = t ?? null, this.defaultValue = t ?? null, this.isValid = t === null ? null : !0, this.isRequired = e.isRequired ?? !0, this.validators = e.validators ?? [];
|
|
10
|
+
}
|
|
11
|
+
get value() {
|
|
12
|
+
return this._value;
|
|
13
|
+
}
|
|
14
|
+
set value(t) {
|
|
15
|
+
this._value = this.normalizeValue(t), this.checkValid();
|
|
16
|
+
}
|
|
17
|
+
get error() {
|
|
18
|
+
return this._error ?? "";
|
|
19
|
+
}
|
|
20
|
+
set error(t) {
|
|
21
|
+
this._error = t;
|
|
22
|
+
}
|
|
23
|
+
set __valid_error(t) {
|
|
24
|
+
const [e = "", i = ""] = (this.error || "").split(" | ");
|
|
25
|
+
t ? e || (this.error = i ? `${t} | ${this.error}` : t) : this.error = i || null;
|
|
26
|
+
}
|
|
27
|
+
reset() {
|
|
28
|
+
this._value = this.defaultValue ?? null, this._error = null, this.isValid = this.defaultValue === null ? null : !0;
|
|
29
|
+
}
|
|
30
|
+
normalizeValue(t) {
|
|
31
|
+
return t;
|
|
32
|
+
}
|
|
33
|
+
fieldIsEmpty(t) {
|
|
34
|
+
return t == null || t === "" ? !0 : typeof t == "object" && Object.keys(t).length === 0;
|
|
35
|
+
}
|
|
36
|
+
get isEmpty() {
|
|
37
|
+
return this.fieldIsEmpty(this._value);
|
|
38
|
+
}
|
|
39
|
+
fieldIsZero(t) {
|
|
40
|
+
return t === 0 || t === "0";
|
|
41
|
+
}
|
|
42
|
+
get isZero() {
|
|
43
|
+
return this.fieldIsZero(this._value);
|
|
44
|
+
}
|
|
45
|
+
checkValid() {
|
|
46
|
+
if (this.isEmpty && !this.isZero)
|
|
47
|
+
return this.isValid = !this.isRequired, this.isValid;
|
|
48
|
+
this.isValid = !0;
|
|
49
|
+
for (const t of this.validators) {
|
|
50
|
+
const e = t(this._value);
|
|
51
|
+
if (e)
|
|
52
|
+
return this._error = e, this.isValid = !1, !1;
|
|
53
|
+
}
|
|
54
|
+
return this._error = null, this.isValid;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
class m extends r {
|
|
58
|
+
minLength;
|
|
59
|
+
maxLength;
|
|
60
|
+
regex;
|
|
61
|
+
example;
|
|
62
|
+
mask;
|
|
63
|
+
returnWithMask;
|
|
64
|
+
constructor(t = "", e = {}) {
|
|
65
|
+
super(t, e), this.minLength = e.minLength ?? null, this.maxLength = e.maxLength ?? null, this.regex = e.regex instanceof RegExp ? e.regex : new RegExp(e.regex || ".*"), this.example = e.example ?? "Enter a valid string", this.mask = e.mask ?? null, this.returnWithMask = e.returnWithMask ?? !1, this.validators.push(this.minLengthValidator.bind(this)), this.validators.push(this.maxLengthValidator.bind(this)), this.validators.push(this.regexValidator.bind(this));
|
|
66
|
+
}
|
|
67
|
+
normalizeValue(t) {
|
|
68
|
+
return this.fieldIsEmpty(t) ? "" : String(t);
|
|
69
|
+
}
|
|
70
|
+
get valueClear() {
|
|
71
|
+
if (this.returnWithMask)
|
|
72
|
+
return this._value === "" ? null : this._value;
|
|
73
|
+
if (!this.mask) return this._value || null;
|
|
74
|
+
const t = this.mask.split(/_/g).join("");
|
|
75
|
+
return (this._value || "").replace(new RegExp(`[${t}]`, "g"), "");
|
|
76
|
+
}
|
|
77
|
+
get value() {
|
|
78
|
+
return this._value;
|
|
79
|
+
}
|
|
80
|
+
set value(t) {
|
|
81
|
+
if (!this.mask || !t) {
|
|
82
|
+
super.value = t;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const e = this.mask.replace(/_/g, ""), i = t.replace(new RegExp(`[${e}]`, "g"), "");
|
|
86
|
+
if (!i) {
|
|
87
|
+
super.value = "";
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
let s = 0;
|
|
91
|
+
const l = this.mask.replace(/_/g, () => i[s++] ?? "_"), a = l.indexOf("_");
|
|
92
|
+
super.value = a === -1 ? l : l.slice(0, a);
|
|
93
|
+
}
|
|
94
|
+
minLengthValidator(t) {
|
|
95
|
+
return this.minLength === null || !t ? null : t.length < this.minLength ? `Minimum ${this.minLength} characters` : null;
|
|
96
|
+
}
|
|
97
|
+
maxLengthValidator(t) {
|
|
98
|
+
return this.maxLength === null || !t ? null : t.length > this.maxLength ? `Maximum ${this.maxLength} characters` : null;
|
|
99
|
+
}
|
|
100
|
+
regexValidator(t) {
|
|
101
|
+
return t ? this.regex.test(t) ? null : this.example ? `Example: ${this.example}` : "Invalid format" : null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
class d extends r {
|
|
105
|
+
min;
|
|
106
|
+
max;
|
|
107
|
+
example;
|
|
108
|
+
constructor(t = null, e = {}) {
|
|
109
|
+
super(t, e), this.min = e.min ?? null, this.max = e.max ?? null, this.example = e.example ?? "Enter a valid number", this.validators.push(this.typeValidator.bind(this)), this.validators.push(this.minValidator.bind(this)), this.validators.push(this.maxValidator.bind(this));
|
|
110
|
+
}
|
|
111
|
+
normalizeValue(t) {
|
|
112
|
+
if (this.fieldIsEmpty(t)) return null;
|
|
113
|
+
if (typeof t == "number") return isNaN(t) ? null : t;
|
|
114
|
+
if (typeof t == "string") {
|
|
115
|
+
const e = t.trim();
|
|
116
|
+
if (e === "") return null;
|
|
117
|
+
const i = Number(e);
|
|
118
|
+
return isNaN(i) ? null : i;
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
get valueClear() {
|
|
123
|
+
return this._value;
|
|
124
|
+
}
|
|
125
|
+
typeValidator(t) {
|
|
126
|
+
return t === null ? this.isRequired ? `Example: ${this.example}` : null : typeof t != "number" || isNaN(t) ? this.example ? `Example: ${this.example}` : "Invalid number" : null;
|
|
127
|
+
}
|
|
128
|
+
minValidator(t) {
|
|
129
|
+
return t === null || this.min === null ? null : t < this.min ? `Minimum value: ${this.min}` : null;
|
|
130
|
+
}
|
|
131
|
+
maxValidator(t) {
|
|
132
|
+
return t === null || this.max === null ? null : t > this.max ? `Maximum value: ${this.max}` : null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
class o extends r {
|
|
136
|
+
mode;
|
|
137
|
+
min;
|
|
138
|
+
max;
|
|
139
|
+
example;
|
|
140
|
+
constructor(t = null, e = {}) {
|
|
141
|
+
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:ss"), this.validators.push(this.typeValidator.bind(this)), this.validators.push(this.minValidator.bind(this)), this.validators.push(this.maxValidator.bind(this));
|
|
143
|
+
}
|
|
144
|
+
normalizeValue(t) {
|
|
145
|
+
if (this.fieldIsEmpty(t)) return null;
|
|
146
|
+
if (t instanceof Date) return isNaN(t.getTime()) ? null : t;
|
|
147
|
+
if (typeof t == "string" || typeof t == "number") {
|
|
148
|
+
const e = new Date(t);
|
|
149
|
+
return isNaN(e.getTime()) ? null : e;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
get valueClear() {
|
|
154
|
+
if (!this._value) return null;
|
|
155
|
+
const t = (l) => String(l).padStart(2, "0"), e = this._value.getFullYear(), i = t(this._value.getMonth() + 1), s = t(this._value.getDate());
|
|
156
|
+
return this.mode === "date" ? `${e}-${i}-${s}` : this._value.toISOString();
|
|
157
|
+
}
|
|
158
|
+
typeValidator(t) {
|
|
159
|
+
return t === null ? this.isRequired ? `Example: ${this.example}` : null : !(t instanceof Date) || isNaN(t.getTime()) ? this.example ? `Example: ${this.example}` : "Invalid date format" : null;
|
|
160
|
+
}
|
|
161
|
+
minValidator(t) {
|
|
162
|
+
return !t || !this.min ? null : t < this.min ? `Date must be after ${this.formatDate(this.min)}` : null;
|
|
163
|
+
}
|
|
164
|
+
/** 🔹 Максимальная дата */
|
|
165
|
+
maxValidator(t) {
|
|
166
|
+
return !t || !this.max ? null : t > this.max ? `Date must be before ${this.formatDate(this.max)}` : null;
|
|
167
|
+
}
|
|
168
|
+
formatDate(t) {
|
|
169
|
+
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());
|
|
170
|
+
return this.mode === "date" ? `${i}-${s}-${l}` : `${i}-${s}-${l} ${a}:${h}`;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
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
|
+
}
|
|
238
|
+
export {
|
|
239
|
+
o as DateTimeField,
|
|
240
|
+
c as Form,
|
|
241
|
+
r as FormField,
|
|
242
|
+
d as NumberField,
|
|
243
|
+
m as StringField
|
|
244
|
+
};
|
package/package.json
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adaptform",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Library of adaptive forms of validation",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs.js",
|
|
7
|
+
"module": "./dist/index.es.js",
|
|
7
8
|
"types": "./dist/index.d.ts",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.es.js",
|
|
12
|
+
"require": "./dist/index.cjs.js"
|
|
13
|
+
}
|
|
12
14
|
},
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"validation",
|
|
16
|
-
"typescript"
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/**/*"
|
|
17
17
|
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "vite",
|
|
20
|
+
"build": "vite build",
|
|
21
|
+
"prepublishOnly": "npm run build"
|
|
22
|
+
},
|
|
23
|
+
"keywords": ["form", "validation", "typescript"],
|
|
18
24
|
"author": "ArhiMEN",
|
|
19
|
-
"license": "
|
|
20
|
-
"type": "module",
|
|
25
|
+
"license": "MIT",
|
|
21
26
|
"devDependencies": {
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
]
|
|
27
|
+
"vite": "^7.1.1",
|
|
28
|
+
"vite-plugin-dts": "^4.0.0",
|
|
29
|
+
"typescript": "^5.2.2"
|
|
30
|
+
}
|
|
27
31
|
}
|
package/dist/adaptform.es.js
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
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
|
-
};
|
package/dist/adaptform.umd.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"})});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/dist/core/Form.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
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
|
-
}
|
package/dist/core/FormField.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
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 {};
|
package/dist/core/SimpleField.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
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.js
DELETED