{"id":"ad2b3e4dfecac2a1","repo":"colinhacks/zod","slug":"set-cannot-be-represented-in-json-schema","errorCode":null,"errorMessage":"Set cannot be represented in JSON Schema","messagePattern":"Set 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":270,"sourceCode":"    throw new Error(\"Function types cannot be represented in JSON Schema\");\n  }\n};\n\nexport const transformProcessor: Processor<schemas.$ZodTransform> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"Transforms cannot be represented in JSON Schema\");\n  }\n};\n\nexport const mapProcessor: Processor<schemas.$ZodMap> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"Map cannot be represented in JSON Schema\");\n  }\n};\n\nexport const setProcessor: Processor<schemas.$ZodSet> = (_schema, ctx, _json, _params) => {\n  if (ctx.unrepresentable === \"throw\") {\n    throw new Error(\"Set cannot be represented in JSON Schema\");\n  }\n};\n\n// ==================== COMPOSITE TYPE PROCESSORS ====================\n\nexport const arrayProcessor: Processor<schemas.$ZodArray> = (schema, ctx, _json, params) => {\n  const json = _json as JSONSchema.ArraySchema;\n  const def = schema._zod.def as schemas.$ZodArrayDef;\n  const { minimum, maximum } = schema._zod.bag;\n  if (typeof minimum === \"number\") json.minItems = minimum;\n  if (typeof maximum === \"number\") json.maxItems = maximum;\n\n  json.type = \"array\";\n  json.items = process(def.element, ctx as any, {\n    ...params,\n    path: [...params.path, \"items\"],\n  });\n};","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/core/json-schema-processors.ts#L252-L288","documentation":"JSON Schema arrays are ordered and can carry duplicates, so a `Set` (unique, unordered) has no faithful representation; `setProcessor` (json-schema-processors.ts:268) throws for `z.set(valueType)` when `ctx.unrepresentable === \"throw\"` (default).","triggerScenarios":"`z.toJSONSchema()` over a schema containing `z.set(z.string())`, with default options.","commonSituations":"Documenting a schema that models unique collections as JS Sets; converting a shared schema that uses Set for deduplication.","solutions":["Pass `{ unrepresentable: \"any\" }` to emit an empty schema for the set node.","Re-model as `z.array(valueType)` (JSON Schema array); enforce uniqueness at the application layer.","If uniqueness must be visible in the contract, use `z.array(valueType)` plus a `uniqueItems: true` via an override."],"exampleFix":"// before\nz.toJSONSchema(z.set(z.string())); // throws\n// after\nz.toJSONSchema(z.array(z.string())); // -> { type: \"array\", items: { type: \"string\" } }","handlingStrategy":"fallback","validationCode":"// Model unique collections as arrays in contract schemas.\nconst contractSchema = z.array(z.string());\nconst json = z.toJSONSchema(contractSchema);","typeGuard":"function usesSet(schema: z.ZodType): boolean {\n  return schema._zod.traits.has(\"$ZodSet\");\n}","tryCatchPattern":"try {\n  return z.toJSONSchema(schema);\n} catch (e) {\n  if (e instanceof Error && /cannot be represented in JSON Schema/.test(e.message)) {\n    return z.toJSONSchema(schema, { unrepresentable: \"any\" });\n  }\n  throw e;\n}","preventionTips":["Use `z.array(valueType)` for contract schemas; enforce uniqueness in application logic.","Apply `uniqueItems: true` via an override if uniqueness must appear in the contract.","Reserve `z.set()` for runtime parsing schemas."],"tags":["json-schema","set","array","unrepresentable","openapi"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}