io-ts schema will appear here...
What is JSON to io-ts Schema Generator?
io-ts is a runtime type system for TypeScript that provides both validation and type safety. It allows you to define types that exist at runtime (unlike TypeScript interfaces) and validate data against them. This tool automatically generates io-ts type definitions from JSON samples, eliminating the need to manually write t.type, t.string, t.number, and other constructors.
How to Use
- Paste your JSON into the left panel.
- Click "Generate" to produce the io-ts schema.
- Copy the output and paste it into your TypeScript project.
- Install io-ts: npm install io-ts fp-ts
- Use the schema for runtime validation with Schema.decode(data).
Why Use This Tool?
Tips & Best Practices
- io-ts requires fp-ts as a peer dependency for functional programming utilities
- Use Schema.decode(data) to validate — it returns Either<Errors, T>
- For optional fields, manually change to t.union([t.string, t.undefined])
- Add refinements with t.brand for more specific constraints (e.g., positive numbers)
- Combine with io-ts-types for common patterns like DateFromISOString
Frequently Asked Questions
What is the difference between io-ts and Zod?
Both provide runtime validation with TypeScript type inference. io-ts is older and based on fp-ts functional programming patterns, returning Either<Errors, T> for validation. Zod is more modern with a simpler API, throwing errors or returning safe parse results. io-ts is preferred in functional programming contexts; Zod is easier for most developers.
How do I validate data with io-ts?
Use the decode method: const result = Schema.decode(data). The result is an Either type from fp-ts. Use isRight(result) to check success, then result.right for the validated data. For errors, result.left contains detailed error information. Example: import { isRight } from "fp-ts/Either"; if (isRight(result)) console.log(result.right);
Can I make fields optional in io-ts?
The generator creates exact types from the JSON sample. To make a field optional, change t.string to t.union([t.string, t.undefined]) or use the t.partial helper for objects: t.partial({ optionalField: t.string }). Review the generated schema and adjust as needed for your API.