adaptform 1.1.2 → 1.1.4

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/index.cjs.js CHANGED
@@ -1 +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;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class r{_value;defaultValue;_error=null;isRequired;isValid;validators;constructor(e,t={}){this._value=e??null,this.defaultValue=e??null,this.isValid=e===null?null:!0,this.isRequired=t.isRequired??!0,this.validators=t.validators??[]}get value(){return this._value}set value(e){this._value=this.normalizeValue(e),this.checkValid()}get error(){return this._error??""}set error(e){this._error=e}set __valid_error(e){const[t="",i=""]=(this.error||"").split(" | ");e?t||(this.error=i?`${e} | ${this.error}`:e):this.error=i||null}reset(){this._value=this.defaultValue??null,this._error=null,this.isValid=this.defaultValue===null?null:!0}normalizeValue(e){return e}fieldIsEmpty(e){return e==null||e===""?!0:typeof e=="object"&&Object.keys(e).length===0}get isEmpty(){return this.fieldIsEmpty(this._value)}fieldIsZero(e){return e===0||e==="0"}get isZero(){return this.fieldIsZero(this._value)}checkValid(){if(this.isEmpty&&!this.isZero)return this.isValid=!this.isRequired,this.isValid;this.isValid=!0;for(const e of this.validators){const t=e(this._value);if(t)return this._error=t,this.isValid=!1,!1}return this._error=null,this.isValid}}class u{_errors={};get declaredFields(){return Object.keys(this).filter(e=>this[e]instanceof r)}checkValid(){let e=!0;for(const t of this.declaredFields){const i=this[t];i instanceof r&&(i.checkValid(),(i.isValid===!1||i.isValid===null&&i.isRequired)&&(e=!1))}return e}get valueObject(){const e={};for(const t of this.declaredFields){const i=this[t];e[t]=i instanceof r?i.valueClear:i}return e}get valueArray(){return this.declaredFields.map(e=>{const t=this[e];return t instanceof r?t.valueClear:t})}get errors(){return this._errors}set errors(e){for(const t in e){const i=this[t];i instanceof r?i.error=e[t]:this._errors[t]=e[t]}}set fields(e){for(const t in e){if(!this.declaredFields.includes(t))throw new Error(`Field "${t}" is not declared in form "${this.constructor.name}"`);const i=this[t],s=e[t];i instanceof r?i.value=s:this[t]=s}}setFieldValue(e,t){const i=this[e];i instanceof r?i.value=t:this[e]=t}getFieldValue(e){const t=this[e];return t instanceof r?t.valueClear:t}reset(){for(const e of this.declaredFields){const t=this[e];t instanceof r&&t.reset()}for(const e of Object.keys(this._errors))delete this._errors[e];return this}}class h extends r{minLength;maxLength;regex;example;mask;returnWithMask;constructor(e="",t={}){super(e,t),this.minLength=t.minLength??null,this.maxLength=t.maxLength??null,this.regex=t.regex instanceof RegExp?t.regex:new RegExp(t.regex||".*"),this.example=t.example??"Enter a valid string",this.mask=t.mask??null,this.returnWithMask=t.returnWithMask??!1,this.validators.push(this.minLengthValidator.bind(this)),this.validators.push(this.maxLengthValidator.bind(this)),this.validators.push(this.regexValidator.bind(this))}normalizeValue(e){return this.fieldIsEmpty(e)?"":String(e)}get valueClear(){if(this.returnWithMask)return this._value===""?null:this._value;if(!this.mask)return this._value||null;const e=this.mask.split(/_/g).join("");return(this._value||"").replace(new RegExp(`[${e}]`,"g"),"")}get value(){return this._value}set value(e){if(!this.mask||!e){super.value=e;return}const t=this.mask.replace(/_/g,""),i=e.replace(new RegExp(`[${t}]`,"g"),"");if(!i){super.value="";return}let s=0;const l=this.mask.replace(/_/g,()=>i[s++]??"_"),a=l.indexOf("_");super.value=a===-1?l:l.slice(0,a)}minLengthValidator(e){return this.minLength===null||!e?null:e.length<this.minLength?`Minimum ${this.minLength} characters`:null}maxLengthValidator(e){return this.maxLength===null||!e?null:e.length>this.maxLength?`Maximum ${this.maxLength} characters`:null}regexValidator(e){return e?this.regex.test(e)?null:this.example?`Example: ${this.example}`:"Invalid format":null}}class d extends r{min;max;example;constructor(e=null,t={}){super(e,t),this.min=t.min??null,this.max=t.max??null,this.example=t.example??"Enter a valid number",this.validators.push(this.typeValidator.bind(this)),this.validators.push(this.minValidator.bind(this)),this.validators.push(this.maxValidator.bind(this))}normalizeValue(e){if(this.fieldIsEmpty(e))return null;if(typeof e=="number")return isNaN(e)?null:e;if(typeof e=="string"){const t=e.trim();if(t==="")return null;const i=Number(t);return isNaN(i)?null:i}return null}get valueClear(){return this._value}typeValidator(e){return e===null?this.isRequired?`Example: ${this.example}`:null:typeof e!="number"||isNaN(e)?this.example?`Example: ${this.example}`:"Invalid number":null}minValidator(e){return e===null||this.min===null?null:e<this.min?`Minimum value: ${this.min}`:null}maxValidator(e){return e===null||this.max===null?null:e>this.max?`Maximum value: ${this.max}`:null}}class o extends r{mode;min;max;example;constructor(e=null,t={}){const i=e instanceof Date?e:e?new Date(e):null;super(i,t),this.mode=t.mode??"datetime",this.min=t.min?new Date(t.min):null,this.max=t.max?new Date(t.max):null,this.example=t.example??(this.mode==="date"?"YYYY-MM-DD":"YYYY-MM-DD HH:mm:ss"),this.validators.push(this.typeValidator.bind(this)),this.validators.push(this.minValidator.bind(this)),this.validators.push(this.maxValidator.bind(this))}normalizeValue(e){if(this.fieldIsEmpty(e))return null;if(e instanceof Date)return isNaN(e.getTime())?null:e;if(typeof e=="string"||typeof e=="number"){const t=new Date(e);return isNaN(t.getTime())?null:t}return null}get valueClear(){if(!this._value)return null;const e=l=>String(l).padStart(2,"0"),t=this._value.getFullYear(),i=e(this._value.getMonth()+1),s=e(this._value.getDate());return this.mode==="date"?`${t}-${i}-${s}`:this._value.toISOString()}typeValidator(e){return e===null?this.isRequired?`Example: ${this.example}`:null:!(e instanceof Date)||isNaN(e.getTime())?this.example?`Example: ${this.example}`:"Invalid date format":null}minValidator(e){return!e||!this.min?null:e<this.min?`Date must be after ${this.formatDate(this.min)}`:null}maxValidator(e){return!e||!this.max?null:e>this.max?`Date must be before ${this.formatDate(this.max)}`:null}formatDate(e){const t=f=>String(f).padStart(2,"0"),i=e.getFullYear(),s=t(e.getMonth()+1),l=t(e.getDate()),a=t(e.getHours()),c=t(e.getMinutes());return this.mode==="date"?`${i}-${s}-${l}`:`${i}-${s}-${l} ${a}:${c}`}}const m=Object.freeze(Object.defineProperty({__proto__:null},Symbol.toStringTag,{value:"Module"})),g={Form:u,FormField:r,StringField:h,NumberField:d,DateTimeField:o,FieldOptions:m};exports.DateTimeField=o;exports.FieldOptions=m;exports.Form=u;exports.FormField=r;exports.NumberField=d;exports.StringField=h;exports.default=g;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,13 @@
1
+ declare const AdaptForm: {
2
+ Form: typeof Form;
3
+ FormField: typeof FormField;
4
+ StringField: typeof StringField;
5
+ NumberField: typeof NumberField;
6
+ DateTimeField: typeof DateTimeField;
7
+ FieldOptions: typeof FieldOptions;
8
+ };
9
+ export default AdaptForm;
10
+
1
11
  export declare class DateTimeField extends FormField<Date, string> {
2
12
  mode: "date" | "datetime";
3
13
  min: Date | null;
@@ -13,7 +23,7 @@ export declare class DateTimeField extends FormField<Date, string> {
13
23
  private formatDate;
14
24
  }
15
25
 
16
- export declare interface DateTimeFieldOptions extends FormFieldOptions {
26
+ declare interface DateTimeFieldOptions extends FormFieldOptions {
17
27
  mode?: "date" | "datetime";
18
28
  min?: Date | string | number;
19
29
  max?: Date | string | number;
@@ -22,6 +32,16 @@ export declare interface DateTimeFieldOptions extends FormFieldOptions {
22
32
 
23
33
  declare type DateValue = Date | string | number | null;
24
34
 
35
+ declare namespace FieldOptions {
36
+ export {
37
+ FormFieldOptions,
38
+ StringFieldOptions,
39
+ NumberFieldOptions,
40
+ DateTimeFieldOptions
41
+ }
42
+ }
43
+ export { FieldOptions }
44
+
25
45
  export declare abstract class Form<TFields extends Record<string, any> = any> {
26
46
  private readonly _errors;
27
47
  protected get declaredFields(): (keyof TFields)[];
@@ -59,7 +79,7 @@ export declare abstract class FormField<Value = any, ClearValue = Value> {
59
79
  abstract get valueClear(): ClearValue | null;
60
80
  }
61
81
 
62
- export declare interface FormFieldOptions {
82
+ declare interface FormFieldOptions {
63
83
  isRequired?: boolean;
64
84
  validators?: Array<(value: any) => string | null>;
65
85
  }
@@ -76,7 +96,7 @@ export declare class NumberField extends FormField<number> {
76
96
  private maxValidator;
77
97
  }
78
98
 
79
- export declare interface NumberFieldOptions extends FormFieldOptions {
99
+ declare interface NumberFieldOptions extends FormFieldOptions {
80
100
  min?: number;
81
101
  max?: number;
82
102
  example?: string;
@@ -99,7 +119,7 @@ export declare class StringField extends FormField<string> {
99
119
  private regexValidator;
100
120
  }
101
121
 
102
- export declare interface StringFieldOptions extends FormFieldOptions {
122
+ declare interface StringFieldOptions extends FormFieldOptions {
103
123
  minLength?: number;
104
124
  maxLength?: number;
105
125
  regex?: RegExp | string;
package/dist/index.es.js CHANGED
@@ -54,7 +54,72 @@ class r {
54
54
  return this._error = null, this.isValid;
55
55
  }
56
56
  }
57
- class m extends r {
57
+ class m {
58
+ _errors = {};
59
+ get declaredFields() {
60
+ return Object.keys(this).filter(
61
+ (t) => this[t] instanceof r
62
+ );
63
+ }
64
+ checkValid() {
65
+ let t = !0;
66
+ for (const e of this.declaredFields) {
67
+ const i = this[e];
68
+ i instanceof r && (i.checkValid(), (i.isValid === !1 || i.isValid === null && i.isRequired) && (t = !1));
69
+ }
70
+ return t;
71
+ }
72
+ get valueObject() {
73
+ const t = {};
74
+ for (const e of this.declaredFields) {
75
+ const i = this[e];
76
+ t[e] = i instanceof r ? i.valueClear : i;
77
+ }
78
+ return t;
79
+ }
80
+ get valueArray() {
81
+ return this.declaredFields.map((t) => {
82
+ const e = this[t];
83
+ return e instanceof r ? e.valueClear : e;
84
+ });
85
+ }
86
+ get errors() {
87
+ return this._errors;
88
+ }
89
+ set errors(t) {
90
+ for (const e in t) {
91
+ const i = this[e];
92
+ i instanceof r ? i.error = t[e] : this._errors[e] = t[e];
93
+ }
94
+ }
95
+ set fields(t) {
96
+ for (const e in t) {
97
+ if (!this.declaredFields.includes(e))
98
+ throw new Error(
99
+ `Field "${e}" is not declared in form "${this.constructor.name}"`
100
+ );
101
+ const i = this[e], s = t[e];
102
+ i instanceof r ? i.value = s : this[e] = s;
103
+ }
104
+ }
105
+ setFieldValue(t, e) {
106
+ const i = this[t];
107
+ i instanceof r ? i.value = e : this[t] = e;
108
+ }
109
+ getFieldValue(t) {
110
+ const e = this[t];
111
+ return e instanceof r ? e.valueClear : e;
112
+ }
113
+ reset() {
114
+ for (const t of this.declaredFields) {
115
+ const e = this[t];
116
+ e instanceof r && e.reset();
117
+ }
118
+ for (const t of Object.keys(this._errors)) delete this._errors[t];
119
+ return this;
120
+ }
121
+ }
122
+ class o extends r {
58
123
  minLength;
59
124
  maxLength;
60
125
  regex;
@@ -132,7 +197,7 @@ class d extends r {
132
197
  return t === null || this.max === null ? null : t > this.max ? `Maximum value: ${this.max}` : null;
133
198
  }
134
199
  }
135
- class o extends r {
200
+ class c extends r {
136
201
  mode;
137
202
  min;
138
203
  max;
@@ -170,75 +235,22 @@ class o extends r {
170
235
  return this.mode === "date" ? `${i}-${s}-${l}` : `${i}-${s}-${l} ${a}:${h}`;
171
236
  }
172
237
  }
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
+ const f = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
239
+ __proto__: null
240
+ }, Symbol.toStringTag, { value: "Module" })), g = {
241
+ Form: m,
242
+ FormField: r,
243
+ StringField: o,
244
+ NumberField: d,
245
+ DateTimeField: c,
246
+ FieldOptions: f
247
+ };
238
248
  export {
239
- o as DateTimeField,
240
- c as Form,
249
+ c as DateTimeField,
250
+ f as FieldOptions,
251
+ m as Form,
241
252
  r as FormField,
242
253
  d as NumberField,
243
- m as StringField
254
+ o as StringField,
255
+ g as default
244
256
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adaptform",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Library of adaptive forms of validation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs.js",