{"record":{"id":"168b5634da54477b","repo":"colinhacks/zod","slug":"date-cannot-be-represented-in-json-schema","errorCode":null,"errorMessage":"Date cannot be represented in JSON Schema","messagePattern":"Date cannot be represented in JSON Schema","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v4/core/json-schema-processors.ts","lineNumber":150,"sourceCode":"    throw new Error(\"Void cannot be represented in JSON Schema\");\n  }\n};\n\nexport const neverProcessor: Processor<schemas.$ZodNever> = (_schema, _ctx, json, _params) => {\n  json.not = {};\n};\n\nexport const anyProcessor: Processor<schemas.$ZodAny> = (_schema, _ctx, _json, _params) => {\n  // empty schema accepts anything\n};\n\nexport const unknownProcessor: Processor<schemas.$ZodUnknown> = (_schema, _ctx, _json, _params) => {\n  // empty schema accepts anything\n};\n\nexport const dateProcessor: Processor<schemas.$ZodDate> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"Date cannot be represented in JSON Schema\");\n  }\n};\n\nexport const enumProcessor: Processor<schemas.$ZodEnum> = (schema, _ctx, json, _params) => {\n  const def = schema._zod.def as schemas.$ZodEnumDef;\n  const values = getEnumValues(def.entries);\n  // Number enums can have both string and number values\n  if (values.every((v) => typeof v === \"number\")) json.type = \"number\";\n  if (values.every((v) => typeof v === \"string\")) json.type = \"string\";\n  json.enum = values;\n};\n\nexport const literalProcessor: Processor<schemas.$ZodLiteral> = (schema, ctx, json, _params) => {\n  const def = schema._zod.def as schemas.$ZodLiteralDef<any>;\n  const vals: (string | number | boolean | null)[] = [];\n  for (const val of def.values) {\n    if (val === undefined) {\n      if (ctx.unrepresentable === \"throw\") {","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/colinhacks/zod/blob/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/core/json-schema-processors.ts#L132-L168","documentation":"Thrown by toJSONSchema() when a ZodDate schema (z.date()) is encountered with unrepresentable 'throw' (default). ZodDate validates a JS Date object instance; JSON Schema has no date-instance type (only string formats like 'date-time'), so emitting one would change the validation semantics and the converter refuses.","triggerScenarios":"Calling toJSONSchema() on a schema containing z.date(). Generating OpenAPI from a model whose timestamps are Date instances rather than ISO strings.","commonSituations":"Server models that parse into Date objects for runtime use, but whose wire/API contract is an ISO 8601 string — exporting the runtime schema directly mismatches the wire format.","solutions":["For the exported/JSON contract, use z.iso.datetime() or z.string().datetime() (which produce { type: 'string', format: 'date-time' }) instead of z.date().","Pass { unrepresentable: 'any' } to toJSONSchema() to emit {} for date fields if you accept the loss of validation metadata.","Maintain two schemas: a runtime schema with z.date() and an export schema with z.iso.datetime(), sharing field shape via composition."],"exampleFix":"// before (throws)\nconst Schema = z.object({ createdAt: z.date() });\nz.toJSONSchema(Schema);\n\n// after (string date-time for the JSON contract)\nconst Schema = z.object({ createdAt: z.iso.datetime() });\nz.toJSONSchema(Schema);\n// { type: 'object', properties: { createdAt: { type: 'string', format: 'date-time' } } }","handlingStrategy":"try-catch","validationCode":"// Prefer exporting the wire (string) shape rather than the Date-instance shape.\nfunction exportSchema(runtimeSchema) {\n  // Replace z.date() with z.iso.datetime() for export, or accept the any fallback.\n  try {\n    return z.toJSONSchema(runtimeSchema);\n  } catch (e) {\n    if (e.message === 'Date cannot be represented in JSON Schema') {\n      return z.toJSONSchema(runtimeSchema, { unrepresentable: 'any' });\n    }\n    throw e;\n  }\n}","typeGuard":"function hasDate(schema) {\n  return schema._zod.def.type === 'date';\n}","tryCatchPattern":"try {\n  return z.toJSONSchema(schema);\n} catch (e) {\n  if (e.message === 'Date cannot be represented in JSON Schema') {\n    // Either downgrade to string date-time or allow the any fallback\n    return z.toJSONSchema(schema, { unrepresentable: 'any' });\n  }\n  throw e;\n}","preventionTips":["Model external/API contracts with z.iso.datetime() / z.string().datetime() and reserve z.date() for runtime parsing.","When you must export a Date-instance schema, pass { unrepresentable: 'any' } explicitly.","Document each model as 'runtime' or 'export' to avoid mixing the two."],"tags":["json-schema","date","unrepresentable","to-json-schema","openapi"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}