adaptform 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
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,6 +1,111 @@
1
- export * from './core/FieldOptions';
2
- export * from './core/FormField';
3
- export * from './core/string/StringField';
4
- export * from './core/number/NumberField';
5
- export * from './core/date/DateTimeField';
6
- export * from './core/Form';
1
+ export declare class DateTimeField extends FormField<Date, string> {
2
+ mode: "date" | "datetime";
3
+ min: Date | null;
4
+ max: Date | null;
5
+ example: string;
6
+ constructor(defaultValue?: DateValue, options?: DateTimeFieldOptions);
7
+ protected normalizeValue(value: any): Date | null;
8
+ get valueClear(): string | null;
9
+ private typeValidator;
10
+ private minValidator;
11
+ /** 🔹 Максимальная дата */
12
+ private maxValidator;
13
+ private formatDate;
14
+ }
15
+
16
+ export declare interface DateTimeFieldOptions extends FormFieldOptions {
17
+ mode?: "date" | "datetime";
18
+ min?: Date | string | number;
19
+ max?: Date | string | number;
20
+ example?: string;
21
+ }
22
+
23
+ declare type DateValue = Date | string | number | null;
24
+
25
+ export declare abstract class Form<TFields extends Record<string, any> = any> {
26
+ private readonly _errors;
27
+ protected get declaredFields(): (keyof TFields)[];
28
+ checkValid(): boolean;
29
+ get valueObject(): Record<keyof TFields, any>;
30
+ get valueArray(): any[];
31
+ get errors(): Record<string, any>;
32
+ set errors(errors: Record<string, any>);
33
+ set fields(values: Partial<Record<keyof TFields, any>>);
34
+ setFieldValue<K extends keyof TFields>(key: K, value: any): void;
35
+ getFieldValue<K extends keyof TFields>(key: K): any;
36
+ reset(): this;
37
+ }
38
+
39
+ export declare abstract class FormField<Value = any, ClearValue = Value> {
40
+ protected _value: Value | null;
41
+ protected defaultValue: Value | null;
42
+ protected _error: string | null;
43
+ isRequired: boolean;
44
+ isValid: boolean | null;
45
+ validators: Array<(value: Value | null) => string | null>;
46
+ protected constructor(defaultValue?: Value | null, options?: FormFieldOptions);
47
+ get value(): Value | null;
48
+ set value(value: Value | null);
49
+ get error(): string;
50
+ set error(value: string | null);
51
+ set __valid_error(value: string | null);
52
+ reset(): void;
53
+ protected normalizeValue(value: Value | null): Value | null;
54
+ protected fieldIsEmpty(value: Value | null): boolean;
55
+ get isEmpty(): boolean;
56
+ protected fieldIsZero(value: Value | null): boolean;
57
+ get isZero(): boolean;
58
+ checkValid(): boolean;
59
+ abstract get valueClear(): ClearValue | null;
60
+ }
61
+
62
+ export declare interface FormFieldOptions {
63
+ isRequired?: boolean;
64
+ validators?: Array<(value: any) => string | null>;
65
+ }
66
+
67
+ export declare class NumberField extends FormField<number> {
68
+ min: number | null;
69
+ max: number | null;
70
+ example: string;
71
+ constructor(defaultValue?: number | null, options?: NumberFieldOptions);
72
+ protected normalizeValue(value: any): number | null;
73
+ get valueClear(): number | null;
74
+ private typeValidator;
75
+ private minValidator;
76
+ private maxValidator;
77
+ }
78
+
79
+ export declare interface NumberFieldOptions extends FormFieldOptions {
80
+ min?: number;
81
+ max?: number;
82
+ example?: string;
83
+ }
84
+
85
+ export declare class StringField extends FormField<string> {
86
+ minLength: number | null;
87
+ maxLength: number | null;
88
+ regex: RegExp;
89
+ example: string;
90
+ mask: string | null;
91
+ returnWithMask: boolean;
92
+ constructor(defaultValue?: string, options?: StringFieldOptions);
93
+ protected normalizeValue(value: string | null): string | null;
94
+ get valueClear(): string | null;
95
+ get value(): string | null;
96
+ set value(value: string | null);
97
+ private minLengthValidator;
98
+ private maxLengthValidator;
99
+ private regexValidator;
100
+ }
101
+
102
+ export declare interface StringFieldOptions extends FormFieldOptions {
103
+ minLength?: number;
104
+ maxLength?: number;
105
+ regex?: RegExp | string;
106
+ mask?: string;
107
+ example?: string;
108
+ returnWithMask?: boolean;
109
+ }
110
+
111
+ export { }
package/package.json CHANGED
@@ -1,29 +1,30 @@
1
1
  {
2
2
  "name": "adaptform",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Library of adaptive forms of validation",
5
- "main": "index.js",
6
- "module": "./dist/index.mjs",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs.js",
7
+ "module": "./dist/index.es.js",
7
8
  "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.es.js",
12
+ "require": "./dist/index.cjs.js"
13
+ }
14
+ },
8
15
  "files": [
9
16
  "dist/**/*"
10
17
  ],
