@tanstack/form-core 1.4.0 → 1.5.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 +12 -0
- package/dist/cjs/FieldApi.cjs.map +1 -1
- package/dist/cjs/FieldApi.d.cts +12 -0
- package/dist/cjs/FormApi.cjs +12 -0
- package/dist/cjs/FormApi.cjs.map +1 -1
- package/dist/cjs/FormApi.d.cts +18 -0
- package/dist/cjs/standardSchemaValidator.cjs +15 -7
- package/dist/cjs/standardSchemaValidator.cjs.map +1 -1
- package/dist/cjs/standardSchemaValidator.d.cts +8 -18
- package/dist/cjs/util-types.d.cts +1 -3
- package/dist/esm/FieldApi.d.ts +12 -0
- package/dist/esm/FieldApi.js +13 -1
- package/dist/esm/FieldApi.js.map +1 -1
- package/dist/esm/FormApi.d.ts +18 -0
- package/dist/esm/FormApi.js +13 -1
- package/dist/esm/FormApi.js.map +1 -1
- package/dist/esm/standardSchemaValidator.d.ts +8 -18
- package/dist/esm/standardSchemaValidator.js +15 -7
- package/dist/esm/standardSchemaValidator.js.map +1 -1
- package/dist/esm/util-types.d.ts +1 -3
- package/package.json +1 -1
- package/src/FieldApi.ts +24 -0
- package/src/FormApi.ts +26 -0
- package/src/standardSchemaValidator.ts +38 -23
- package/src/util-types.ts +4 -8
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
import type { ValidationSource } from './types'
|
|
2
2
|
|
|
3
|
-
export type TStandardSchemaValidatorValue<
|
|
3
|
+
export type TStandardSchemaValidatorValue<
|
|
4
|
+
TData,
|
|
5
|
+
TSource extends ValidationSource = ValidationSource,
|
|
6
|
+
> = {
|
|
4
7
|
value: TData
|
|
5
|
-
validationSource:
|
|
8
|
+
validationSource: TSource
|
|
6
9
|
}
|
|
7
10
|
|
|
11
|
+
export type TStandardSchemaValidatorIssue<
|
|
12
|
+
TSource extends ValidationSource = ValidationSource,
|
|
13
|
+
> = TSource extends 'form'
|
|
14
|
+
? {
|
|
15
|
+
form: Record<string, StandardSchemaV1Issue[]>
|
|
16
|
+
fields: Record<string, StandardSchemaV1Issue[]>
|
|
17
|
+
}
|
|
18
|
+
: TSource extends 'field'
|
|
19
|
+
? StandardSchemaV1Issue[]
|
|
20
|
+
: never
|
|
21
|
+
|
|
8
22
|
function prefixSchemaToErrors(issues: readonly StandardSchemaV1Issue[]) {
|
|
9
23
|
const schema = new Map<string, StandardSchemaV1Issue[]>()
|
|
10
24
|
|
|
@@ -26,30 +40,24 @@ function prefixSchemaToErrors(issues: readonly StandardSchemaV1Issue[]) {
|
|
|
26
40
|
return Object.fromEntries(schema)
|
|
27
41
|
}
|
|
28
42
|
|
|
29
|
-
const
|
|
30
|
-
issues
|
|
31
|
-
|
|
32
|
-
const defaultFormTransformer = (issues: readonly StandardSchemaV1Issue[]) => {
|
|
43
|
+
const transformFormIssues = <TSource extends ValidationSource>(
|
|
44
|
+
issues: readonly StandardSchemaV1Issue[],
|
|
45
|
+
): TStandardSchemaValidatorIssue<TSource> => {
|
|
33
46
|
const schemaErrors = prefixSchemaToErrors(issues)
|
|
34
47
|
return {
|
|
35
48
|
form: schemaErrors,
|
|
36
49
|
fields: schemaErrors,
|
|
37
|
-
}
|
|
50
|
+
} as TStandardSchemaValidatorIssue<TSource>
|
|
38
51
|
}
|
|
39
52
|
|
|
40
|
-
const transformIssues = (
|
|
41
|
-
validationSource: 'form' | 'field',
|
|
42
|
-
issues: readonly StandardSchemaV1Issue[],
|
|
43
|
-
) =>
|
|
44
|
-
validationSource === 'form'
|
|
45
|
-
? defaultFormTransformer(issues)
|
|
46
|
-
: defaultFieldTransformer(issues)
|
|
47
|
-
|
|
48
53
|
export const standardSchemaValidators = {
|
|
49
|
-
validate(
|
|
50
|
-
{
|
|
54
|
+
validate<TSource extends ValidationSource = ValidationSource>(
|
|
55
|
+
{
|
|
56
|
+
value,
|
|
57
|
+
validationSource,
|
|
58
|
+
}: TStandardSchemaValidatorValue<unknown, TSource>,
|
|
51
59
|
schema: StandardSchemaV1,
|
|
52
|
-
) {
|
|
60
|
+
): TStandardSchemaValidatorIssue<TSource> | undefined {
|
|
53
61
|
const result = schema['~standard'].validate(value)
|
|
54
62
|
|
|
55
63
|
if (result instanceof Promise) {
|
|
@@ -58,17 +66,24 @@ export const standardSchemaValidators = {
|
|
|
58
66
|
|
|
59
67
|
if (!result.issues) return
|
|
60
68
|
|
|
61
|
-
|
|
69
|
+
if (validationSource === 'field')
|
|
70
|
+
return result.issues as TStandardSchemaValidatorIssue<TSource>
|
|
71
|
+
return transformFormIssues<TSource>(result.issues)
|
|
62
72
|
},
|
|
63
|
-
async validateAsync(
|
|
64
|
-
{
|
|
73
|
+
async validateAsync<TSource extends ValidationSource>(
|
|
74
|
+
{
|
|
75
|
+
value,
|
|
76
|
+
validationSource,
|
|
77
|
+
}: TStandardSchemaValidatorValue<unknown, TSource>,
|
|
65
78
|
schema: StandardSchemaV1,
|
|
66
|
-
) {
|
|
79
|
+
): Promise<TStandardSchemaValidatorIssue<TSource> | undefined> {
|
|
67
80
|
const result = await schema['~standard'].validate(value)
|
|
68
81
|
|
|
69
82
|
if (!result.issues) return
|
|
70
83
|
|
|
71
|
-
|
|
84
|
+
if (validationSource === 'field')
|
|
85
|
+
return result.issues as TStandardSchemaValidatorIssue<TSource>
|
|
86
|
+
return transformFormIssues<TSource>(result.issues)
|
|
72
87
|
},
|
|
73
88
|
}
|
|
74
89
|
|
package/src/util-types.ts
CHANGED
|
@@ -19,8 +19,6 @@ type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch
|
|
|
19
19
|
*/
|
|
20
20
|
export type Narrow<A> = Try<A, [], NarrowRaw<A>>
|
|
21
21
|
|
|
22
|
-
type IsAny<T> = 0 extends 1 & T ? true : false
|
|
23
|
-
|
|
24
22
|
export interface AnyDeepKeyAndValue {
|
|
25
23
|
key: string
|
|
26
24
|
value: any
|
|
@@ -117,15 +115,13 @@ export interface UnknownDeepKeyAndValue<TParent extends AnyDeepKeyAndValue> {
|
|
|
117
115
|
value: unknown
|
|
118
116
|
}
|
|
119
117
|
|
|
120
|
-
export type DeepKeyAndValueUnknown<TParent extends AnyDeepKeyAndValue> =
|
|
121
|
-
UnknownDeepKeyAndValue<TParent>
|
|
122
|
-
|
|
123
118
|
export type DeepKeysAndValues<
|
|
124
119
|
T,
|
|
125
120
|
TParent extends AnyDeepKeyAndValue = never,
|
|
126
121
|
TAcc = never,
|
|
127
|
-
> =
|
|
128
|
-
|
|
122
|
+
> = unknown extends T
|
|
123
|
+
? TAcc | UnknownDeepKeyAndValue<TParent>
|
|
124
|
+
: unknown extends T // this stops runaway recursion when T is any
|
|
129
125
|
? T
|
|
130
126
|
: T extends string | number | boolean | bigint | Date
|
|
131
127
|
? TAcc
|
|
@@ -134,7 +130,7 @@ export type DeepKeysAndValues<
|
|
|
134
130
|
? DeepKeyAndValueArray<TParent, T, TAcc>
|
|
135
131
|
: DeepKeyAndValueTuple<TParent, T, TAcc>
|
|
136
132
|
: keyof T extends never
|
|
137
|
-
? TAcc |
|
|
133
|
+
? TAcc | UnknownDeepKeyAndValue<TParent>
|
|
138
134
|
: T extends object
|
|
139
135
|
? DeepKeyAndValueObject<TParent, T, TAcc>
|
|
140
136
|
: TAcc
|