Swagger to Zod for TypeScript
Convert Swagger/OpenAPI JSON schema components into Zod validators for TypeScript projects.
Swagger and OpenAPI inputs
Swagger 2.0 and OpenAPI 3.x describe similar API contracts with different document shapes. FrameworkKit accepts Swagger 2.0 style `definitions` and OpenAPI 3.x `components.schemas` so TypeScript teams can generate validators from both legacy and modern API specs.
Recommended workflow
Start with shared definitions, inspect the generated validators, then refine edge cases such as custom formats, nullable values, and oneOf/anyOf unions. Keep generated validators close to API clients, webhooks, queue consumers, or route handlers that receive unknown data.
- Generate from `definitions` when the source is Swagger 2.0.
- Generate from `components.schemas` when the source is OpenAPI 3.x.
- Use fixture tests before relying on generated schemas in production.
Swagger 2.0 example
Older Swagger files often keep reusable schemas under `definitions`. Generate validators from those definitions, then migrate the source contract toward OpenAPI components when possible.
{
"swagger": "2.0",
"definitions": {
"User": {
"type": "object",
"required": ["id", "email"],
"properties": { "id": { "type": "string" }, "email": { "type": "string" } }
}
}
}TypeScript runtime boundary
Use generated Zod schemas where TypeScript cannot protect you: parsed JSON, fetch responses, queue messages, webhooks, and data entering server actions.
const result = WebhookEventSchema.safeParse(await request.json());
if (!result.success) {
return Response.json(
{ error: "Invalid webhook payload", issues: result.error.issues },
{ status: 400 },
);
}Refinement pass
Swagger contracts may describe broad strings where the application expects IDs, dates, URLs, or enum values. Add Zod refinements after generation when the business rule is stricter than the contract.
Swagger 2.0 required arrays
Swagger 2.0 keeps required property names in a `required` array on the object schema. During conversion, keys in that array should stay required while other properties become optional. This is separate from nullable handling, which is more explicit in OpenAPI 3.x.
- Check every object with a `required` array.
- Confirm optional generated fields match real response payloads.
- Do not assume a missing key and a `null` value mean the same thing.
References and circular models
Legacy Swagger definitions often reuse models through `$ref`. Keep referenced schemas named and reusable, then review circular or mutually recursive models before copying the result into application code.
{
"definitions": {
"Post": {
"type": "object",
"properties": {
"author": { "$ref": "#/definitions/User" }
}
}
}
}FrameworkKit vs generated clients
Use FrameworkKit when the immediate job is to convert and review Zod validators in the browser. Use openapi-zod-client, Orval, Kubb, or another generator when the team wants a committed typed client that regenerates from the Swagger or OpenAPI document.
Migration checklist
Compare generated schemas against fixture payloads, then document whether Swagger or app code owns each validator so contract drift is visible.
- Keep generated type names stable across releases.
- Review local references and circular schema patterns.
- Add tests for nullable, missing, and malformed fields.
- Plan the migration from Swagger `definitions` to OpenAPI `components.schemas` if the API contract is still actively maintained.
Swagger to Zod migration map
| Contract source | Zod output | Migration note |
|---|---|---|
| Swagger 2.0 definitions | Named validators from `definitions`. | Check required arrays and legacy format names. |
| OpenAPI 3 components | Named validators from `components.schemas`. | Prefer this shape for new contracts. |
| Shared references | Validators that reuse local component schemas. | Confirm referenced names are stable. |
| Custom formats | String validators or review notes. | Add manual refinements when the format is domain-specific. |
Use FrameworkKit to generate the starter code, then review the output before shipping it in production.
Generate with OpenAPI to ZodOpenAPI to Zod resources
OpenAPI to Zod converter
Convert OpenAPI or Swagger schema components into runtime Zod validators.
Zod API schemas
Validate request bodies and API responses with Zod before data reaches application code.
OpenAPI to Zod for tRPC
Use contract-first OpenAPI schemas with Zod validation around tRPC-style TypeScript procedures.
Nullable OpenAPI field errors
Fix Zod parse failures when OpenAPI nullable fields are generated as optional or non-null schemas.
OpenAPI to Zod validators
Review OpenAPI component examples before generating runtime Zod validators.
Zod to JSON Schema Converter
Use the opposite direction when TypeScript-owned Zod schemas need portable JSON Schema output.
Zod API schemas
Convert TypeScript-owned Zod contracts into OpenAPI-compatible schema objects.
openapi-zod-client alternatives
Compare the openapi-zod-client npm package with the browser-only OpenAPI to Zod converter and manual schemas.
FrameworkKit vs OpenAPI code generators
Compare FrameworkKit with openapi-zod-client, Orval, and Kubb for OpenAPI to Zod workflows.
FAQ
Can Swagger 2.0 definitions be converted to Zod?
Yes. FrameworkKit accepts common Swagger 2.0 `definitions` and OpenAPI 3.x `components.schemas` in JSON input.
Why use Zod with Swagger in TypeScript?
Swagger describes the API contract, while Zod validates unknown runtime data before TypeScript code trusts it.
Do generated schemas include inferred TypeScript types?
Yes. The generated Zod validators can be paired with inferred TypeScript types for parsed data.
Are oneOf and anyOf always automatic?
Simple unions can be generated, but complex polymorphic schemas should be reviewed and tested with real fixtures.
Related comparisons
Zod vs JSON Schema
Choose Zod for TypeScript-first runtime validation and inferred types; choose JSON Schema for portable contracts across APIs, AJV, OpenAPI, and non-TypeScript systems.
OpenAPI to Zod vs Manual Schemas
Compare generated Zod schemas from OpenAPI with manually maintained runtime validators.
OpenAPI to Zod Converter vs openapi-zod-client, Orval, and Kubb
Compare FrameworkKit's browser-only OpenAPI to Zod converter with openapi-zod-client, Orval, and Kubb code-generation workflows.
Related tools
Zod to JSON Schema Converter
Use a free browser-only online converter to turn Zod 4 schemas into JSON Schema for Draft 2020-12, Draft 7, AJV, or OpenAPI-compatible output.
JSON to Zod Converter
Convert JSON to Zod schemas with strict objects, optional field inference, and inferred TypeScript types.
JSON Schema to Zod
Convert a JSON Schema document into Zod validators and inferred TypeScript types in your browser.