TanStack
Validation

Validation Outputs

The values that a form works with might not be the values you need to send to the backend. For example, an optional description might be an empty string in the form, but it needs to be stored as null. We need to transform an empty description to null before submission.

Transform in onSubmit

For simple transformations, onSubmit is more than enough. Adjust the value just before sending it.

ts
const form = useForm({
  defaultValues: { description: '' },
  onSubmit: async ({ value }) => {
    const textOrNull = value.description === '' ? null : value.description

    await saveProfile({ description: textOrNull })
  },
})

Reuse a schema's output

If you're using a Standard Schema library like Zod or Valibot, the schema may have already transformed the value for the backend. However, onSubmit's value property always contains the form's values without schema transformations.

To reuse the schema's transformed value in onSubmit, read it from validatorOutputs. This array has one entry for each form-level validator, in the same order as the validators array. A schema validator's entry contains its output, including any transformations.

In the examples below, the schema is the first and only validator, so schemaResult contains its output:

Zod
import { useForm } from '@tanstack/react-form'
import { z } from 'zod'

const optionalText = z.string().transform((str) => (str === '' ? null : str))

const schema = z.object({
  description: optionalText,
})

function ProfileForm() {
  const form = useForm({
    defaultValues: { description: '' },
    validators: [
      {
        run: schema,
      },
    ],
    onSubmit: async ({ validatorOutputs }) => {
      const [schemaResult] = validatorOutputs

      await saveProfile(schemaResult)
    },
  })
}

Reuse a validator function's output

You may have to transform the form's values before validating them. To avoid parsing the values twice, you can return any data from a validator by wrapping it in createOutput. Output data does not count as a validation error.

To access the output data, onSubmit receives validatorOutputs, an array with one entry for each form-level validator, in the same order as the validators array.

ts
const form = useForm({
  defaultValues: { description: '' },
  validators: [
    {
      run: ({ value }) => {
        if (value.description.length > 200) {
          return 'Description is too long!'
        }

        return undefined
      },
    },
    {
      run: ({ value, createOutput }) => {
        const description = value.description === '' ? null : value.description

        // ... do validation

        return createOutput({ description })
      },
    },
  ],
  onSubmit: async ({ validatorOutputs }) => {
    const [nothing, output] = validatorOutputs

    await saveProfile(output)
  },
})

Reusing data across validators

Currently, while onSubmit can reuse data from validators, you cannot reuse data between validators. For example, suppose the schema and the validator function both need to parse the description first. The recommended way to avoid parsing it twice is to merge the two validators:

Zod
import { useForm } from '@tanstack/react-form'
import { z } from 'zod'

const optionalText = z.string().transform((str) => (str === '' ? null : str))

const schema = z.object({
  description: optionalText,
})

function ProfileForm() {
  const form = useForm({
    defaultValues: { description: '' },
    validators: [
      {
        run: ({ value, parseIssues, createOutput }) => {
          const result = schema.safeParse(value)

          if (!result.success) return parseIssues(result.error.issues)

          // validate with result.data
          return createOutput(result.data)
        },
      },
    ],
    onSubmit: async ({ validatorOutputs }) => {
      const [schemaResult] = validatorOutputs

      await saveProfile(schemaResult)
    },
  })
}

Schemas and validator functions

For the curious, here's roughly what happens behind the scenes when you pass a schema to run:

ts
import { useForm, type StandardSchemaV1 } from '@tanstack/react-form'

// Compatible libraries implement this interface.
declare const schema: StandardSchemaV1<{ description: string }>

useForm({
  defaultValues: { description: '' },
  validators: [
    { run: schema },
    {
      // The same behavior using a function:
      run: async ({ value, parseIssues, createOutput }) => {
        const result = await schema['~standard'].validate(value)

        if (result.issues) return parseIssues(result.issues)

        return createOutput(result.value)
      },
    },
  ],
})