@tanstack/form-core 1.4.1 → 1.6.1
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 +22 -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/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 +23 -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/package.json +1 -1
- package/src/FieldApi.ts +24 -0
- package/src/FormApi.ts +39 -0
- package/src/standardSchemaValidator.ts +38 -23
|
@@ -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
|
|