@tanstack/form-core 0.40.0 → 0.40.2
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.map +1 -1
- package/dist/cjs/FieldApi.d.cts +3 -2
- package/dist/cjs/FormApi.cjs.map +1 -1
- package/dist/cjs/FormApi.d.cts +3 -3
- package/dist/cjs/standardSchemaValidator.cjs.map +1 -1
- package/dist/cjs/standardSchemaValidator.d.cts +94 -4
- package/dist/esm/FieldApi.d.ts +3 -2
- package/dist/esm/FieldApi.js.map +1 -1
- package/dist/esm/FormApi.d.ts +3 -3
- package/dist/esm/FormApi.js.map +1 -1
- package/dist/esm/standardSchemaValidator.d.ts +94 -4
- package/dist/esm/standardSchemaValidator.js.map +1 -1
- package/package.json +1 -2
- package/src/FieldApi.ts +15 -54
- package/src/FormApi.ts +3 -3
- package/src/standardSchemaValidator.ts +109 -11
|
@@ -3,16 +3,15 @@ import type {
|
|
|
3
3
|
Validator,
|
|
4
4
|
ValidatorAdapterParams,
|
|
5
5
|
} from './types'
|
|
6
|
-
import type { v1 } 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,
|
|
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,12 +78,111 @@ 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
|
}
|
|
86
85
|
|
|
87
86
|
export const isStandardSchemaValidator = (
|
|
88
87
|
validator: unknown,
|
|
89
|
-
): validator is
|
|
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
|
+
}
|