{"record":{"id":"25a6f2edda964aaa","repo":"colinhacks/zod","slug":"nan-cannot-be-represented-in-json-schema","errorCode":null,"errorMessage":"NaN cannot be represented in JSON Schema","messagePattern":"NaN 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":204,"sourceCode":"    const val = vals[0]!;\n    json.type = val === null ? (\"null\" as const) : (typeof val as any);\n    if (ctx.target === \"draft-04\" || ctx.target === \"openapi-3.0\") {\n      json.enum = [val];\n    } else {\n      json.const = val;\n    }\n  } else {\n    if (vals.every((v) => typeof v === \"number\")) json.type = \"number\";\n    if (vals.every((v) => typeof v === \"string\")) json.type = \"string\";\n    if (vals.every((v) => typeof v === \"boolean\")) json.type = \"boolean\";\n    if (vals.every((v) => v === null)) json.type = \"null\";\n    json.enum = vals;\n  }\n};\n\nexport const nanProcessor: Processor<schemas.$ZodNaN> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"NaN cannot be represented in JSON Schema\");\n  }\n};\n\nexport const templateLiteralProcessor: Processor<schemas.$ZodTemplateLiteral> = (schema, _ctx, json, _params) => {\n  const _json = json as JSONSchema.StringSchema;\n  const pattern = schema._zod.pattern;\n  if (!pattern) throw new Error(\"Pattern not found in template literal\");\n  _json.type = \"string\";\n  _json.pattern = pattern.source;\n};\n\nexport const fileProcessor: Processor<schemas.$ZodFile> = (schema, _ctx, json, _params) => {\n  const _json = json as JSONSchema.StringSchema;\n  const file: JSONSchema.StringSchema = {\n    type: \"string\",\n    format: \"binary\",\n    contentEncoding: \"binary\",\n  };","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/colinhacks/zod/blob/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/core/json-schema-processors.ts#L186-L222","documentation":"Thrown by toJSONSchema() when a ZodNaN schema (z.nan()) is encountered with unrepresentable 'throw' (default). NaN is not a valid JSON number (JSON.parse would never produce it), so JSON Schema cannot represent it.","triggerScenarios":"Calling toJSONSchema() on a schema that includes z.nan(), e.g. as a sentinel in a union. Exporting a model that intentionally admits NaN.","commonSituations":"Numerical computation schemas that flag NaN results, then exporting the schema for documentation or external validation.","solutions":["Pass { unrepresentable: 'any' } to toJSONSchema() so the NaN branch becomes {}.","Remove z.nan() from the exported schema; model NaN handling at runtime only.","If the external contract needs a sentinel, use an explicit string literal ('NaN') and translate at the boundary."],"exampleFix":"// before (throws)\nconst Schema = z.union([z.number(), z.nan()]);\nz.toJSONSchema(Schema);\n\n// after\nconst Schema = z.union([z.number(), z.nan()]);\nz.toJSONSchema(Schema, { unrepresentable: 'any' });\n// NaN branch collapses to {}; union becomes anyOf [ {type:'number'}, {} ]","handlingStrategy":"try-catch","validationCode":"const opts = schemaContains(schema, (s) => s._zod.def.type === 'nan')\n  ? { unrepresentable: 'any' }\n  : {};\nconst json = z.toJSONSchema(schema, opts);","typeGuard":"function hasNaN(schema) {\n  return schema._zod.def.type === 'nan';\n}","tryCatchPattern":"try {\n  return z.toJSONSchema(schema);\n} catch (e) {\n  if (e.message === 'NaN cannot be represented in JSON Schema') {\n    return z.toJSONSchema(schema, { unrepresentable: 'any' });\n  }\n  throw e;\n}","preventionTips":["Do not include z.nan() in exported schemas; handle NaN purely at runtime.","Use a string sentinel ('NaN') if the external contract needs one.","Pass { unrepresentable: 'any' } when exporting schemas with numeric unions that include NaN."],"tags":["json-schema","nan","unrepresentable","to-json-schema"],"backgroundTag":null,"analyzedSha":"2d90846af918af9602e088812d63a035d47cdbe4","analyzedAt":"2026-08-11T01:21:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}