adaptform 2.0.5 → 2.2.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.
@@ -0,0 +1,387 @@
1
+ /** Shared mutation boundary; subclasses only describe state and their mutations. */
2
+ declare abstract class ObservableNode<TSnapshot> {
3
+ private readonly _stateStore;
4
+ protected abstract captureState(): TSnapshot;
5
+ protected stateChildren(): readonly ObservableNode<unknown>[];
6
+ private contains;
7
+ protected validateAfterMutation(callback: () => unknown): void;
8
+ protected batchUpdate<TResult>(callback: () => TResult, validate: () => unknown): TResult;
9
+ subscribe(listener: () => void): () => void;
10
+ getSnapshot(): TSnapshot;
11
+ protected update<TResult>(callback: () => TResult): TResult;
12
+ }
13
+
14
+ /** Snapshots contain data only: mutable built-ins and opaque instances are described. */
15
+ type SnapshotValue = null | undefined | string | number | boolean | bigint | readonly SnapshotValue[] | {
16
+ readonly [key: string]: SnapshotValue;
17
+ };
18
+ interface NodeSnapshot {
19
+ readonly isValid: boolean | null;
20
+ readonly isTouched: boolean;
21
+ readonly isDirty: boolean;
22
+ }
23
+ interface FieldSnapshot extends NodeSnapshot {
24
+ readonly rawValue: SnapshotValue;
25
+ readonly valueClear: SnapshotValue;
26
+ readonly errors: readonly string[];
27
+ }
28
+ interface FormSnapshot extends NodeSnapshot {
29
+ readonly fieldsValue: Readonly<Record<string, SnapshotValue>>;
30
+ readonly allErrors: Readonly<Record<string, readonly string[]>>;
31
+ readonly globalErrors: Readonly<Record<string, string>>;
32
+ readonly fields: Readonly<Record<string, FieldSnapshot | ArrayFieldSnapshot | ArrayFormSnapshot>>;
33
+ }
34
+ interface ArrayFieldSnapshot extends NodeSnapshot {
35
+ readonly itemsValue: readonly SnapshotValue[];
36
+ readonly errors: readonly string[];
37
+ readonly items: readonly FieldSnapshot[];
38
+ }
39
+ interface ArrayFormSnapshot extends NodeSnapshot {
40
+ readonly formsFieldsValue: readonly Readonly<Record<string, SnapshotValue>>[];
41
+ readonly errors: readonly string[];
42
+ readonly forms: readonly FormSnapshot[];
43
+ }
44
+
45
+ interface FieldPlugin<T = any> {
46
+ pluginOptions?: PluginOptions;
47
+ init?(field: Field<T>): void;
48
+ toRawValue?(value: any, field: Field<T>): any;
49
+ toValueClear?(value: any, field: Field<T>): any;
50
+ }
51
+ declare abstract class BasePlugin<T = any> implements FieldPlugin<T> {
52
+ pluginOptions: PluginOptions;
53
+ constructor(pluginOptions?: PluginOptions);
54
+ }
55
+
56
+ declare class ArrayField<T = any> extends ObservableNode<ArrayFieldSnapshot> {
57
+ protected factory: (value?: T) => Field<T>;
58
+ protected captureState(): ArrayFieldSnapshot;
59
+ protected options: ArrayFieldOptions;
60
+ protected _items: Field<T>[];
61
+ protected _errors: string[];
62
+ protected stateChildren(): readonly ObservableNode<unknown>[];
63
+ private _baseline;
64
+ private _hasValidationResult;
65
+ private _isTouched;
66
+ get isTouched(): boolean;
67
+ set isTouched(value: boolean);
68
+ constructor(factory: (value?: T) => Field<T>, options?: ArrayFieldOptions, initial?: (T | null)[]);
69
+ get isDirty(): boolean;
70
+ commitBaseline(): void;
71
+ set itemsValue(items: (T | null)[]);
72
+ setItemsValue(items: (T | null)[], validate?: boolean): void;
73
+ get itemsValue(): (T | null)[];
74
+ get items(): Field<T>[];
75
+ add(value?: T, force_check?: boolean): Field<T>;
76
+ remove(index: number): void;
77
+ clear(): void;
78
+ private isRequired;
79
+ checkValid(form?: Form): boolean;
80
+ checkAndSetValid(form?: Form): boolean;
81
+ get isValid(): boolean;
82
+ get error(): string;
83
+ set error(message: string | null);
84
+ get errors(): string[];
85
+ reset(): void;
86
+ }
87
+
88
+ declare class ArrayFormField<T extends Form = Form> extends ObservableNode<ArrayFormSnapshot> {
89
+ private FormConstructor;
90
+ private options;
91
+ protected captureState(): ArrayFormSnapshot;
92
+ private _forms;
93
+ private _errors;
94
+ protected stateChildren(): readonly ObservableNode<unknown>[];
95
+ private _baseline;
96
+ private _hasValidationResult;
97
+ private _isTouched;
98
+ get isTouched(): boolean;
99
+ set isTouched(value: boolean);
100
+ constructor(FormConstructor: new () => T, options?: ArrayFormFieldOptions, initial?: unknown[]);
101
+ get isDirty(): boolean;
102
+ commitBaseline(): void;
103
+ set forms(forms: unknown[]);
104
+ setForms(forms: unknown[], validate?: boolean): void;
105
+ get forms(): T[];
106
+ get formsFieldsValue(): FormFieldsValue<T>[];
107
+ add(data?: unknown, force_check?: boolean): T;
108
+ remove(index: number): void;
109
+ clear(): void;
110
+ reset(): void;
111
+ private isRequired;
112
+ checkValid(form?: Form): boolean;
113
+ checkAndSetValid(form?: Form): boolean;
114
+ get isValid(): boolean;
115
+ get errors(): string[];
116
+ get error(): string;
117
+ set error(message: string | null);
118
+ }
119
+
120
+ interface messagesOptions {
121
+ isRequired?: string;
122
+ validate?: string;
123
+ type?: string;
124
+ invalid?: string;
125
+ }
126
+ interface messagesStringOptions extends messagesOptions {
127
+ minLength?: string;
128
+ maxLength?: string;
129
+ pattern?: string;
130
+ }
131
+ interface messagesNumberOptions extends messagesOptions {
132
+ gt?: string;
133
+ ge?: string;
134
+ lt?: string;
135
+ le?: string;
136
+ }
137
+ interface messagesBooleanOptions extends messagesOptions {
138
+ mustBeTrue?: string;
139
+ mustBeFalse?: string;
140
+ }
141
+ interface messagesDateOptions extends messagesOptions {
142
+ minDate?: string;
143
+ maxDate?: string;
144
+ }
145
+ interface messagesDecimalOptions extends messagesOptions {
146
+ gt?: string;
147
+ ge?: string;
148
+ lt?: string;
149
+ le?: string;
150
+ decimalPlaces?: string;
151
+ maxDigits?: string;
152
+ }
153
+ interface messagesArrayFieldOptions extends messagesOptions {
154
+ minItems?: string;
155
+ maxItems?: string;
156
+ }
157
+ interface messagesArrayFormFieldOptions extends messagesOptions {
158
+ minForms?: string;
159
+ maxForms?: string;
160
+ }
161
+ interface FieldOptions {
162
+ isRequired?: boolean | ((form?: any) => boolean);
163
+ messages?: messagesOptions;
164
+ validate?: (value: any, form?: any) => boolean | string;
165
+ plugins?: FieldPlugin[];
166
+ }
167
+ interface StringOptions extends FieldOptions {
168
+ minLength?: number;
169
+ maxLength?: number;
170
+ pattern?: RegExp;
171
+ messages?: messagesStringOptions;
172
+ }
173
+ interface NumberOptions extends FieldOptions {
174
+ gt?: number;
175
+ ge?: number;
176
+ lt?: number;
177
+ le?: number;
178
+ messages?: messagesNumberOptions;
179
+ }
180
+ interface BooleanOptions extends FieldOptions {
181
+ mustBeTrue?: boolean;
182
+ mustBeFalse?: boolean;
183
+ messages?: messagesBooleanOptions;
184
+ }
185
+ interface DateOptions extends FieldOptions {
186
+ minDate?: Date;
187
+ maxDate?: Date;
188
+ isDatetime?: boolean;
189
+ messages?: messagesDateOptions;
190
+ }
191
+ interface DecimalOptions extends FieldOptions {
192
+ gt?: number;
193
+ ge?: number;
194
+ lt?: number;
195
+ le?: number;
196
+ decimalPlaces?: number;
197
+ maxDigits?: number;
198
+ messages?: messagesDecimalOptions;
199
+ }
200
+ interface ArrayFieldOptions {
201
+ isRequired?: boolean | ((form?: any) => boolean);
202
+ messages?: messagesArrayFieldOptions;
203
+ validate?: (value: any[], form?: any) => boolean | string;
204
+ minItems?: number;
205
+ maxItems?: number;
206
+ }
207
+ interface ArrayFormFieldOptions {
208
+ isRequired?: boolean | ((form?: any) => boolean);
209
+ messages?: messagesArrayFormFieldOptions;
210
+ validate?: (value: any[], form?: any) => boolean | string;
211
+ minForms?: number;
212
+ maxForms?: number;
213
+ }
214
+ interface PluginOptions {
215
+ priority?: number;
216
+ }
217
+ interface MaskPluginOptions {
218
+ maskFormat: string;
219
+ maskPlaceholder: string;
220
+ digitPattern: RegExp;
221
+ }
222
+
223
+ declare abstract class FieldType<T, O extends FieldOptions = FieldOptions> {
224
+ options: O;
225
+ constructor(options: O);
226
+ abstract cast(rawValue: any): T | null;
227
+ abstract validate(value: T, form?: Form): string[];
228
+ abstract getTypeName(): string;
229
+ }
230
+
231
+ type FieldTypeConstructor<T, O extends FieldOptions = FieldOptions> = new (options: O) => FieldType<T, O>;
232
+ declare class Field<T = any, O extends FieldOptions = FieldOptions> extends ObservableNode<FieldSnapshot> {
233
+ protected _rawValue: any;
234
+ protected _valueClear: T | null;
235
+ protected fieldType: FieldType<T, O>;
236
+ protected defaultValue: T | null;
237
+ private _baseline;
238
+ protected _errors: string[];
239
+ private _isValid;
240
+ get isValid(): boolean | null;
241
+ set isValid(value: boolean | null);
242
+ private _isTouched;
243
+ get isTouched(): boolean;
244
+ set isTouched(value: boolean);
245
+ constructor(TypeConstructor: FieldTypeConstructor<T, O>, defaultValue?: T | null, options?: O | null);
246
+ protected captureState(): FieldSnapshot;
247
+ protected get plugins(): FieldPlugin<any>[];
248
+ get options(): O;
249
+ set options(options: O);
250
+ get rawValue(): T | null;
251
+ set rawValue(input: any);
252
+ setRawValue(input: any, validate?: boolean): void;
253
+ private assignRawValue;
254
+ get errors(): string[];
255
+ get error(): string;
256
+ set error(message: string | null);
257
+ get valueClear(): T | null;
258
+ get isDirty(): boolean;
259
+ commitBaseline(): void;
260
+ private isRequired;
261
+ checkValid(form?: Form): boolean;
262
+ checkAndSetValid(form?: Form): boolean;
263
+ get isEmpty(): boolean;
264
+ reset(): void;
265
+ }
266
+
267
+ type ErrorFieldMapping = string | ((errorValue: any) => string);
268
+ interface ParsedError {
269
+ fieldName: string;
270
+ message: string;
271
+ sourceKey: string;
272
+ matchedBy: 'static' | 'dynamic' | 'fallback';
273
+ }
274
+ interface DynamicRule {
275
+ pattern: RegExp;
276
+ mapping: ErrorFieldMapping | ((match: RegExpMatchArray, value?: any) => string);
277
+ priority: number;
278
+ }
279
+ declare class ErrorsSchema {
280
+ private _staticRules;
281
+ private _dynamicRules;
282
+ private _fallbackHandler?;
283
+ private _valueTransformers;
284
+ /**
285
+ * Add a static rule that matches an exact error key.
286
+ * @param errorKey - Exact error key returned by the server.
287
+ * @param fieldName - Form field name or a function that resolves it dynamically.
288
+ */
289
+ staticKey(errorKey: string, fieldName: string | ((errorValue: any) => string)): this;
290
+ /**
291
+ * Add a dynamic rule using a regular expression.
292
+ * @param pattern - Regular expression used to match an error key.
293
+ * @param mapping - Field name or function that derives it from the match.
294
+ * @param options - Additional options, such as rule priority.
295
+ */
296
+ dynamicKey(pattern: RegExp, mapping: string | ((match: RegExpMatchArray, value?: any) => string), options?: {
297
+ priority?: number;
298
+ }): this;
299
+ /**
300
+ * Set a handler for errors that do not match any rule.
301
+ */
302
+ fallback(handler: (key: string, value: any) => string | null): this;
303
+ /**
304
+ * Add a transformer for error values.
305
+ * Useful for normalizing messages, such as server responses containing arrays.
306
+ */
307
+ transformValue(transformer: (value: any) => any): this;
308
+ /**
309
+ * Create a schema from a simple mapping object.
310
+ */
311
+ static fromObject(mapping: Record<string, string>): ErrorsSchema;
312
+ /**
313
+ * Create a schema for an array of similarly structured forms.
314
+ */
315
+ static forArray(itemSchema: ErrorsSchema, arrayFieldName: string, indexExtractor?: (key: string) => number): ErrorsSchema;
316
+ /**
317
+ * Parse errors while applying configured value transformers.
318
+ * Supports nested structures using dot notation.
319
+ */
320
+ parseErrors(errors: Record<string, any>, prefix?: string): Record<string, string>;
321
+ /**
322
+ * Parse server errors and return detailed metadata.
323
+ */
324
+ parseErrorsDetailed(errors: Record<string, any>, prefix?: string): Record<string, ParsedError>;
325
+ /**
326
+ * Create a schema for a nested form.
327
+ */
328
+ nest(prefix: string): ErrorsSchema;
329
+ /**
330
+ * Clone the current schema.
331
+ */
332
+ clone(): ErrorsSchema;
333
+ /**
334
+ * Merge the current schema with another schema.
335
+ */
336
+ merge(other: ErrorsSchema): ErrorsSchema;
337
+ private isNestedObject;
338
+ private findMappingDetailed;
339
+ private findMapping;
340
+ }
341
+ /**
342
+ * Default base schema.
343
+ */
344
+ declare const DefaultErrorsSchema: ErrorsSchema;
345
+
346
+ type FormFieldValue<TField> = TField extends Field<infer TValue, infer _Options> ? TValue | null : TField extends ArrayField<infer TValue> ? (TValue | null)[] : TField extends ArrayFormField<infer TForm> ? TForm extends Form ? FormFieldsValue<TForm>[] : unknown[] : never;
347
+ type FormFieldsValue<TForm extends Form> = {
348
+ [K in keyof TForm as TForm[K] extends Field<infer _Value, infer _Options> | ArrayField<infer _ArrayValue> | ArrayFormField<infer _NestedForm> ? K : never]: FormFieldValue<TForm[K]>;
349
+ };
350
+ declare abstract class Form extends ObservableNode<FormSnapshot> {
351
+ protected captureState(): FormSnapshot;
352
+ protected stateChildren(): readonly ObservableNode<unknown>[];
353
+ batch<TResult>(callback: () => TResult & (TResult extends PromiseLike<unknown> ? never : unknown)): TResult;
354
+ private _globalErrors;
355
+ errorsShema: ErrorsSchema;
356
+ protected get _fields(): Record<string, Field<any> | ArrayField<any> | ArrayFormField<any>>;
357
+ checkValid(form?: this): boolean;
358
+ get fieldsValue(): FormFieldsValue<this>;
359
+ set fields(data: Record<string, unknown>);
360
+ setFields(data: Record<string, unknown>, validate?: boolean): void;
361
+ get allErrors(): Record<string, string[]>;
362
+ /**
363
+ * Recursively collect all errors from an ArrayFormField.
364
+ */
365
+ private collectArrayFormErrors;
366
+ get globalErrors(): Record<string, string>;
367
+ set errors(errors: Record<string, unknown>);
368
+ private clearErrorsRecursively;
369
+ /**
370
+ * Apply an error to the corresponding field.
371
+ * Supports nested fields using dot notation.
372
+ */
373
+ private applyErrorToField;
374
+ private applyNestedError;
375
+ setGlobalError(key: string, message: string): void;
376
+ clearGlobalErrors(): void;
377
+ reset(): void;
378
+ get isDirty(): boolean;
379
+ commitBaseline(): void;
380
+ get isTouched(): boolean;
381
+ get isValid(): boolean;
382
+ toFormData(): FormData;
383
+ toJSON(): Record<string, unknown>;
384
+ private serializeJsonValue;
385
+ }
386
+
387
+ export { ArrayField as A, BasePlugin as B, type messagesOptions as C, type DecimalOptions as D, ErrorsSchema as E, Field as F, type messagesStringOptions as G, type MaskPluginOptions as M, type NumberOptions as N, type PluginOptions as P, type StringOptions as S, FieldType as a, type BooleanOptions as b, type DateOptions as c, type ArrayFieldOptions as d, type ArrayFieldSnapshot as e, ArrayFormField as f, type ArrayFormFieldOptions as g, type ArrayFormSnapshot as h, DefaultErrorsSchema as i, type DynamicRule as j, type FieldOptions as k, type FieldPlugin as l, type FieldSnapshot as m, Form as n, type FormFieldValue as o, type FormFieldsValue as p, type FormSnapshot as q, type NodeSnapshot as r, type ParsedError as s, type SnapshotValue as t, type messagesArrayFieldOptions as u, type messagesArrayFormFieldOptions as v, type messagesBooleanOptions as w, type messagesDateOptions as x, type messagesDecimalOptions as y, type messagesNumberOptions as z };