object.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseObjectDef = void 0;
  4. const parseDef_1 = require("../parseDef.js");
  5. function decideAdditionalProperties(def, refs) {
  6. if (refs.removeAdditionalStrategy === 'strict') {
  7. return def.catchall._def.typeName === 'ZodNever' ?
  8. def.unknownKeys !== 'strict'
  9. : (0, parseDef_1.parseDef)(def.catchall._def, {
  10. ...refs,
  11. currentPath: [...refs.currentPath, 'additionalProperties'],
  12. }) ?? true;
  13. }
  14. else {
  15. return def.catchall._def.typeName === 'ZodNever' ?
  16. def.unknownKeys === 'passthrough'
  17. : (0, parseDef_1.parseDef)(def.catchall._def, {
  18. ...refs,
  19. currentPath: [...refs.currentPath, 'additionalProperties'],
  20. }) ?? true;
  21. }
  22. }
  23. function parseObjectDef(def, refs) {
  24. const result = {
  25. type: 'object',
  26. ...Object.entries(def.shape()).reduce((acc, [propName, propDef]) => {
  27. if (propDef === undefined || propDef._def === undefined)
  28. return acc;
  29. const propertyPath = [...refs.currentPath, 'properties', propName];
  30. const parsedDef = (0, parseDef_1.parseDef)(propDef._def, {
  31. ...refs,
  32. currentPath: propertyPath,
  33. propertyPath,
  34. });
  35. if (parsedDef === undefined)
  36. return acc;
  37. if (refs.openaiStrictMode && propDef.isOptional() && !propDef.isNullable()) {
  38. console.warn(`Zod field at \`${propertyPath.join('/')}\` uses \`.optional()\` without \`.nullable()\` which is not supported by the API. See: https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses#all-fields-must-be-required\nThis will become an error in a future version of the SDK.`);
  39. }
  40. return {
  41. properties: {
  42. ...acc.properties,
  43. [propName]: parsedDef,
  44. },
  45. required: propDef.isOptional() && !refs.openaiStrictMode ? acc.required : [...acc.required, propName],
  46. };
  47. }, { properties: {}, required: [] }),
  48. additionalProperties: decideAdditionalProperties(def, refs),
  49. };
  50. if (!result.required.length)
  51. delete result.required;
  52. return result;
  53. }
  54. exports.parseObjectDef = parseObjectDef;
  55. //# sourceMappingURL=object.js.map