@tanstack/form-core 0.42.0 → 0.43.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.
- package/dist/cjs/FieldApi.cjs +17 -28
- package/dist/cjs/FieldApi.cjs.map +1 -1
- package/dist/cjs/FieldApi.d.cts +91 -79
- package/dist/cjs/FormApi.cjs +50 -53
- package/dist/cjs/FormApi.cjs.map +1 -1
- package/dist/cjs/FormApi.d.cts +74 -67
- package/dist/cjs/formOptions.cjs.map +1 -1
- package/dist/cjs/formOptions.d.cts +2 -3
- package/dist/cjs/index.cjs +2 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/mergeForm.cjs +33 -9
- package/dist/cjs/mergeForm.cjs.map +1 -1
- package/dist/cjs/mergeForm.d.cts +2 -3
- package/dist/cjs/metaHelper.cjs.map +1 -1
- package/dist/cjs/metaHelper.d.cts +2 -3
- package/dist/cjs/standardSchemaValidator.cjs +24 -31
- package/dist/cjs/standardSchemaValidator.cjs.map +1 -1
- package/dist/cjs/standardSchemaValidator.d.cts +24 -4
- package/dist/cjs/types.d.cts +15 -27
- package/dist/cjs/util-types.d.cts +1 -0
- package/dist/cjs/utils.cjs +4 -0
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +4 -3
- package/dist/esm/FieldApi.d.ts +91 -79
- package/dist/esm/FieldApi.js +18 -29
- package/dist/esm/FieldApi.js.map +1 -1
- package/dist/esm/FormApi.d.ts +74 -67
- package/dist/esm/FormApi.js +52 -55
- package/dist/esm/FormApi.js.map +1 -1
- package/dist/esm/formOptions.d.ts +2 -3
- package/dist/esm/formOptions.js.map +1 -1
- package/dist/esm/index.js +4 -3
- package/dist/esm/mergeForm.d.ts +2 -3
- package/dist/esm/mergeForm.js +33 -9
- package/dist/esm/mergeForm.js.map +1 -1
- package/dist/esm/metaHelper.d.ts +2 -3
- package/dist/esm/metaHelper.js.map +1 -1
- package/dist/esm/standardSchemaValidator.d.ts +24 -4
- package/dist/esm/standardSchemaValidator.js +24 -31
- package/dist/esm/standardSchemaValidator.js.map +1 -1
- package/dist/esm/types.d.ts +15 -27
- package/dist/esm/util-types.d.ts +1 -0
- package/dist/esm/utils.d.ts +4 -3
- package/dist/esm/utils.js +4 -0
- package/dist/esm/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/FieldApi.ts +803 -273
- package/src/FormApi.ts +613 -183
- package/src/formOptions.ts +26 -4
- package/src/mergeForm.ts +63 -26
- package/src/metaHelper.ts +28 -6
- package/src/standardSchemaValidator.ts +47 -58
- package/src/types.ts +39 -34
- package/src/util-types.ts +2 -0
- package/src/utils.ts +15 -9
package/dist/esm/FieldApi.d.ts
CHANGED
|
@@ -1,58 +1,66 @@
|
|
|
1
1
|
import { Derived } from '@tanstack/store';
|
|
2
|
-
import {
|
|
3
|
-
import { StandardSchemaV1 } from './standardSchemaValidator.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { DeepKeys, DeepValue, NoInfer, UnwrapOneLevelOfArray } from './util-types.js';
|
|
3
|
+
import { StandardSchemaV1, StandardSchemaV1Issue, TStandardSchemaValidatorValue } from './standardSchemaValidator.js';
|
|
4
|
+
import { FieldInfo, FormApi, FormAsyncValidateOrFn, FormValidateOrFn, UnwrapFormAsyncValidateOrFn, UnwrapFormValidateOrFn } from './FormApi.js';
|
|
5
|
+
import { UpdateMetaOptions, ValidationCause, ValidationError, ValidationErrorMap } from './types.js';
|
|
6
6
|
import { Updater } from './utils.js';
|
|
7
7
|
/**
|
|
8
8
|
* @private
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
type FieldErrorMapFromValidator<TFormData, TName extends DeepKeys<TFormData>, TData extends DeepValue<TFormData, TName>, TOnMount extends undefined | FieldValidateOrFn<TFormData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TFormData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TFormData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TFormData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TFormData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TFormData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TFormData, TName, TData>> = Partial<Record<DeepKeys<TFormData>, ValidationErrorMap<TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync>>>;
|
|
11
|
+
/**
|
|
12
|
+
* @private
|
|
13
|
+
*/
|
|
14
|
+
export type FieldValidateFn<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = (props: {
|
|
11
15
|
value: TData;
|
|
12
|
-
fieldApi: FieldApi<TParentData, TName,
|
|
13
|
-
}) =>
|
|
16
|
+
fieldApi: FieldApi<TParentData, TName, TData, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
|
17
|
+
}) => unknown;
|
|
14
18
|
/**
|
|
15
19
|
* @private
|
|
16
20
|
*/
|
|
17
|
-
export type FieldValidateOrFn<TParentData, TName extends DeepKeys<TParentData>,
|
|
21
|
+
export type FieldValidateOrFn<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = FieldValidateFn<TParentData, TName, TData> | StandardSchemaV1<TData, unknown>;
|
|
22
|
+
export type UnwrapFieldValidateOrFn<TParentData, TName extends DeepKeys<TParentData>, TValidateOrFn extends undefined | FieldValidateOrFn<any, any, any>, TFormValidateOrFn extends undefined | FormValidateOrFn<any>> = ([TFormValidateOrFn] extends [StandardSchemaV1<any, infer TStandardOut>] ? TName extends keyof TStandardOut ? StandardSchemaV1Issue[] : undefined : undefined) | (UnwrapFormValidateOrFn<TFormValidateOrFn> extends infer TFormValidateVal ? TFormValidateVal extends {
|
|
23
|
+
fields: any;
|
|
24
|
+
} ? TName extends keyof TFormValidateVal['fields'] ? TFormValidateVal['fields'][TName] : undefined : undefined : never) | ([TValidateOrFn] extends [FieldValidateFn<any, any, any>] ? ReturnType<TValidateOrFn> : [TValidateOrFn] extends [StandardSchemaV1<any, any>] ? StandardSchemaV1Issue[] : undefined);
|
|
18
25
|
/**
|
|
19
26
|
* @private
|
|
20
27
|
*/
|
|
21
|
-
export type FieldValidateAsyncFn<TParentData, TName extends DeepKeys<TParentData>,
|
|
28
|
+
export type FieldValidateAsyncFn<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = (options: {
|
|
22
29
|
value: TData;
|
|
23
|
-
fieldApi: FieldApi<TParentData, TName,
|
|
30
|
+
fieldApi: FieldApi<TParentData, TName, TData, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
|
24
31
|
signal: AbortSignal;
|
|
25
|
-
}) =>
|
|
32
|
+
}) => unknown | Promise<unknown>;
|
|
26
33
|
/**
|
|
27
34
|
* @private
|
|
28
35
|
*/
|
|
29
|
-
export type FieldAsyncValidateOrFn<TParentData, TName extends DeepKeys<TParentData>,
|
|
36
|
+
export type FieldAsyncValidateOrFn<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = FieldValidateAsyncFn<TParentData, TName, TData> | StandardSchemaV1<TData, unknown>;
|
|
37
|
+
export type UnwrapFieldAsyncValidateOrFn<TParentData, TName extends DeepKeys<TParentData>, TValidateOrFn extends undefined | FieldAsyncValidateOrFn<any, any, any>, TFormValidateOrFn extends undefined | FormAsyncValidateOrFn<any>> = ([TFormValidateOrFn] extends [StandardSchemaV1<any, infer TStandardOut>] ? TName extends keyof TStandardOut ? StandardSchemaV1Issue[] : undefined : undefined) | (UnwrapFormAsyncValidateOrFn<TFormValidateOrFn> extends infer TFormValidateVal ? TFormValidateVal extends {
|
|
38
|
+
fields: any;
|
|
39
|
+
} ? TName extends keyof TFormValidateVal['fields'] ? TFormValidateVal['fields'][TName] : undefined : undefined : never) | ([TValidateOrFn] extends [FieldValidateAsyncFn<any, any, any>] ? Awaited<ReturnType<TValidateOrFn>> : [TValidateOrFn] extends [StandardSchemaV1<any, any>] ? StandardSchemaV1Issue[] : undefined);
|
|
30
40
|
/**
|
|
31
41
|
* @private
|
|
32
42
|
*/
|
|
33
|
-
export type FieldListenerFn<TParentData, TName extends DeepKeys<TParentData>,
|
|
43
|
+
export type FieldListenerFn<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> = (props: {
|
|
34
44
|
value: TData;
|
|
35
|
-
fieldApi: FieldApi<TParentData, TName,
|
|
45
|
+
fieldApi: FieldApi<TParentData, TName, TData, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
|
36
46
|
}) => void;
|
|
37
|
-
export interface FieldValidators<TParentData, TName extends DeepKeys<TParentData>,
|
|
47
|
+
export interface FieldValidators<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>> {
|
|
38
48
|
/**
|
|
39
|
-
* An optional function that
|
|
49
|
+
* An optional function, that runs on the mount event of input.
|
|
40
50
|
*/
|
|
41
|
-
onMount?:
|
|
51
|
+
onMount?: TOnMount;
|
|
42
52
|
/**
|
|
43
|
-
* An optional
|
|
44
|
-
* If `validatorAdapter` is passed, this may also accept a property from the respective adapter
|
|
53
|
+
* An optional function, that runs on the change event of input.
|
|
45
54
|
*
|
|
46
|
-
* @example z.string().min(1)
|
|
55
|
+
* @example z.string().min(1)
|
|
47
56
|
*/
|
|
48
|
-
onChange?:
|
|
57
|
+
onChange?: TOnChange;
|
|
49
58
|
/**
|
|
50
|
-
* An optional property similar to `onChange` but async validation
|
|
51
|
-
* is passed, this may also accept a property from the respective adapter
|
|
59
|
+
* An optional property similar to `onChange` but async validation
|
|
52
60
|
*
|
|
53
|
-
* @example z.string().refine(async (val) => val.length > 3, { message: 'Testing 123' })
|
|
61
|
+
* @example z.string().refine(async (val) => val.length > 3, { message: 'Testing 123' })
|
|
54
62
|
*/
|
|
55
|
-
onChangeAsync?:
|
|
63
|
+
onChangeAsync?: TOnChangeAsync;
|
|
56
64
|
/**
|
|
57
65
|
* An optional number to represent how long the `onChangeAsync` should wait before running
|
|
58
66
|
*
|
|
@@ -65,18 +73,16 @@ export interface FieldValidators<TParentData, TName extends DeepKeys<TParentData
|
|
|
65
73
|
onChangeListenTo?: DeepKeys<TParentData>[];
|
|
66
74
|
/**
|
|
67
75
|
* An optional function, that runs on the blur event of input.
|
|
68
|
-
* If `validatorAdapter` is passed, this may also accept a property from the respective adapter
|
|
69
76
|
*
|
|
70
|
-
* @example z.string().min(1)
|
|
77
|
+
* @example z.string().min(1)
|
|
71
78
|
*/
|
|
72
|
-
onBlur?:
|
|
79
|
+
onBlur?: TOnBlur;
|
|
73
80
|
/**
|
|
74
|
-
* An optional property similar to `onBlur` but async validation.
|
|
75
|
-
* is passed, this may also accept a property from the respective adapter
|
|
81
|
+
* An optional property similar to `onBlur` but async validation.
|
|
76
82
|
*
|
|
77
|
-
* @example z.string().refine(async (val) => val.length > 3, { message: 'Testing 123' })
|
|
83
|
+
* @example z.string().refine(async (val) => val.length > 3, { message: 'Testing 123' })
|
|
78
84
|
*/
|
|
79
|
-
onBlurAsync?:
|
|
85
|
+
onBlurAsync?: TOnBlurAsync;
|
|
80
86
|
/**
|
|
81
87
|
* An optional number to represent how long the `onBlurAsync` should wait before running
|
|
82
88
|
*
|
|
@@ -89,29 +95,27 @@ export interface FieldValidators<TParentData, TName extends DeepKeys<TParentData
|
|
|
89
95
|
onBlurListenTo?: DeepKeys<TParentData>[];
|
|
90
96
|
/**
|
|
91
97
|
* An optional function, that runs on the submit event of form.
|
|
92
|
-
* If `validatorAdapter` is passed, this may also accept a property from the respective adapter
|
|
93
98
|
*
|
|
94
|
-
* @example z.string().min(1)
|
|
99
|
+
* @example z.string().min(1)
|
|
95
100
|
*/
|
|
96
|
-
onSubmit?:
|
|
101
|
+
onSubmit?: TOnSubmit;
|
|
97
102
|
/**
|
|
98
|
-
* An optional property similar to `onSubmit` but async validation.
|
|
99
|
-
* is passed, this may also accept a property from the respective adapter
|
|
103
|
+
* An optional property similar to `onSubmit` but async validation.
|
|
100
104
|
*
|
|
101
|
-
* @example z.string().refine(async (val) => val.length > 3, { message: 'Testing 123' })
|
|
105
|
+
* @example z.string().refine(async (val) => val.length > 3, { message: 'Testing 123' })
|
|
102
106
|
*/
|
|
103
|
-
onSubmitAsync?:
|
|
107
|
+
onSubmitAsync?: TOnSubmitAsync;
|
|
104
108
|
}
|
|
105
|
-
export interface FieldListeners<TParentData, TName extends DeepKeys<TParentData>,
|
|
106
|
-
onChange?: FieldListenerFn<TParentData, TName,
|
|
107
|
-
onBlur?: FieldListenerFn<TParentData, TName,
|
|
108
|
-
onMount?: FieldListenerFn<TParentData, TName,
|
|
109
|
-
onSubmit?: FieldListenerFn<TParentData, TName,
|
|
109
|
+
export interface FieldListeners<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>> {
|
|
110
|
+
onChange?: FieldListenerFn<TParentData, TName, TData>;
|
|
111
|
+
onBlur?: FieldListenerFn<TParentData, TName, TData>;
|
|
112
|
+
onMount?: FieldListenerFn<TParentData, TName, TData>;
|
|
113
|
+
onSubmit?: FieldListenerFn<TParentData, TName, TData>;
|
|
110
114
|
}
|
|
111
115
|
/**
|
|
112
116
|
* An object type representing the options for a field in a form.
|
|
113
117
|
*/
|
|
114
|
-
export interface FieldOptions<TParentData, TName extends DeepKeys<TParentData>,
|
|
118
|
+
export interface FieldOptions<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>> {
|
|
115
119
|
/**
|
|
116
120
|
* The field name. The type will be `DeepKeys<TParentData>` to ensure your name is a deep key of the parent dataset.
|
|
117
121
|
*/
|
|
@@ -128,30 +132,30 @@ export interface FieldOptions<TParentData, TName extends DeepKeys<TParentData>,
|
|
|
128
132
|
* If `true`, always run async validation, even if there are errors emitted during synchronous validation.
|
|
129
133
|
*/
|
|
130
134
|
asyncAlways?: boolean;
|
|
131
|
-
/**
|
|
132
|
-
* A validator provided by an extension, like `yupValidator` from `@tanstack/yup-form-adapter`
|
|
133
|
-
*/
|
|
134
|
-
validatorAdapter?: TFieldValidator;
|
|
135
135
|
/**
|
|
136
136
|
* A list of validators to pass to the field
|
|
137
137
|
*/
|
|
138
|
-
validators?: FieldValidators<TParentData, TName,
|
|
138
|
+
validators?: FieldValidators<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync>;
|
|
139
139
|
/**
|
|
140
140
|
* An optional object with default metadata for the field.
|
|
141
141
|
*/
|
|
142
|
-
defaultMeta?: Partial<FieldMeta
|
|
142
|
+
defaultMeta?: Partial<FieldMeta<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, any, any, any, any, any, any, any>>;
|
|
143
143
|
/**
|
|
144
144
|
* A list of listeners which attach to the corresponding events
|
|
145
145
|
*/
|
|
146
|
-
listeners?: FieldListeners<TParentData, TName,
|
|
146
|
+
listeners?: FieldListeners<TParentData, TName, TData>;
|
|
147
|
+
/**
|
|
148
|
+
* Disable the `flat(1)` operation on `field.errors`. This is useful if you want to keep the error structure as is. Not suggested for most use-cases.
|
|
149
|
+
*/
|
|
150
|
+
disableErrorFlat?: boolean;
|
|
147
151
|
}
|
|
148
152
|
/**
|
|
149
153
|
* An object type representing the required options for the FieldApi class.
|
|
150
154
|
*/
|
|
151
|
-
export interface FieldApiOptions<TParentData, TName extends DeepKeys<TParentData>,
|
|
152
|
-
form: FormApi<TParentData,
|
|
155
|
+
export interface FieldApiOptions<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>> extends FieldOptions<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync> {
|
|
156
|
+
form: FormApi<TParentData, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer>;
|
|
153
157
|
}
|
|
154
|
-
export type FieldMetaBase = {
|
|
158
|
+
export type FieldMetaBase<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>> = {
|
|
155
159
|
/**
|
|
156
160
|
* A flag indicating whether the field has been touched.
|
|
157
161
|
*/
|
|
@@ -167,30 +171,33 @@ export type FieldMetaBase = {
|
|
|
167
171
|
/**
|
|
168
172
|
* A map of errors related to the field value.
|
|
169
173
|
*/
|
|
170
|
-
errorMap: ValidationErrorMap
|
|
174
|
+
errorMap: ValidationErrorMap<UnwrapFieldValidateOrFn<TParentData, TName, TOnMount, TFormOnMount>, UnwrapFieldValidateOrFn<TParentData, TName, TOnChange, TFormOnChange>, UnwrapFieldAsyncValidateOrFn<TParentData, TName, TOnChangeAsync, TFormOnChangeAsync>, UnwrapFieldValidateOrFn<TParentData, TName, TOnBlur, TFormOnBlur>, UnwrapFieldAsyncValidateOrFn<TParentData, TName, TOnBlurAsync, TFormOnBlurAsync>, UnwrapFieldValidateOrFn<TParentData, TName, TOnSubmit, TFormOnSubmit>, UnwrapFieldAsyncValidateOrFn<TParentData, TName, TOnSubmitAsync, TFormOnSubmitAsync>>;
|
|
171
175
|
/**
|
|
172
176
|
* A flag indicating whether the field is currently being validated.
|
|
173
177
|
*/
|
|
174
178
|
isValidating: boolean;
|
|
175
179
|
};
|
|
176
|
-
export type
|
|
180
|
+
export type AnyFieldMetaBase = FieldMetaBase<any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
|
181
|
+
export type FieldMetaDerived<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>> = {
|
|
177
182
|
/**
|
|
178
183
|
* An array of errors related to the field value.
|
|
179
184
|
*/
|
|
180
|
-
errors:
|
|
185
|
+
errors: Array<UnwrapOneLevelOfArray<UnwrapFieldValidateOrFn<TParentData, TName, TOnMount, TFormOnMount>> | UnwrapOneLevelOfArray<UnwrapFieldValidateOrFn<TParentData, TName, TOnChange, TFormOnChange>> | UnwrapOneLevelOfArray<UnwrapFieldAsyncValidateOrFn<TParentData, TName, TOnChangeAsync, TFormOnChangeAsync>> | UnwrapOneLevelOfArray<UnwrapFieldValidateOrFn<TParentData, TName, TOnBlur, TFormOnBlur>> | UnwrapOneLevelOfArray<UnwrapFieldAsyncValidateOrFn<TParentData, TName, TOnBlurAsync, TFormOnBlurAsync>> | UnwrapOneLevelOfArray<UnwrapFieldValidateOrFn<TParentData, TName, TOnSubmit, TFormOnSubmit>> | UnwrapOneLevelOfArray<UnwrapFieldAsyncValidateOrFn<TParentData, TName, TOnSubmitAsync, TFormOnSubmitAsync>>>;
|
|
181
186
|
/**
|
|
182
187
|
* A flag that is `true` if the field's value has not been modified by the user. Opposite of `isDirty`.
|
|
183
188
|
*/
|
|
184
189
|
isPristine: boolean;
|
|
185
190
|
};
|
|
191
|
+
export type AnyFieldMetaDerived = FieldMetaDerived<any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
|
186
192
|
/**
|
|
187
193
|
* An object type representing the metadata of a field in a form.
|
|
188
194
|
*/
|
|
189
|
-
export type FieldMeta = FieldMetaBase & FieldMetaDerived
|
|
195
|
+
export type FieldMeta<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>> = FieldMetaBase<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync> & FieldMetaDerived<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync>;
|
|
196
|
+
export type AnyFieldMeta = FieldMeta<any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
|
190
197
|
/**
|
|
191
198
|
* An object type representing the state of a field.
|
|
192
199
|
*/
|
|
193
|
-
export type FieldState<TData
|
|
200
|
+
export type FieldState<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>> = {
|
|
194
201
|
/**
|
|
195
202
|
* The current value of the field.
|
|
196
203
|
*/
|
|
@@ -198,8 +205,14 @@ export type FieldState<TData> = {
|
|
|
198
205
|
/**
|
|
199
206
|
* The current metadata of the field.
|
|
200
207
|
*/
|
|
201
|
-
meta: FieldMeta
|
|
208
|
+
meta: FieldMeta<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync>;
|
|
202
209
|
};
|
|
210
|
+
/**
|
|
211
|
+
* @public
|
|
212
|
+
*
|
|
213
|
+
* A type representing the Field API with all generics set to `any` for convenience.
|
|
214
|
+
*/
|
|
215
|
+
export type AnyFieldApi = FieldApi<any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any>;
|
|
203
216
|
/**
|
|
204
217
|
* A class representing the API for managing a form field.
|
|
205
218
|
*
|
|
@@ -209,11 +222,11 @@ export type FieldState<TData> = {
|
|
|
209
222
|
* However, if you need to create a new instance manually, you can do so by calling
|
|
210
223
|
* the `new FieldApi` constructor.
|
|
211
224
|
*/
|
|
212
|
-
export declare class FieldApi<TParentData, TName extends DeepKeys<TParentData>,
|
|
225
|
+
export declare class FieldApi<TParentData, TName extends DeepKeys<TParentData>, TData extends DeepValue<TParentData, TName>, TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnChangeAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnBlurAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>, TOnSubmitAsync extends undefined | FieldAsyncValidateOrFn<TParentData, TName, TData>, TFormOnMount extends undefined | FormValidateOrFn<TParentData>, TFormOnChange extends undefined | FormValidateOrFn<TParentData>, TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnBlur extends undefined | FormValidateOrFn<TParentData>, TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>, TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>, TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>> {
|
|
213
226
|
/**
|
|
214
227
|
* A reference to the form API instance.
|
|
215
228
|
*/
|
|
216
|
-
form: FieldApiOptions<TParentData, TName,
|
|
229
|
+
form: FieldApiOptions<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer>['form'];
|
|
217
230
|
/**
|
|
218
231
|
* The field name.
|
|
219
232
|
*/
|
|
@@ -221,32 +234,30 @@ export declare class FieldApi<TParentData, TName extends DeepKeys<TParentData>,
|
|
|
221
234
|
/**
|
|
222
235
|
* The field options.
|
|
223
236
|
*/
|
|
224
|
-
options: FieldApiOptions<TParentData, TName,
|
|
237
|
+
options: FieldApiOptions<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer>;
|
|
225
238
|
/**
|
|
226
239
|
* The field state store.
|
|
227
240
|
*/
|
|
228
|
-
store: Derived<FieldState<TData>>;
|
|
241
|
+
store: Derived<FieldState<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync>>;
|
|
229
242
|
/**
|
|
230
243
|
* The current field state.
|
|
231
244
|
*/
|
|
232
|
-
get state(): FieldState<TData>;
|
|
245
|
+
get state(): FieldState<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync>;
|
|
233
246
|
timeoutIds: Record<ValidationCause, ReturnType<typeof setTimeout> | null>;
|
|
234
247
|
/**
|
|
235
248
|
* Initializes a new `FieldApi` instance.
|
|
236
249
|
*/
|
|
237
|
-
constructor(opts: FieldApiOptions<TParentData, TName,
|
|
250
|
+
constructor(opts: FieldApiOptions<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer>);
|
|
238
251
|
/**
|
|
239
252
|
* @private
|
|
240
253
|
*/
|
|
241
|
-
runValidator<TValue extends {
|
|
242
|
-
|
|
243
|
-
fieldApi: FieldApi<any, any, any, any>;
|
|
244
|
-
validationSource: ValidationSource;
|
|
254
|
+
runValidator<TValue extends TStandardSchemaValidatorValue<TData> & {
|
|
255
|
+
fieldApi: AnyFieldApi;
|
|
245
256
|
}, TType extends 'validate' | 'validateAsync'>(props: {
|
|
246
|
-
validate: TType extends 'validate' ? FieldValidateOrFn<any, any, any
|
|
257
|
+
validate: TType extends 'validate' ? FieldValidateOrFn<any, any, any> : FieldAsyncValidateOrFn<any, any, any>;
|
|
247
258
|
value: TValue;
|
|
248
259
|
type: TType;
|
|
249
|
-
}):
|
|
260
|
+
}): unknown;
|
|
250
261
|
/**
|
|
251
262
|
* Mounts the field instance to the form.
|
|
252
263
|
*/
|
|
@@ -254,7 +265,7 @@ export declare class FieldApi<TParentData, TName extends DeepKeys<TParentData>,
|
|
|
254
265
|
/**
|
|
255
266
|
* Updates the field instance with new options.
|
|
256
267
|
*/
|
|
257
|
-
update: (opts: FieldApiOptions<TParentData, TName,
|
|
268
|
+
update: (opts: FieldApiOptions<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync, TFormOnServer>) => void;
|
|
258
269
|
/**
|
|
259
270
|
* Gets the current field value.
|
|
260
271
|
* @deprecated Use `field.state.value` instead.
|
|
@@ -264,15 +275,15 @@ export declare class FieldApi<TParentData, TName extends DeepKeys<TParentData>,
|
|
|
264
275
|
* Sets the field value and run the `change` validator.
|
|
265
276
|
*/
|
|
266
277
|
setValue: (updater: Updater<TData>, options?: UpdateMetaOptions) => void;
|
|
267
|
-
getMeta: () => FieldMeta
|
|
278
|
+
getMeta: () => FieldMeta<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync>;
|
|
268
279
|
/**
|
|
269
280
|
* Sets the field metadata.
|
|
270
281
|
*/
|
|
271
|
-
setMeta: (updater: Updater<FieldMeta
|
|
282
|
+
setMeta: (updater: Updater<FieldMeta<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TFormOnMount, TFormOnChange, TFormOnChangeAsync, TFormOnBlur, TFormOnBlurAsync, TFormOnSubmit, TFormOnSubmitAsync>>) => void;
|
|
272
283
|
/**
|
|
273
284
|
* Gets the field information object.
|
|
274
285
|
*/
|
|
275
|
-
getInfo: () => FieldInfo<TParentData
|
|
286
|
+
getInfo: () => FieldInfo<TParentData>;
|
|
276
287
|
/**
|
|
277
288
|
* Pushes a new value to the field.
|
|
278
289
|
*/
|
|
@@ -300,7 +311,7 @@ export declare class FieldApi<TParentData, TName extends DeepKeys<TParentData>,
|
|
|
300
311
|
/**
|
|
301
312
|
* @private
|
|
302
313
|
*/
|
|
303
|
-
getLinkedFields: (cause: ValidationCause) =>
|
|
314
|
+
getLinkedFields: (cause: ValidationCause) => AnyFieldApi[];
|
|
304
315
|
/**
|
|
305
316
|
* @private
|
|
306
317
|
*/
|
|
@@ -310,7 +321,7 @@ export declare class FieldApi<TParentData, TName extends DeepKeys<TParentData>,
|
|
|
310
321
|
/**
|
|
311
322
|
* @private
|
|
312
323
|
*/
|
|
313
|
-
validateAsync: (cause: ValidationCause, formValidationResultPromise: Promise<
|
|
324
|
+
validateAsync: (cause: ValidationCause, formValidationResultPromise: Promise<FieldErrorMapFromValidator<TParentData, TName, TData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync>>) => Promise<unknown[]>;
|
|
314
325
|
/**
|
|
315
326
|
* Validates the field value.
|
|
316
327
|
*/
|
|
@@ -330,3 +341,4 @@ export declare class FieldApi<TParentData, TName extends DeepKeys<TParentData>,
|
|
|
330
341
|
*/
|
|
331
342
|
setErrorMap(errorMap: ValidationErrorMap): void;
|
|
332
343
|
}
|
|
344
|
+
export {};
|
package/dist/esm/FieldApi.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { batch, Derived } from "@tanstack/store";
|
|
2
|
-
import { isStandardSchemaValidator,
|
|
2
|
+
import { isStandardSchemaValidator, standardSchemaValidators } from "./standardSchemaValidator.js";
|
|
3
3
|
import { getBy, getSyncValidatorArray, getAsyncValidatorArray } from "./utils.js";
|
|
4
4
|
class FieldApi {
|
|
5
5
|
/**
|
|
@@ -25,11 +25,13 @@ class FieldApi {
|
|
|
25
25
|
type: "validate"
|
|
26
26
|
});
|
|
27
27
|
if (error) {
|
|
28
|
-
this.setMeta(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
this.setMeta(
|
|
29
|
+
(prev) => ({
|
|
30
|
+
...prev,
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
32
|
+
errorMap: { ...prev == null ? void 0 : prev.errorMap, onMount: error }
|
|
33
|
+
})
|
|
34
|
+
);
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
37
|
(_b = (_a = this.options.listeners) == null ? void 0 : _a.onMount) == null ? void 0 : _b.call(_a, {
|
|
@@ -342,20 +344,8 @@ class FieldApi {
|
|
|
342
344
|
* @private
|
|
343
345
|
*/
|
|
344
346
|
runValidator(props) {
|
|
345
|
-
const adapters = [
|
|
346
|
-
this.form.options.validatorAdapter,
|
|
347
|
-
this.options.validatorAdapter
|
|
348
|
-
];
|
|
349
|
-
for (const adapter of adapters) {
|
|
350
|
-
if (adapter && (typeof props.validate !== "function" || "~standard" in props.validate)) {
|
|
351
|
-
return adapter()[props.type](
|
|
352
|
-
props.value,
|
|
353
|
-
props.validate
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
347
|
if (isStandardSchemaValidator(props.validate)) {
|
|
358
|
-
return
|
|
348
|
+
return standardSchemaValidators[props.type](
|
|
359
349
|
props.value,
|
|
360
350
|
props.validate
|
|
361
351
|
);
|
|
@@ -366,20 +356,19 @@ class FieldApi {
|
|
|
366
356
|
* Updates the field's errorMap
|
|
367
357
|
*/
|
|
368
358
|
setErrorMap(errorMap) {
|
|
369
|
-
this.setMeta(
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
359
|
+
this.setMeta(
|
|
360
|
+
(prev) => ({
|
|
361
|
+
...prev,
|
|
362
|
+
errorMap: {
|
|
363
|
+
...prev.errorMap,
|
|
364
|
+
...errorMap
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
);
|
|
376
368
|
}
|
|
377
369
|
}
|
|
378
370
|
function normalizeError(rawError) {
|
|
379
371
|
if (rawError) {
|
|
380
|
-
if (typeof rawError !== "string") {
|
|
381
|
-
return "Invalid Form Values";
|
|
382
|
-
}
|
|
383
372
|
return rawError;
|
|
384
373
|
}
|
|
385
374
|
return void 0;
|