@tanstack/form-core 0.40.1 → 0.40.3
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 +1 -1
- package/dist/cjs/FieldApi.cjs.map +1 -1
- package/dist/cjs/FieldApi.d.cts +4 -4
- package/dist/cjs/FormApi.cjs +1 -15
- package/dist/cjs/FormApi.cjs.map +1 -1
- package/dist/cjs/FormApi.d.cts +1 -1
- package/dist/cjs/standardSchemaValidator.cjs.map +1 -1
- package/dist/cjs/standardSchemaValidator.d.cts +93 -3
- package/dist/cjs/utils.cjs +4 -1
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +1 -1
- package/dist/esm/FieldApi.d.ts +4 -4
- package/dist/esm/FieldApi.js +1 -1
- package/dist/esm/FieldApi.js.map +1 -1
- package/dist/esm/FormApi.d.ts +1 -1
- package/dist/esm/FormApi.js +1 -15
- package/dist/esm/FormApi.js.map +1 -1
- package/dist/esm/standardSchemaValidator.d.ts +93 -3
- package/dist/esm/standardSchemaValidator.js.map +1 -1
- package/dist/esm/utils.d.ts +1 -1
- package/dist/esm/utils.js +4 -1
- package/dist/esm/utils.js.map +1 -1
- package/package.json +1 -2
- package/src/FieldApi.ts +17 -61
- package/src/FormApi.ts +3 -16
- package/src/standardSchemaValidator.ts +108 -10
- package/src/utils.ts +12 -3
package/src/FormApi.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
isStandardSchemaValidator,
|
|
13
13
|
standardSchemaValidator,
|
|
14
14
|
} from './standardSchemaValidator'
|
|
15
|
+
import type { StandardSchemaV1 } from './standardSchemaValidator'
|
|
15
16
|
import type { FieldApi, FieldMeta } from './FieldApi'
|
|
16
17
|
import type {
|
|
17
18
|
FormValidationError,
|
|
@@ -26,7 +27,6 @@ import type {
|
|
|
26
27
|
} from './types'
|
|
27
28
|
import type { DeepKeys, DeepValue } from './util-types'
|
|
28
29
|
import type { Updater } from './utils'
|
|
29
|
-
import type { StandardSchemaV1 } from '@standard-schema/spec'
|
|
30
30
|
|
|
31
31
|
export type FieldsErrorMapFromValidator<TFormData> = Partial<
|
|
32
32
|
Record<DeepKeys<TFormData>, ValidationErrorMap>
|
|
@@ -532,21 +532,8 @@ export class FormApi<
|
|
|
532
532
|
mount = () => {
|
|
533
533
|
const { onMount } = this.options.validators || {}
|
|
534
534
|
if (!onMount) return
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
value: {
|
|
538
|
-
value: this.state.values,
|
|
539
|
-
formApi: this,
|
|
540
|
-
validationSource: 'form',
|
|
541
|
-
},
|
|
542
|
-
type: 'validate',
|
|
543
|
-
})
|
|
544
|
-
if (error) {
|
|
545
|
-
this.store.setState((prev) => ({
|
|
546
|
-
...prev,
|
|
547
|
-
errorMap: { ...prev.errorMap, onMount: error },
|
|
548
|
-
}))
|
|
549
|
-
}
|
|
535
|
+
|
|
536
|
+
this.validateSync('mount')
|
|
550
537
|
}
|
|
551
538
|
|
|
552
539
|
/**
|
|
@@ -3,16 +3,15 @@ import type {
|
|
|
3
3
|
Validator,
|
|
4
4
|
ValidatorAdapterParams,
|
|
5
5
|
} from './types'
|
|
6
|
-
import type { StandardSchemaV1 } from '@standard-schema/spec'
|
|
7
6
|
|
|
8
|
-
type Params = ValidatorAdapterParams<
|
|
7
|
+
type Params = ValidatorAdapterParams<StandardSchemaV1Issue>
|
|
9
8
|
type TransformFn = NonNullable<Params['transformErrors']>
|
|
10
9
|
|
|
11
10
|
function prefixSchemaToErrors(
|
|
12
|
-
issues: readonly
|
|
11
|
+
issues: readonly StandardSchemaV1Issue[],
|
|
13
12
|
transformErrors: TransformFn,
|
|
14
13
|
) {
|
|
15
|
-
const schema = new Map<string,
|
|
14
|
+
const schema = new Map<string, StandardSchemaV1Issue[]>()
|
|
16
15
|
|
|
17
16
|
for (const issue of issues) {
|
|
18
17
|
const path = [...(issue.path ?? [])]
|
|
@@ -39,18 +38,18 @@ function prefixSchemaToErrors(
|
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
function defaultFormTransformer(transformErrors: TransformFn) {
|
|
42
|
-
return (issues: readonly
|
|
43
|
-
form: transformErrors(issues as
|
|
41
|
+
return (issues: readonly StandardSchemaV1Issue[]) => ({
|
|
42
|
+
form: transformErrors(issues as StandardSchemaV1Issue[]),
|
|
44
43
|
fields: prefixSchemaToErrors(issues, transformErrors),
|
|
45
44
|
})
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
export const standardSchemaValidator =
|
|
49
|
-
(params: Params = {}): Validator<unknown, StandardSchemaV1
|
|
48
|
+
(params: Params = {}): Validator<unknown, StandardSchemaV1<any>> =>
|
|
50
49
|
() => {
|
|
51
50
|
const transformFieldErrors =
|
|
52
51
|
params.transformErrors ??
|
|
53
|
-
((issues:
|
|
52
|
+
((issues: StandardSchemaV1Issue[]) =>
|
|
54
53
|
issues.map((issue) => issue.message).join(', '))
|
|
55
54
|
|
|
56
55
|
const getTransformStrategy = (validationSource: 'form' | 'field') =>
|
|
@@ -70,7 +69,7 @@ export const standardSchemaValidator =
|
|
|
70
69
|
|
|
71
70
|
const transformer = getTransformStrategy(validationSource)
|
|
72
71
|
|
|
73
|
-
return transformer(result.issues as
|
|
72
|
+
return transformer(result.issues as StandardSchemaV1Issue[])
|
|
74
73
|
},
|
|
75
74
|
async validateAsync({ value, validationSource }, fn) {
|
|
76
75
|
const result = await fn['~standard'].validate(value)
|
|
@@ -79,7 +78,7 @@ export const standardSchemaValidator =
|
|
|
79
78
|
|
|
80
79
|
const transformer = getTransformStrategy(validationSource)
|
|
81
80
|
|
|
82
|
-
return transformer(result.issues as
|
|
81
|
+
return transformer(result.issues as StandardSchemaV1Issue[])
|
|
83
82
|
},
|
|
84
83
|
}
|
|
85
84
|
}
|
|
@@ -88,3 +87,102 @@ export const isStandardSchemaValidator = (
|
|
|
88
87
|
validator: unknown,
|
|
89
88
|
): validator is StandardSchemaV1 =>
|
|
90
89
|
!!validator && '~standard' in (validator as object)
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The Standard Schema interface.
|
|
93
|
+
*/
|
|
94
|
+
export type StandardSchemaV1<Input = unknown, Output = Input> = {
|
|
95
|
+
/**
|
|
96
|
+
* The Standard Schema properties.
|
|
97
|
+
*/
|
|
98
|
+
readonly '~standard': StandardSchemaV1Props<Input, Output>
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The Standard Schema properties interface.
|
|
103
|
+
*/
|
|
104
|
+
interface StandardSchemaV1Props<Input = unknown, Output = Input> {
|
|
105
|
+
/**
|
|
106
|
+
* The version number of the standard.
|
|
107
|
+
*/
|
|
108
|
+
readonly version: 1
|
|
109
|
+
/**
|
|
110
|
+
* The vendor name of the schema library.
|
|
111
|
+
*/
|
|
112
|
+
readonly vendor: string
|
|
113
|
+
/**
|
|
114
|
+
* Validates unknown input values.
|
|
115
|
+
*/
|
|
116
|
+
readonly validate: (
|
|
117
|
+
value: unknown,
|
|
118
|
+
) => StandardSchemaV1Result<Output> | Promise<StandardSchemaV1Result<Output>>
|
|
119
|
+
/**
|
|
120
|
+
* Inferred types associated with the schema.
|
|
121
|
+
*/
|
|
122
|
+
readonly types?: StandardSchemaV1Types<Input, Output> | undefined
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The result interface of the validate function.
|
|
126
|
+
*/
|
|
127
|
+
type StandardSchemaV1Result<Output> =
|
|
128
|
+
| StandardSchemaV1SuccessResult<Output>
|
|
129
|
+
| StandardSchemaV1FailureResult
|
|
130
|
+
/**
|
|
131
|
+
* The result interface if validation succeeds.
|
|
132
|
+
*/
|
|
133
|
+
interface StandardSchemaV1SuccessResult<Output> {
|
|
134
|
+
/**
|
|
135
|
+
* The typed output value.
|
|
136
|
+
*/
|
|
137
|
+
readonly value: Output
|
|
138
|
+
/**
|
|
139
|
+
* The non-existent issues.
|
|
140
|
+
*/
|
|
141
|
+
readonly issues?: undefined
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The result interface if validation fails.
|
|
145
|
+
*/
|
|
146
|
+
interface StandardSchemaV1FailureResult {
|
|
147
|
+
/**
|
|
148
|
+
* The issues of failed validation.
|
|
149
|
+
*/
|
|
150
|
+
readonly issues: ReadonlyArray<StandardSchemaV1Issue>
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The issue interface of the failure output.
|
|
154
|
+
*/
|
|
155
|
+
interface StandardSchemaV1Issue {
|
|
156
|
+
/**
|
|
157
|
+
* The error message of the issue.
|
|
158
|
+
*/
|
|
159
|
+
readonly message: string
|
|
160
|
+
/**
|
|
161
|
+
* The path of the issue, if any.
|
|
162
|
+
*/
|
|
163
|
+
readonly path?:
|
|
164
|
+
| ReadonlyArray<PropertyKey | StandardSchemaV1PathSegment>
|
|
165
|
+
| undefined
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The path segment interface of the issue.
|
|
169
|
+
*/
|
|
170
|
+
interface StandardSchemaV1PathSegment {
|
|
171
|
+
/**
|
|
172
|
+
* The key representing a path segment.
|
|
173
|
+
*/
|
|
174
|
+
readonly key: PropertyKey
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The Standard Schema types interface.
|
|
178
|
+
*/
|
|
179
|
+
interface StandardSchemaV1Types<Input = unknown, Output = Input> {
|
|
180
|
+
/**
|
|
181
|
+
* The input type of the schema.
|
|
182
|
+
*/
|
|
183
|
+
readonly input: Input
|
|
184
|
+
/**
|
|
185
|
+
* The output type of the schema.
|
|
186
|
+
*/
|
|
187
|
+
readonly output: Output
|
|
188
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -280,17 +280,24 @@ export function getSyncValidatorArray<T>(
|
|
|
280
280
|
cause: ValidationCause,
|
|
281
281
|
options: SyncValidatorArrayPartialOptions<T>,
|
|
282
282
|
): T extends FieldValidators<any, any>
|
|
283
|
-
? Array<
|
|
283
|
+
? Array<
|
|
284
|
+
SyncValidator<T['onChange'] | T['onBlur'] | T['onSubmit'] | T['onMount']>
|
|
285
|
+
>
|
|
284
286
|
: T extends FormValidators<any, any>
|
|
285
|
-
? Array<
|
|
287
|
+
? Array<
|
|
288
|
+
SyncValidator<
|
|
289
|
+
T['onChange'] | T['onBlur'] | T['onSubmit'] | T['onMount']
|
|
290
|
+
>
|
|
291
|
+
>
|
|
286
292
|
: never {
|
|
287
|
-
const { onChange, onBlur, onSubmit } = (options.validators || {}) as
|
|
293
|
+
const { onChange, onBlur, onSubmit, onMount } = (options.validators || {}) as
|
|
288
294
|
| FieldValidators<any, any>
|
|
289
295
|
| FormValidators<any, any>
|
|
290
296
|
|
|
291
297
|
const changeValidator = { cause: 'change', validate: onChange } as const
|
|
292
298
|
const blurValidator = { cause: 'blur', validate: onBlur } as const
|
|
293
299
|
const submitValidator = { cause: 'submit', validate: onSubmit } as const
|
|
300
|
+
const mountValidator = { cause: 'mount', validate: onMount } as const
|
|
294
301
|
|
|
295
302
|
// Allows us to clear onServer errors
|
|
296
303
|
const serverValidator = {
|
|
@@ -299,6 +306,8 @@ export function getSyncValidatorArray<T>(
|
|
|
299
306
|
} as const
|
|
300
307
|
|
|
301
308
|
switch (cause) {
|
|
309
|
+
case 'mount':
|
|
310
|
+
return [mountValidator] as never
|
|
302
311
|
case 'submit':
|
|
303
312
|
return [
|
|
304
313
|
changeValidator,
|