BASELibraryCatalogs

Forms and data validation in TypeScript

A form should retain entered data, explain errors, and submit correctly. TypeScript describes types during development, but data from a browser, file, or API still needs runtime validation. Choosing tools is easier when you identify the form state library and the valid-data schema separately.

Define data boundaries first

Prepare examples of valid input and edge cases: an empty string, a missing field, an invalid date, or an overly long value. Zod and Valibot express validation as a schema. Zod links schemas with TypeScript type inference; Valibot offers modular imports and transformations.

Choose one main schema system for a particular application boundary. Check integration with existing code, error formats, and the size of the bundle actually built. A library's name does not replace measurements in your own project.

  • Distinguish missing values, empty strings, and defaults.
  • Keep server-side checks even when the client validates input.
  • Make transformation outputs explicit: for example, convert an input string to a number only after validation.

Add form handling and errors

React Hook Form handles input in React. TanStack Form is a headless option with typed state and synchronous or asynchronous validation. Choose one form approach, then align it with your schema and UI components.

Using a registration form, test input, a local error, server rejection, and resubmission. Messages should sit beside the relevant field, and entered data should remain after failure. Vitest helps capture schema edge cases without running the entire form.

  • Show the submission state and prevent accidental duplicate requests.
  • Test an asynchronous error for a value the user has already changed.
  • Make errors clear in words; color alone is insufficient.

Align the form with the API

If the backend uses Zod, zod-to-openapi can generate an OpenAPI description from schemas. It connects schemas and documentation; it does not guarantee that every endpoint implements the contract. Compare the generated description with actual requests and responses.

The listed code libraries do not require a mandatory subscription. Servers, third-party APIs, and application operations have separate costs. Form validation does not replace authorization, upload limits, or protection of server operations.

  • Check schema and library versions when updating dependencies.
  • Do not treat a static TypeScript type as validation of a network response.
  • Agree on error wording, data formats, and the server contract before adding many fields.

Tools and why to choose them

  • Zod

    Define runtime data schemas with TypeScript type inference and validate application boundaries.

  • Valibot

    An alternative schema system with modular imports and transformations.

  • React Hook Form

    Manage React form state and input; choose it separately from the schema library.

  • TanStack Form

    Build headless forms with typed state and asynchronous validation.

  • zod-to-openapi

    Describe APIs from Zod schemas when the project already uses Zod at its server boundary.

  • Vitest

    Test empty, invalid, and boundary values in schemas and transformations.

How BASE selects and reviews information

Other topics · Entire library