@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 3.9 kB
Source Map (JSON)
{"version":3,"file":"object.cjs","names":["parseDef"],"sources":["../../../../src/utils/zod-to-json-schema/parsers/object.ts"],"sourcesContent":["import { ZodObjectDef, ZodTypeAny } from \"zod/v3\";\nimport { parseDef } from \"../parseDef.js\";\nimport { JsonSchema7Type } from \"../parseTypes.js\";\nimport { Refs } from \"../Refs.js\";\n\nexport type JsonSchema7ObjectType = {\n type: \"object\";\n properties: Record<string, JsonSchema7Type>;\n additionalProperties?: boolean | JsonSchema7Type;\n required?: string[];\n};\n\nexport function parseObjectDef(def: ZodObjectDef, refs: Refs) {\n const forceOptionalIntoNullable = refs.target === \"openAi\";\n\n const result: JsonSchema7ObjectType = {\n type: \"object\",\n properties: {},\n };\n\n const required: string[] = [];\n\n const shape = def.shape();\n\n for (const propName in shape) {\n let propDef = shape[propName];\n\n if (propDef === undefined || propDef._def === undefined) {\n continue;\n }\n\n let propOptional = safeIsOptional(propDef);\n\n if (propOptional && forceOptionalIntoNullable) {\n if (propDef._def.typeName === \"ZodOptional\") {\n propDef = propDef._def.innerType;\n }\n\n if (!propDef.isNullable()) {\n propDef = propDef.nullable();\n }\n\n propOptional = false;\n }\n\n const parsedDef = parseDef(propDef._def, {\n ...refs,\n currentPath: [...refs.currentPath, \"properties\", propName],\n propertyPath: [...refs.currentPath, \"properties\", propName],\n });\n\n if (parsedDef === undefined) {\n continue;\n }\n\n result.properties[propName] = parsedDef;\n\n if (!propOptional) {\n required.push(propName);\n }\n }\n\n if (required.length) {\n result.required = required;\n }\n\n const additionalProperties = decideAdditionalProperties(def, refs);\n\n if (additionalProperties !== undefined) {\n result.additionalProperties = additionalProperties;\n }\n\n return result;\n}\n\nfunction decideAdditionalProperties(def: ZodObjectDef, refs: Refs) {\n if (def.catchall._def.typeName !== \"ZodNever\") {\n return parseDef(def.catchall._def, {\n ...refs,\n currentPath: [...refs.currentPath, \"additionalProperties\"],\n });\n }\n\n switch (def.unknownKeys) {\n case \"passthrough\":\n return refs.allowedAdditionalProperties;\n case \"strict\":\n return refs.rejectedAdditionalProperties;\n case \"strip\":\n return refs.removeAdditionalStrategy === \"strict\"\n ? refs.allowedAdditionalProperties\n : refs.rejectedAdditionalProperties;\n }\n}\n\nfunction safeIsOptional(schema: ZodTypeAny): boolean {\n try {\n return schema.isOptional();\n } catch {\n return true;\n }\n}\n"],"mappings":";;AAYA,SAAgB,eAAe,KAAmB,MAAY;CAC5D,MAAM,4BAA4B,KAAK,WAAW;CAElD,MAAM,SAAgC;EACpC,MAAM;EACN,YAAY,CAAC;CACf;CAEA,MAAM,WAAqB,CAAC;CAE5B,MAAM,QAAQ,IAAI,MAAM;CAExB,KAAK,MAAM,YAAY,OAAO;EAC5B,IAAI,UAAU,MAAM;EAEpB,IAAI,YAAY,KAAA,KAAa,QAAQ,SAAS,KAAA,GAC5C;EAGF,IAAI,eAAe,eAAe,OAAO;EAEzC,IAAI,gBAAgB,2BAA2B;GAC7C,IAAI,QAAQ,KAAK,aAAa,eAC5B,UAAU,QAAQ,KAAK;GAGzB,IAAI,CAAC,QAAQ,WAAW,GACtB,UAAU,QAAQ,SAAS;GAG7B,eAAe;EACjB;EAEA,MAAM,YAAYA,iBAAAA,SAAS,QAAQ,MAAM;GACvC,GAAG;GACH,aAAa;IAAC,GAAG,KAAK;IAAa;IAAc;GAAQ;GACzD,cAAc;IAAC,GAAG,KAAK;IAAa;IAAc;GAAQ;EAC5D,CAAC;EAED,IAAI,cAAc,KAAA,GAChB;EAGF,OAAO,WAAW,YAAY;EAE9B,IAAI,CAAC,cACH,SAAS,KAAK,QAAQ;CAE1B;CAEA,IAAI,SAAS,QACX,OAAO,WAAW;CAGpB,MAAM,uBAAuB,2BAA2B,KAAK,IAAI;CAEjE,IAAI,yBAAyB,KAAA,GAC3B,OAAO,uBAAuB;CAGhC,OAAO;AACT;AAEA,SAAS,2BAA2B,KAAmB,MAAY;CACjE,IAAI,IAAI,SAAS,KAAK,aAAa,YAC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