| 288 | }; |
| 289 | |
| 290 | export const objectProcessor: Processor<schemas.$ZodObject> = (schema, ctx, _json, params) => { |
| 291 | const json = _json as JSONSchema.ObjectSchema; |
| 292 | const def = schema._zod.def as schemas.$ZodObjectDef; |
| 293 | json.type = "object"; |
| 294 | json.properties = {}; |
| 295 | const shape = def.shape; |
| 296 | |
| 297 | for (const key in shape) { |
| 298 | json.properties[key] = process(shape[key]!, ctx as any, { |
| 299 | ...params, |
| 300 | path: [...params.path, "properties", key], |
| 301 | }); |
| 302 | } |
| 303 | |
| 304 | // required keys |
| 305 | const allKeys = new Set(Object.keys(shape)); |
| 306 | const requiredKeys = new Set( |
| 307 | [...allKeys].filter((key) => { |
| 308 | const v = def.shape[key]!._zod; |
| 309 | if (ctx.io === "input") { |
| 310 | return v.optin === undefined; |
| 311 | } else { |
| 312 | return v.optout === undefined; |
| 313 | } |
| 314 | }) |
| 315 | ); |
| 316 | |
| 317 | if (requiredKeys.size > 0) { |
| 318 | json.required = Array.from(requiredKeys); |
| 319 | } |
| 320 | |
| 321 | // catchall |
| 322 | if (def.catchall?._zod.def.type === "never") { |
| 323 | // strict |
| 324 | json.additionalProperties = false; |
| 325 | } else if (!def.catchall) { |
| 326 | // regular |
| 327 | if (ctx.io === "output") json.additionalProperties = false; |
| 328 | } else if (def.catchall) { |
| 329 | json.additionalProperties = process(def.catchall, ctx as any, { |
| 330 | ...params, |
| 331 | path: [...params.path, "additionalProperties"], |
| 332 | }); |
| 333 | } |
| 334 | }; |
| 335 | |
| 336 | export const unionProcessor: Processor<schemas.$ZodUnion> = (schema, ctx, json, params) => { |
| 337 | const def = schema._zod.def as schemas.$ZodUnionDef; |