Fix Zod Parse Errors on Nullable OpenAPI Fields
Debug Zod parse errors when OpenAPI nullable fields, optional properties, and OpenAPI 3.0 versus 3.1 schema output do not match runtime API payloads.
Quick answer: why nullable OpenAPI fields fail
Most nullable OpenAPI parse errors happen because the runtime payload returns `null` but the generated Zod schema expects a string, or because the field is omitted but the schema only modeled nullable. Nullable and optional are different contracts.
OpenAPI 3.0 nullable example
OpenAPI 3.0 commonly represents nullable values with `nullable: true`. A converter should turn that into a Zod nullable field, not a plain string.
{
"type": "object",
"required": ["id", "nickname"],
"properties": {
"id": { "type": "string" },
"nickname": {
"type": "string",
"nullable": true
}
}
}OpenAPI 3.1 nullable example
OpenAPI 3.1 aligns more closely with JSON Schema and can represent nullable values with a type union that includes `null`.
{
"type": "object",
"required": ["id", "nickname"],
"properties": {
"id": { "type": "string" },
"nickname": {
"type": ["string", "null"]
}
}
}Generated Zod fix
When the API can return `null`, the Zod schema needs `.nullable()`. When the API can omit the property, the schema needs `.optional()`.
const UserSchema = z.object({
id: z.string(),
nickname: z.string().nullable(),
avatarUrl: z.string().url().nullable().optional(),
});nullable is not optional
A nullable field can be present with the value `null`. An optional field can be missing. If the API does both, the Zod field must model both behaviors.
- Use `.nullable()` for `null` values.
- Use `.optional()` for omitted keys.
- Use `.nullable().optional()` only when both payload shapes are valid.
safeParse debugging workflow
Use `safeParse` while debugging contract drift so you can inspect issue paths without crashing the caller.
const result = UserSchema.safeParse(payload);
if (!result.success) {
console.table(
result.error.issues.map((issue) => ({
path: issue.path.join("."),
code: issue.code,
message: issue.message,
})),
);
}OpenAPI 3.0 vs 3.1 checklist
Check the OpenAPI version before deciding whether `nullable: true` or a `type` array should be expected in the schema document.
- For OpenAPI 3.0, review `nullable: true` on schema properties.
- For OpenAPI 3.1, review `type: ["string", "null"]` or equivalent JSON Schema output.
- Confirm `required` controls presence, not whether a present value can be null.
- Test generated Zod against real payloads, not only against the schema document.
Production checklist
Before shipping a generated OpenAPI-to-Zod schema, add tests for present values, null values, omitted fields, and malformed values.
- Keep nullable fixtures in test data.
- Review required arrays and optional properties together.
- Do not silence nullable parse errors with `z.any()`.
- Regenerate schemas after upstream OpenAPI nullable changes.
Nullable versus optional fields
| Payload behavior | Zod shape | Meaning |
|---|---|---|
| Field can be null | `z.string().nullable()` | The key is present and the value may be `null`. |
| Field can be omitted | `z.string().optional()` | The key may be missing from the object. |
| Field can be null or omitted | `z.string().nullable().optional()` | The API may return `null` or omit the field entirely. |
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.
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
Why does Zod reject null from an OpenAPI field?
The generated schema probably expects a non-null value. Add or generate `.nullable()` when the OpenAPI field is allowed to return `null`.
Is nullable the same as optional in Zod?
No. Nullable means the value can be `null`. Optional means the key can be omitted. Some API fields need both.
How does OpenAPI 3.1 represent nullable fields?
OpenAPI 3.1 can use JSON Schema style type unions such as `type: ["string", "null"]` instead of OpenAPI 3.0's `nullable: true`.
Should I use safeParse for nullable field debugging?
Yes. `safeParse` lets you inspect issue paths and messages without throwing, which is useful while comparing real payloads to generated schemas.
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.