openai-zod-to-json-schema
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -266,2 +266,8 @@ import { ZodArrayDef, ZodBigIntDef, ZodNumberDef, ZodDateDef, ZodEnumDef, ZodIntersectionDef, ZodLiteralDef, ZodStringDef, ZodRecordDef, ZodTypeAny, ZodMapDef, ZodNativeEnumDef, ZodNullableDef, ZodObjectDef, ZodSetDef, ZodTupleDef, ZodTupleItems, ZodUnionDef, ZodDiscriminatedUnionDef, ZodTypeDef, ZodSchema, ZodBrandedDef, ZodCatchDef, ZodDefaultDef, ZodEffectsDef, ZodOptionalDef, ZodPipelineDef, ZodPromiseDef, ZodReadonlyDef } from 'zod'; | ||
seen: Map<ZodTypeDef, Seen>; | ||
/** | ||
* Set of all the `$ref`s we created, e.g. `Set(['#/$defs/ui'])` | ||
* this notable does not include any `definitions` that were | ||
* explicitly given as an option. | ||
*/ | ||
seenRefs: Set<string>; | ||
currentPath: string[]; | ||
@@ -282,3 +288,3 @@ propertyPath: string[] | undefined; | ||
name: string | undefined; | ||
$refStrategy: 'root' | 'relative' | 'none' | 'seen'; | ||
$refStrategy: 'root' | 'relative' | 'none' | 'seen' | 'extract-to-root'; | ||
basePath: string[]; | ||
@@ -290,6 +296,7 @@ effectStrategy: 'input' | 'any'; | ||
removeAdditionalStrategy: 'passthrough' | 'strict'; | ||
nullableStrategy: 'from-target' | 'property'; | ||
target: Target; | ||
strictUnions: boolean; | ||
definitionPath: string; | ||
definitions: Record<string, ZodSchema>; | ||
definitions: Record<string, ZodSchema | ZodTypeDef>; | ||
errorMessages: boolean; | ||
@@ -301,7 +308,6 @@ markdownDescription: boolean; | ||
base64Strategy: 'format:binary' | 'contentEncoding:base64' | 'pattern:zod'; | ||
nameStrategy: 'ref' | 'title'; | ||
nameStrategy: 'ref' | 'duplicate-ref' | 'title'; | ||
override?: (def: ZodTypeDef, refs: Refs, seen: Seen | undefined, forceResolution?: boolean) => JsonSchema7Type | undefined | typeof ignoreOverride; | ||
openaiStrictMode?: boolean; | ||
}; | ||
declare const defaultOptions: Options; | ||
declare const getDefaultOptions: <Target extends Targets>(options: Partial<Options<Target>> | string | undefined) => Options<Target>; | ||
@@ -334,2 +340,2 @@ | ||
export { type DateStrategy, type ErrorMessages, type JsonSchema7AllOfType, type JsonSchema7AnyType, type JsonSchema7ArrayType, type JsonSchema7BigintType, type JsonSchema7BooleanType, type JsonSchema7DateType, type JsonSchema7EnumType, type JsonSchema7LiteralType, type JsonSchema7MapType, type JsonSchema7NativeEnumType, type JsonSchema7NeverType, type JsonSchema7NullType, type JsonSchema7NullableType, type JsonSchema7NumberType, type JsonSchema7ObjectType, type JsonSchema7RecordType, type JsonSchema7SetType, type JsonSchema7StringType, type JsonSchema7TupleType, type JsonSchema7Type, type JsonSchema7TypeUnion, type JsonSchema7UndefinedType, type JsonSchema7UnionType, type JsonSchema7UnknownType, type Options, type Refs, type Seen, type Targets, addErrorMessage, defaultOptions, getDefaultOptions, getRefs, ignoreOverride, parseAnyDef, parseArrayDef, parseBigintDef, parseBooleanDef, parseBrandedDef, parseCatchDef, parseDateDef, parseDef, parseDefaultDef, parseEffectsDef, parseEnumDef, parseIntersectionDef, parseLiteralDef, parseMapDef, parseNativeEnumDef, parseNeverDef, parseNullDef, parseNullableDef, parseNumberDef, parseObjectDef, parseOptionalDef, parsePipelineDef, parsePromiseDef, parseReadonlyDef, parseRecordDef, parseSetDef, parseStringDef, parseTupleDef, parseUndefinedDef, parseUnionDef, parseUnknownDef, primitiveMappings, setResponseValueAndErrors, zodPatterns, zodToJsonSchema }; | ||
export { type DateStrategy, type ErrorMessages, type JsonSchema7AllOfType, type JsonSchema7AnyType, type JsonSchema7ArrayType, type JsonSchema7BigintType, type JsonSchema7BooleanType, type JsonSchema7DateType, type JsonSchema7EnumType, type JsonSchema7LiteralType, type JsonSchema7MapType, type JsonSchema7NativeEnumType, type JsonSchema7NeverType, type JsonSchema7NullType, type JsonSchema7NullableType, type JsonSchema7NumberType, type JsonSchema7ObjectType, type JsonSchema7RecordType, type JsonSchema7SetType, type JsonSchema7StringType, type JsonSchema7TupleType, type JsonSchema7Type, type JsonSchema7TypeUnion, type JsonSchema7UndefinedType, type JsonSchema7UnionType, type JsonSchema7UnknownType, type Options, type Refs, type Seen, type Targets, addErrorMessage, getDefaultOptions, getRefs, ignoreOverride, parseAnyDef, parseArrayDef, parseBigintDef, parseBooleanDef, parseBrandedDef, parseCatchDef, parseDateDef, parseDef, parseDefaultDef, parseEffectsDef, parseEnumDef, parseIntersectionDef, parseLiteralDef, parseMapDef, parseNativeEnumDef, parseNeverDef, parseNullDef, parseNullableDef, parseNumberDef, parseObjectDef, parseOptionalDef, parsePipelineDef, parsePromiseDef, parseReadonlyDef, parseRecordDef, parseSetDef, parseStringDef, parseTupleDef, parseUndefinedDef, parseUnionDef, parseUnknownDef, primitiveMappings, setResponseValueAndErrors, zodPatterns, zodToJsonSchema }; |
@@ -6,3 +6,2 @@ // src/vendor/zod-to-json-schema/Options.ts | ||
$refStrategy: "root", | ||
basePath: ["#"], | ||
effectStrategy: "input", | ||
@@ -12,2 +11,3 @@ pipeStrategy: "all", | ||
mapStrategy: "entries", | ||
nullableStrategy: "from-target", | ||
removeAdditionalStrategy: "passthrough", | ||
@@ -17,3 +17,2 @@ definitionPath: "definitions", | ||
strictUnions: false, | ||
definitions: {}, | ||
errorMessages: false, | ||
@@ -27,10 +26,26 @@ markdownDescription: false, | ||
}; | ||
var getDefaultOptions = (options) => typeof options === "string" ? { | ||
...defaultOptions, | ||
name: options | ||
} : { | ||
...defaultOptions, | ||
...options | ||
var getDefaultOptions = (options) => { | ||
return typeof options === "string" ? { | ||
...defaultOptions, | ||
basePath: ["#"], | ||
definitions: {}, | ||
name: options | ||
} : { | ||
...defaultOptions, | ||
basePath: ["#"], | ||
definitions: {}, | ||
...options | ||
}; | ||
}; | ||
// src/vendor/zod-to-json-schema/util.ts | ||
var zodDef = (zodSchema) => { | ||
return "_def" in zodSchema ? zodSchema._def : zodSchema; | ||
}; | ||
function isEmptyObj(obj) { | ||
if (!obj) return true; | ||
for (const _k in obj) return false; | ||
return true; | ||
} | ||
// src/vendor/zod-to-json-schema/Refs.ts | ||
@@ -44,7 +59,8 @@ var getRefs = (options) => { | ||
propertyPath: void 0, | ||
seenRefs: /* @__PURE__ */ new Set(), | ||
seen: new Map( | ||
Object.entries(_options.definitions).map(([name, def]) => [ | ||
def._def, | ||
zodDef(def), | ||
{ | ||
def: def._def, | ||
def: zodDef(def), | ||
path: [..._options.basePath, _options.definitionPath, name], | ||
@@ -818,3 +834,3 @@ // Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now. | ||
if (["ZodString", "ZodNumber", "ZodBigInt", "ZodBoolean", "ZodNull"].includes(def.innerType._def.typeName) && (!def.innerType._def.checks || !def.innerType._def.checks.length)) { | ||
if (refs.target === "openApi3") { | ||
if (refs.target === "openApi3" || refs.nullableStrategy === "property") { | ||
return { | ||
@@ -1059,2 +1075,5 @@ type: primitiveMappings[def.innerType._def.typeName], | ||
if (seenSchema !== void 0) { | ||
if ("$ref" in seenSchema) { | ||
refs.seenRefs.add(seenSchema.$ref); | ||
} | ||
return seenSchema; | ||
@@ -1076,2 +1095,8 @@ } | ||
return { $ref: item.path.join("/") }; | ||
case "extract-to-root": | ||
const name = item.path.slice(refs.basePath.length + 1).join("_"); | ||
if (name !== refs.name && refs.nameStrategy === "duplicate-ref") { | ||
refs.definitions[name] = item.def; | ||
} | ||
return { $ref: [...refs.basePath, refs.definitionPath, name].join("/") }; | ||
case "relative": | ||
@@ -1183,16 +1208,2 @@ return { $ref: getRelativePath(refs.currentPath, item.path) }; | ||
const refs = getRefs(options); | ||
const definitions = typeof options === "object" && options.definitions ? Object.entries(options.definitions).reduce( | ||
(acc, [name2, schema2]) => ({ | ||
...acc, | ||
[name2]: parseDef( | ||
schema2._def, | ||
{ | ||
...refs, | ||
currentPath: [...refs.basePath, refs.definitionPath, name2] | ||
}, | ||
true | ||
) ?? {} | ||
}), | ||
{} | ||
) : void 0; | ||
const name = typeof options === "string" ? options : options?.nameStrategy === "title" ? void 0 : options?.name; | ||
@@ -1211,6 +1222,38 @@ const main = parseDef( | ||
} | ||
const definitions = (() => { | ||
if (isEmptyObj(refs.definitions)) { | ||
return void 0; | ||
} | ||
const definitions2 = {}; | ||
const processedDefinitions = /* @__PURE__ */ new Set(); | ||
for (let i = 0; i < 500; i++) { | ||
const newDefinitions = Object.entries(refs.definitions).filter( | ||
([key]) => !processedDefinitions.has(key) | ||
); | ||
if (newDefinitions.length === 0) break; | ||
for (const [key, schema2] of newDefinitions) { | ||
definitions2[key] = parseDef( | ||
zodDef(schema2), | ||
{ ...refs, currentPath: [...refs.basePath, refs.definitionPath, key] }, | ||
true | ||
) ?? {}; | ||
processedDefinitions.add(key); | ||
} | ||
} | ||
return definitions2; | ||
})(); | ||
const combined = name === void 0 ? definitions ? { | ||
...main, | ||
[refs.definitionPath]: definitions | ||
} : main : { | ||
} : main : refs.nameStrategy === "duplicate-ref" ? { | ||
...main, | ||
...definitions || refs.seenRefs.size ? { | ||
[refs.definitionPath]: { | ||
...definitions, | ||
// only actually duplicate the schema definition if it was ever referenced | ||
// otherwise the duplication is completely pointless | ||
...refs.seenRefs.size ? { [name]: main } : void 0 | ||
} | ||
} : void 0 | ||
} : { | ||
$ref: [...refs.$refStrategy === "relative" ? [] : refs.basePath, refs.definitionPath, name].join( | ||
@@ -1233,3 +1276,2 @@ "/" | ||
addErrorMessage, | ||
defaultOptions, | ||
getDefaultOptions, | ||
@@ -1236,0 +1278,0 @@ getRefs, |
{ | ||
"name": "openai-zod-to-json-schema", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Convert Zod schemas to JSON schemas which are optionally compatible with OpenAI's structured outputs.", | ||
@@ -32,3 +32,3 @@ "author": "Travis Fischer <travis@transitivebullsh.it>", | ||
"@types/json-schema": "^7.0.15", | ||
"@types/node": "^22.1.0", | ||
"@types/node": "^22.3.0", | ||
"ajv": "^8.17.1", | ||
@@ -40,7 +40,7 @@ "ajv-formats": "^3.0.1", | ||
"json-schema": "^0.4.0", | ||
"lint-staged": "^15.2.8", | ||
"lint-staged": "^15.2.9", | ||
"local-ref-resolver": "^0.2.0", | ||
"npm-run-all2": "^6.2.2", | ||
"only-allow": "^1.2.1", | ||
"openai": "^4.55.0", | ||
"openai": "^4.55.7", | ||
"prettier": "^3.3.3", | ||
@@ -47,0 +47,0 @@ "tsup": "^8.2.4", |
@@ -20,7 +20,7 @@ # openai-zod-to-json-schema <!-- omit from toc --> | ||
This package exports OpenAI's [vendored version of zod-to-json-schema](https://github.com/openai/openai-node/tree/master/src/_vendor/zod-to-json-schema). | ||
This package exports OpenAI's [vendored version of zod-to-json-schema](https://github.com/openai/openai-node/tree/master/src/_vendor/zod-to-json-schema) as a standalone module (the source code is copied directly to guarantee a 1:1 match). | ||
It re-adds all of the unit tests from the original [zod-to-json-schema](https://github.com/StefanTerdell/zod-to-json-schema) by [Stefan Terdell](https://github.com/StefanTerdell). | ||
It also adds some additional unit tests for OpenAI's `strict` mode. See [OpenAI's docs on structured outputs](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas) for more details on the subset of JSON Schemas that is supported by OpenAI's structured outputs. | ||
It also adds some additional unit tests for OpenAI's `strict` mode. See [OpenAI's docs on structured outputs](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas) for more details on the subset of JSON Schemas that are supported by OpenAI's structured outputs. | ||
@@ -58,1 +58,3 @@ This package will be kept in sync with any changes to OpenAI's vendored version. | ||
MIT © [Travis Fischer](https://x.com/transitive_bs) | ||
Also see the original [zod-to-json-schema license](https://github.com/StefanTerdell/zod-to-json-schema). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
294610
2907
59