@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 3.34 kB
Source Map (JSON)
{"version":3,"file":"intersection.cjs","names":["parseDef"],"sources":["../../../../src/utils/zod-to-json-schema/parsers/intersection.ts"],"sourcesContent":["import { ZodIntersectionDef } from \"zod/v3\";\nimport { parseDef } from \"../parseDef.js\";\nimport { JsonSchema7Type } from \"../parseTypes.js\";\nimport { Refs } from \"../Refs.js\";\nimport { JsonSchema7StringType } from \"./string.js\";\n\nexport type JsonSchema7AllOfType = {\n allOf: JsonSchema7Type[];\n unevaluatedProperties?: boolean;\n};\n\nconst isJsonSchema7AllOfType = (\n type: JsonSchema7Type | JsonSchema7StringType\n): type is JsonSchema7AllOfType => {\n if (\"type\" in type && type.type === \"string\") return false;\n return \"allOf\" in type;\n};\n\nexport function parseIntersectionDef(\n def: ZodIntersectionDef,\n refs: Refs\n): JsonSchema7AllOfType | JsonSchema7Type | undefined {\n const allOf = [\n parseDef(def.left._def, {\n ...refs,\n currentPath: [...refs.currentPath, \"allOf\", \"0\"],\n }),\n parseDef(def.right._def, {\n ...refs,\n currentPath: [...refs.currentPath, \"allOf\", \"1\"],\n }),\n ].filter((x): x is JsonSchema7Type => !!x);\n\n let unevaluatedProperties:\n | Pick<JsonSchema7AllOfType, \"unevaluatedProperties\">\n | undefined =\n refs.target === \"jsonSchema2019-09\"\n ? { unevaluatedProperties: false }\n : undefined;\n\n const mergedAllOf: JsonSchema7Type[] = [];\n // If either of the schemas is an allOf, merge them into a single allOf\n allOf.forEach((schema) => {\n if (isJsonSchema7AllOfType(schema)) {\n mergedAllOf.push(...schema.allOf);\n if (schema.unevaluatedProperties === undefined) {\n // If one of the schemas has no unevaluatedProperties set,\n // the merged schema should also have no unevaluatedProperties set\n unevaluatedProperties = undefined;\n }\n } else {\n let nestedSchema: JsonSchema7Type = schema;\n if (\n \"additionalProperties\" in schema &&\n schema.additionalProperties === false\n ) {\n const { additionalProperties, ...rest } = schema;\n nestedSchema = rest;\n } else {\n // As soon as one of the schemas has additionalProperties set not to false, we allow unevaluatedProperties\n unevaluatedProperties = undefined;\n }\n mergedAllOf.push(nestedSchema);\n }\n });\n return mergedAllOf.length\n ? {\n allOf: mergedAllOf,\n ...unevaluatedProperties,\n }\n : undefined;\n}\n"],"mappings":";;AAWA,MAAM,0BACJ,SACiC;CACjC,IAAI,UAAU,QAAQ,KAAK,SAAS,UAAU,OAAO;CACrD,OAAO,WAAW;AACpB;AAEA,SAAgB,qBACd,KACA,MACoD;CACpD,MAAM,QAAQ,CACZA,iBAAAA,SAAS,IAAI,KAAK,MAAM;EACtB,GAAG;EACH,aAAa;GAAC,GAAG,KAAK;GAAa;GAAS;EAAG;CACjD,CAAC,GACDA,iBAAAA,SAAS,IAAI,MAAM,MAAM;EACvB,GAAG;EACH,aAAa;GAAC,GAAG,KAAK;GAAa;GAAS;EAAG;CACjD,CAAC,CACH,CAAC,CAAC,QAAQ,MAA4B,CAAC,CAAC,CAAC;CAEzC,IAAI,wBAGF,KAAK,WAAW,sBACZ,EAAE,uBAAuB,MAAM,IAC/B,KAAA;CAEN,MAAM,cAAiC,CAAC;CAExC,MAAM,SAAS,WAAW;EACxB,IAAI,uBAAuB,MAAM,GAAG;GAClC,YAAY,KAAK,GAAG,OAAO,KAAK;GAChC,IAAI,OAAO,0BAA0B,KAAA,GAGnC,wBAAwB,KAAA;EAE5B,OAAO;GACL,IAAI,eAAgC;GACpC,IACE,0BAA0B,UAC1B,OAAO,yBAAyB,OAChC;IACA,MAAM,EAAE,sBAAsB,GAAG,SAAS;IAC1C,eAAe;GACjB,OAEE,wBAAwB,KAAA;GAE1B,YAAY,KAAK,YAAY;EAC/B;CACF,CAAC;CACD,OAAO,YAAY,SACf;EACE,OAAO;EACP,GAAG;CACL,IACA,KAAA;AACN"}