{"record":{"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/2d90846af918af9602e088812d63a035d47cdbe4/packages/zod/src/v4/core/json-schema-processors.ts#L252-L288","documentation":"Thrown by toJSONSchema() when a ZodSet schema (z.set(elementSchema)) is encountered with unrepresentable 'throw' (default). JSON has no Set type — arrays do not enforce uniqueness — so a typed Set cannot be faithfully expressed in JSON Schema.","triggerScenarios":"Calling toJSONSchema() on z.set(z.string()) or a schema that embeds a set field. Documenting an API whose runtime model uses Set for deduplication.","commonSituations":"Tag/collection fields modelled as Set at runtime, then exported to JSON Schema where the consumer expects an array.","solutions":["Pass { unrepresentable: 'any' } to toJSONSchema() so the set field becomes {}.","Model the external contract as z.array(elementSchema) (optionally with a uniqueness note/refine) and convert to/from Set at the boundary.","If uniqueness must be conveyed, use a custom override() to attach a vendor extension (e.g. x-unique-items: true)."],"exampleFix":"// before (throws)\nconst Schema = z.set(z.string());\nz.toJSONSchema(Schema);\n\n// after (array for the JSON contract)\nconst Schema = z.array(z.string());\nz.toJSONSchema(Schema);\n// { type: 'array', items: { type: 'string' } }","handlingStrategy":"try-catch","validationCode":"const opts = schemaContains(schema, (s) => s._zod.def.type === 'set')\n  ? { unrepresentable: 'any' }\n  : {};\nconst json = z.toJSONSchema(schema, opts);","typeGuard":"function hasSet(schema) {\n  return schema._zod.def.type === 'set';\n}","tryCatchPattern":"try {\n  return z.toJSONSchema(schema);\n} catch (e) {\n  if (e.message === 'Set cannot be represented in JSON Schema') {\n    return z.toJSONSchema(schema, { unrepresentable: 'any' });\n  }\n  throw e;\n}","preventionTips":["Model external contracts with z.array(elementSchema) instead of z.set().","Convert Set <-> Array at the boundary; keep z.set() for runtime-only schemas.","Use an override() vendor extension (x-unique-items) if uniqueness must be conveyed to consumers."],"tags":["json-schema","set","array","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"}