11
18
  "scripts": {
19
+ "dev": "vite",
12
20
  "build": "vite build",
13
21
  "prepublishOnly": "npm run build"
14
22
  },
15
- "keywords": [
16
- "form",
17
- "validation",
18
- "typescript"
19
- ],
23
+ "keywords": ["form", "validation", "typescript"],
20
24
  "author": "ArhiMEN",
21
25
  "license": "MIT",
22
- "type": "module",
23
- "dependencies": {
24
- "vite": "^7.1.1"
25
- },
26
26
  "devDependencies": {
27
+ "vite": "^7.1.1",
27
28
  "vite-plugin-dts": "^4.0.0",
28
29
  "typescript": "^5.2.2"
29
30
  }
@@ -1,23 +0,0 @@
1
- export interface FormFieldOptions {
2
- isRequired?: boolean;
3
- validators?: Array<(value: any) => string | null>;
4
- }
5
- export interface StringFieldOptions extends FormFieldOptions {
6
- minLength?: number;
7
- maxLength?: number;
8
- regex?: RegExp | string;
9
- mask?: string;
10
- example?: string;
11
- returnWithMask?: boolean;
12
- }
13
- export interface NumberFieldOptions extends FormFieldOptions {
14
- min?: number;
15
- max?: number;
16
- example?: string;
17
- }
18
- export interface DateTimeFieldOptions extends FormFieldOptions {
19
- mode?: "date" | "datetime";
20
- min?: Date | string | number;
21
- max?: Date | string | number;
22
- example?: string;
23
- }
@@ -1,13 +0,0 @@
1
- export declare abstract class Form<TFields extends Record<string, any> = any> {
2
- private readonly _errors;
3
- protected get declaredFields(): (keyof TFields)[];
4
- checkValid(): boolean;
5
- get valueObject(): Record<keyof TFields, any>;
6
- get valueArray(): any[];
7
- get errors(): Record<string, any>;
8
- set errors(errors: Record<string, any>);
9
- set fields(values: Partial<Record<keyof TFields, any>>);
10
- setFieldValue<K extends keyof TFields>(key: K, value: any): void;
11
- getFieldValue<K extends keyof TFields>(key: K): any;
12
- reset(): this;
13
- }
@@ -1,23 +0,0 @@
1
- import { FormFieldOptions } from './FieldOptions';
2
- export declare abstract class FormField<Value = any, ClearValue = Value> {
3
- protected _value: Value | null;
4
- protected defaultValue: Value | null;
5
- protected _error: string | null;
6
- isRequired: boolean;
7
- isValid: boolean | null;
8
- validators: Array<(value: Value | null) => string | null>;
9
- protected constructor(defaultValue?: Value | null, options?: FormFieldOptions);
10
- get value(): Value | null;
11
- set value(value: Value | null);
12
- get error(): string;
13
- set error(value: string | null);
14
- set __valid_error(value: string | null);
15
- reset(): void;
16
- protected normalizeValue(value: Value | null): Value | null;
17
- protected fieldIsEmpty(value: Value | null): boolean;
18
- get isEmpty(): boolean;
19
- protected fieldIsZero(value: Value | null): boolean;
20
- get isZero(): boolean;
21
- checkValid(): boolean;
22
- abstract get valueClear(): ClearValue | null;
23
- }
@@ -1,18 +0,0 @@
1
- import { FormField } from '../FormField';
2
- import { DateTimeFieldOptions } from '../FieldOptions';
3
- type DateValue = Date | string | number | null;
4
- export declare class DateTimeField extends FormField<Date, string> {
5
- mode: "date" | "datetime";
6
- min: Date | null;
7
- max: Date | null;
8
- example: string;
9
- 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;
17
- }
18
- export {};
@@ -1,13 +0,0 @@
1
- import { FormField } from '../FormField';
2
- import { NumberFieldOptions } from '../FieldOptions';
3
- export declare class NumberField extends FormField<number> {
4
- min: number | null;
5
- max: number | null;
6
- example: string;
7
- constructor(defaultValue?: number | null, options?: NumberFieldOptions);
8
- protected normalizeValue(value: any): number | null;
9
- get valueClear(): number | null;
10
- private typeValidator;
11
- private minValidator;
12
- private maxValidator;
13
- }
@@ -1,18 +0,0 @@
1
- import { FormField } from '../FormField';
2
- import { StringFieldOptions } from '../FieldOptions';
3
- export declare class StringField extends FormField<string> {
4
- minLength: number | null;
5
- maxLength: number | null;
6
- regex: RegExp;
7
- example: string;
8
- mask: string | null;
9
- returnWithMask: boolean;
10
- constructor(defaultValue?: string, options?: StringFieldOptions);
11
- protected normalizeValue(value: string | null): string | null;
12
- get valueClear(): string | null;
13
- get value(): string | null;
14
- set value(value: string | null);
15
- private minLengthValidator;
16
- private maxLengthValidator;
17
- private regexValidator;
18
- }