@tanstack/form-core 0.39.1 → 0.40.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.
@@ -0,0 +1,90 @@
1
+ import type {
2
+ ValidationError,
3
+ Validator,
4
+ ValidatorAdapterParams,
5
+ } from './types'
6
+ import type { StandardSchemaV1 } from '@standard-schema/spec'
7
+
8
+ type Params = ValidatorAdapterParams<StandardSchemaV1.Issue>
9
+ type TransformFn = NonNullable<Params['transformErrors']>
10
+
11
+ function prefixSchemaToErrors(
12
+ issues: readonly StandardSchemaV1.Issue[],
13
+ transformErrors: TransformFn,
14
+ ) {
15
+ const schema = new Map<string, StandardSchemaV1.Issue[]>()
16
+
17
+ for (const issue of issues) {
18
+ const path = [...(issue.path ?? [])]
19
+ .map((segment) => {
20
+ const normalizedSegment =
21
+ typeof segment === 'object' ? segment.key : segment
22
+ return typeof normalizedSegment === 'number'
23
+ ? `[${normalizedSegment}]`
24
+ : normalizedSegment
25
+ })
26
+ .join('.')
27
+ .replace(/\.\[/g, '[')
28
+
29
+ schema.set(path, (schema.get(path) ?? []).concat(issue))
30
+ }
31
+
32
+ const transformedSchema = {} as Record<string, ValidationError>
33
+
34
+ schema.forEach((value, key) => {
35
+ transformedSchema[key] = transformErrors(value)
36
+ })
37
+
38
+ return transformedSchema
39
+ }
40
+
41
+ function defaultFormTransformer(transformErrors: TransformFn) {
42
+ return (issues: readonly StandardSchemaV1.Issue[]) => ({
43
+ form: transformErrors(issues as StandardSchemaV1.Issue[]),
44
+ fields: prefixSchemaToErrors(issues, transformErrors),
45
+ })
46
+ }
47
+
48
+ export const standardSchemaValidator =
49
+ (params: Params = {}): Validator<unknown, StandardSchemaV1> =>
50
+ () => {
51
+ const transformFieldErrors =
52
+ params.transformErrors ??
53
+ ((issues: StandardSchemaV1.Issue[]) =>
54
+ issues.map((issue) => issue.message).join(', '))
55
+
56
+ const getTransformStrategy = (validationSource: 'form' | 'field') =>
57
+ validationSource === 'form'
58
+ ? defaultFormTransformer(transformFieldErrors)
59
+ : transformFieldErrors
60
+
61
+ return {
62
+ validate({ value, validationSource }, fn) {
63
+ const result = fn['~standard'].validate(value)
64
+
65
+ if (result instanceof Promise) {
66
+ throw new Error('async function passed to sync validator')
67
+ }
68
+
69
+ if (!result.issues) return
70
+
71
+ const transformer = getTransformStrategy(validationSource)
72
+
73
+ return transformer(result.issues as StandardSchemaV1.Issue[])
74
+ },
75
+ async validateAsync({ value, validationSource }, fn) {
76
+ const result = await fn['~standard'].validate(value)
77
+
78
+ if (!result.issues) return
79
+
80
+ const transformer = getTransformStrategy(validationSource)
81
+
82
+ return transformer(result.issues as StandardSchemaV1.Issue[])
83
+ },
84
+ }
85
+ }
86
+
87
+ export const isStandardSchemaValidator = (
88
+ validator: unknown,
89
+ ): validator is StandardSchemaV1 =>
90
+ !!validator && '~standard' in (validator as object)