Skip to content

Commit c1f5747

Browse files
authored
fix: type for use with decode-formdata (#2052)
1 parent f88faaf commit c1f5747

4 files changed

Lines changed: 22 additions & 6 deletions

File tree

.changeset/lucky-beds-rush.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@tanstack/react-form-nextjs': patch
3+
'@tanstack/react-form-remix': patch
4+
'@tanstack/react-form-start': patch
5+
---
6+
7+
Fixes bad inference from `decode-formdata`'s weird typing of the `decode` function, including handling how it incorrectly doesn't handle undefined values for the form info object.

packages/react-form-nextjs/src/createServerValidate.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
ServerFormState,
1515
UnwrapFormAsyncValidateOrFn,
1616
} from '@tanstack/react-form'
17+
import type { FormDataInfo } from 'decode-formdata'
1718

1819
interface CreateServerValidateOptions<
1920
TFormData,
@@ -75,7 +76,7 @@ export const createServerValidate =
7576
TSubmitMeta
7677
>,
7778
) =>
78-
async (formData: FormData, info?: Parameters<typeof decode>[1]) => {
79+
async (formData: FormData, info?: FormDataInfo) => {
7980
const { onServerValidate } = defaultOpts
8081

8182
const runValidator = async ({
@@ -98,7 +99,9 @@ export const createServerValidate =
9899
})
99100
}
100101

101-
const values = decode(formData, info) as never as TFormData
102+
const values = (info
103+
? decode(formData, info)
104+
: decode(formData)) as never as TFormData
102105

103106
const onServerError = (await runValidator({
104107
value: values,

packages/react-form-remix/src/createServerValidate.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
ServerFormState,
1515
UnwrapFormAsyncValidateOrFn,
1616
} from '@tanstack/react-form'
17+
import type { FormDataInfo } from 'decode-formdata'
1718

1819
interface CreateServerValidateOptions<
1920
TFormData,
@@ -75,7 +76,7 @@ export const createServerValidate =
7576
TSubmitMeta
7677
>,
7778
) =>
78-
async (formData: FormData, info?: Parameters<typeof decode>[1]) => {
79+
async (formData: FormData, info?: FormDataInfo) => {
7980
const { onServerValidate } = defaultOpts
8081

8182
const runValidator = async ({
@@ -98,7 +99,9 @@ export const createServerValidate =
9899
})
99100
}
100101

101-
const values = decode(formData, info) as never as TFormData
102+
const values = (info
103+
? decode(formData, info)
104+
: decode(formData)) as never as TFormData
102105

103106
const onServerError = (await runValidator({
104107
value: values,

packages/react-form-start/src/createServerValidate.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
ServerFormState,
1818
UnwrapFormAsyncValidateOrFn,
1919
} from '@tanstack/react-form'
20+
import type { FormDataInfo } from 'decode-formdata'
2021

2122
interface CreateServerValidateOptions<
2223
TFormData,
@@ -57,7 +58,7 @@ const serverFn = createServerFn({ method: 'POST' })
5758
.handler(async ({ data }) => {
5859
const { formData, info, defaultOpts } = data as {
5960
formData: FormData
60-
info?: Parameters<typeof decode>[1]
61+
info?: FormDataInfo
6162
defaultOpts: CreateServerValidateOptions<
6263
any,
6364
any,
@@ -97,7 +98,9 @@ const serverFn = createServerFn({ method: 'POST' })
9798

9899
const referer = getRequestHeader('referer')!
99100

100-
const decodedData = decode(formData, info) as never as any
101+
const decodedData = (info
102+
? decode(formData, info)
103+
: decode(formData)) as never as any
101104

102105
const onServerError = (await runValidator({
103106
value: decodedData,

0 commit comments

Comments
 (0)