adaptform 2.0.4 → 2.1.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.
@@ -14,10 +14,11 @@ declare class ArrayField<T = any> {
14
14
  protected options: ArrayFieldOptions;
15
15
  protected _items: Field<T>[];
16
16
  protected _errors: string[];
17
+ private _hasValidationResult;
17
18
  isTouched: boolean;
18
- constructor(factory: (value?: T) => Field<T>, options?: ArrayFieldOptions, initial?: T[]);
19
- set itemsValue(items: T[]);
20
- get itemsValue(): T[];
19
+ constructor(factory: (value?: T) => Field<T>, options?: ArrayFieldOptions, initial?: (T | null)[]);
20
+ set itemsValue(items: (T | null)[]);
21
+ get itemsValue(): (T | null)[];
21
22
  get items(): Field<T>[];
22
23
  add(value?: T, force_check?: boolean): Field<T>;
23
24
  remove(index: number): void;
@@ -37,12 +38,14 @@ declare class ArrayFormField<T extends Form = Form> {
37
38
  private options;
38
39
  private _forms;
39
40
  private _errors;
41
+ private _hasValidationResult;
40
42
  isTouched: boolean;
41
- constructor(FormConstructor: new () => T, options?: ArrayFormFieldOptions, initial?: any[]);
42
- set forms(forms: any[]);
43
- get forms(): Form[];
44
- get formsFieldsValue(): any[];
45
- add(data?: any, force_check?: boolean): T;
43
+ constructor(FormConstructor: new () => T, options?: ArrayFormFieldOptions, initial?: unknown[]);
44
+ set forms(forms: unknown[]);
45
+ setForms(forms: unknown[], validate?: boolean): void;
46
+ get forms(): T[];
47
+ get formsFieldsValue(): FormFieldsValue<T>[];
48
+ add(data?: unknown, force_check?: boolean): T;
46
49
  remove(index: number): void;
47
50
  clear(): void;
48
51
  reset(): void;
@@ -170,7 +173,7 @@ type FieldTypeConstructor<T, O extends FieldOptions = FieldOptions> = new (optio
170
173
  declare class Field<T = any, O extends FieldOptions = FieldOptions> {
171
174
  protected _rawValue: any;
172
175
  protected _valueClear: T | null;
173
- protected filed_type: FieldType<T, O>;
176
+ protected fieldType: FieldType<T, O>;
174
177
  protected defaultValue: T | null;
175
178
  protected _errors: string[];
176
179
  isValid: boolean | null;
@@ -181,10 +184,12 @@ declare class Field<T = any, O extends FieldOptions = FieldOptions> {
181
184
  set options(options: O);
182
185
  get rawValue(): T | null;
183
186
  set rawValue(input: any);
187
+ setRawValue(input: any, validate?: boolean): void;
188
+ private assignRawValue;
184
189
  get errors(): string[];
185
190
  get error(): string;
186
191
  set error(message: string | null);
187
- get valueClear(): any;
192
+ get valueClear(): T | null;
188
193
  private isRequired;
189
194
  checkValid(form?: Form): boolean;
190
195
  checkAndSetValid(form?: Form): boolean;
@@ -192,22 +197,119 @@ declare class Field<T = any, O extends FieldOptions = FieldOptions> {
192
197
  reset(): void;
193
198
  }
194
199
 
200
+ type ErrorFieldMapping = string | ((errorValue: any) => string);
201
+ interface ParsedError {
202
+ fieldName: string;
203
+ message: string;
204
+ sourceKey: string;
205
+ matchedBy: 'static' | 'dynamic' | 'fallback';
206
+ }
207
+ interface DynamicRule {
208
+ pattern: RegExp;
209
+ mapping: ErrorFieldMapping | ((match: RegExpMatchArray, value?: any) => string);
210
+ priority: number;
211
+ }
212
+ declare class ErrorsSchema {
213
+ private _staticRules;
214
+ private _dynamicRules;
215
+ private _fallbackHandler?;
216
+ private _valueTransformers;
217
+ /**
218
+ * Add a static rule that matches an exact error key.
219
+ * @param errorKey - Exact error key returned by the server.
220
+ * @param fieldName - Form field name or a function that resolves it dynamically.
221
+ */
222
+ staticKey(errorKey: string, fieldName: string | ((errorValue: any) => string)): this;
223
+ /**
224
+ * Add a dynamic rule using a regular expression.
225
+ * @param pattern - Regular expression used to match an error key.
226
+ * @param mapping - Field name or function that derives it from the match.
227
+ * @param options - Additional options, such as rule priority.
228
+ */
229
+ dynamicKey(pattern: RegExp, mapping: string | ((match: RegExpMatchArray, value?: any) => string), options?: {
230
+ priority?: number;
231
+ }): this;
232
+ /**
233
+ * Set a handler for errors that do not match any rule.
234
+ */
235
+ fallback(handler: (key: string, value: any) => string | null): this;
236
+ /**
237
+ * Add a transformer for error values.
238
+ * Useful for normalizing messages, such as server responses containing arrays.
239
+ */
240
+ transformValue(transformer: (value: any) => any): this;
241
+ /**
242
+ * Create a schema from a simple mapping object.
243
+ */
244
+ static fromObject(mapping: Record<string, string>): ErrorsSchema;
245
+ /**
246
+ * Create a schema for an array of similarly structured forms.
247
+ */
248
+ static forArray(itemSchema: ErrorsSchema, arrayFieldName: string, indexExtractor?: (key: string) => number): ErrorsSchema;
249
+ /**
250
+ * Parse errors while applying configured value transformers.
251
+ * Supports nested structures using dot notation.
252
+ */
253
+ parseErrors(errors: Record<string, any>, prefix?: string): Record<string, string>;
254
+ /**
255
+ * Parse server errors and return detailed metadata.
256
+ */
257
+ parseErrorsDetailed(errors: Record<string, any>, prefix?: string): Record<string, ParsedError>;
258
+ /**
259
+ * Create a schema for a nested form.
260
+ */
261
+ nest(prefix: string): ErrorsSchema;
262
+ /**
263
+ * Clone the current schema.
264
+ */
265
+ clone(): ErrorsSchema;
266
+ /**
267
+ * Merge the current schema with another schema.
268
+ */
269
+ merge(other: ErrorsSchema): ErrorsSchema;
270
+ private isNestedObject;
271
+ private findMappingDetailed;
272
+ private findMapping;
273
+ }
274
+ /**
275
+ * Default base schema.
276
+ */
277
+ declare const DefaultErrorsSchema: ErrorsSchema;
278
+
279
+ 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;
280
+ type FormFieldsValue<TForm extends Form> = {
281
+ [K in keyof TForm as TForm[K] extends Field<infer _Value, infer _Options> | ArrayField<infer _ArrayValue> | ArrayFormField<infer _NestedForm> ? K : never]: FormFieldValue<TForm[K]>;
282
+ };
195
283
  declare abstract class Form {
196
284
  private _globalErrors;
285
+ errorsShema: ErrorsSchema;
197
286
  protected get _fields(): Record<string, Field<any> | ArrayField<any> | ArrayFormField<any>>;
198
287
  checkValid(form?: this): boolean;
199
- get fieldsValue(): Record<string, any>;
200
- set fields(data: Record<string, any>);
288
+ get fieldsValue(): FormFieldsValue<this>;
289
+ set fields(data: Record<string, unknown>);
290
+ setFields(data: Record<string, unknown>, validate?: boolean): void;
201
291
  get allErrors(): Record<string, string[]>;
292
+ /**
293
+ * Recursively collect all errors from an ArrayFormField.
294
+ */
295
+ private collectArrayFormErrors;
202
296
  get globalErrors(): Record<string, string>;
203
- set errors(errors: Record<string, string>);
297
+ set errors(errors: Record<string, unknown>);
298
+ private clearErrorsRecursively;
299
+ /**
300
+ * Apply an error to the corresponding field.
301
+ * Supports nested fields using dot notation.
302
+ */
303
+ private applyErrorToField;
304
+ private applyNestedError;
204
305
  setGlobalError(key: string, message: string): void;
205
306
  clearGlobalErrors(): void;
206
307
  reset(): void;
207
308
  get isTouched(): boolean;
208
309
  get isValid(): boolean;
209
310
  toFormData(): FormData;
210
- toJSON(): Record<string, any>;
311
+ toJSON(): Record<string, unknown>;
312
+ private serializeJsonValue;
211
313
  }
212
314
 
213
- export { type ArrayFieldOptions as A, BasePlugin as B, type DecimalOptions as D, Field as F, 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, Form as d, type messagesStringOptions as e, type messagesNumberOptions as f, type messagesBooleanOptions as g, type messagesDateOptions as h, type messagesDecimalOptions as i, type messagesArrayFieldOptions as j, type messagesArrayFormFieldOptions as k, type FieldOptions as l, type messagesOptions as m, type ArrayFormFieldOptions as n, ArrayField as o, ArrayFormField as p, type FieldPlugin as q };
315
+ export { ArrayField as A, BasePlugin as B, type DecimalOptions as D, ErrorsSchema as E, Field as F, 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, ArrayFormField as e, type ArrayFormFieldOptions as f, DefaultErrorsSchema as g, type DynamicRule as h, type FieldOptions as i, type FieldPlugin as j, Form as k, type FormFieldValue as l, type FormFieldsValue as m, type ParsedError as n, type messagesArrayFieldOptions as o, type messagesArrayFormFieldOptions as p, type messagesBooleanOptions as q, type messagesDateOptions as r, type messagesDecimalOptions as s, type messagesNumberOptions as t, type messagesOptions as u, type messagesStringOptions as v };