gitea push
This commit is contained in:
+31
-16
@@ -9,7 +9,7 @@ const z = {
|
||||
iso: _iso,
|
||||
};
|
||||
// Keys that are recognized and handled by the conversion logic
|
||||
const RECOGNIZED_KEYS = new Set([
|
||||
const RECOGNIZED_KEYS = /*@__PURE__*/ new Set([
|
||||
// Schema identification
|
||||
"$schema",
|
||||
"$ref",
|
||||
@@ -485,13 +485,6 @@ function convertBaseSchema(schema, ctx) {
|
||||
default:
|
||||
throw new Error(`Unsupported type: ${type}`);
|
||||
}
|
||||
// Apply metadata
|
||||
if (schema.description) {
|
||||
zodSchema = zodSchema.describe(schema.description);
|
||||
}
|
||||
if (schema.default !== undefined) {
|
||||
zodSchema = zodSchema.default(schema.default);
|
||||
}
|
||||
return zodSchema;
|
||||
}
|
||||
function convertSchema(schema, ctx) {
|
||||
@@ -536,23 +529,28 @@ function convertSchema(schema, ctx) {
|
||||
if (schema.readOnly === true) {
|
||||
baseSchema = z.readonly(baseSchema);
|
||||
}
|
||||
// Collect metadata: core schema keywords and unrecognized keys
|
||||
// Apply `default` so it wraps the fully-composed schema. This ensures
|
||||
// `parse(undefined) -> default` works regardless of which branch of
|
||||
// `convertBaseSchema` produced the inner schema (enum/const/not/typed/etc.).
|
||||
if (schema.default !== undefined) {
|
||||
baseSchema = baseSchema.default(schema.default);
|
||||
}
|
||||
// Collect non-description annotation metadata into the user-supplied
|
||||
// registry. Description is handled separately below via `.describe()` to
|
||||
// preserve the contract that `schema.description` reads from globalRegistry.
|
||||
const extraMeta = {};
|
||||
// Core schema keywords that should be captured as metadata
|
||||
const coreMetadataKeys = ["$id", "id", "$comment", "$anchor", "$vocabulary", "$dynamicRef", "$dynamicAnchor"];
|
||||
for (const key of coreMetadataKeys) {
|
||||
if (key in schema) {
|
||||
extraMeta[key] = schema[key];
|
||||
}
|
||||
}
|
||||
// Content keywords - store as metadata
|
||||
const contentMetadataKeys = ["contentEncoding", "contentMediaType", "contentSchema"];
|
||||
for (const key of contentMetadataKeys) {
|
||||
if (key in schema) {
|
||||
extraMeta[key] = schema[key];
|
||||
}
|
||||
}
|
||||
// Unrecognized keys (custom metadata)
|
||||
for (const key of Object.keys(schema)) {
|
||||
if (!RECOGNIZED_KEYS.has(key)) {
|
||||
extraMeta[key] = schema[key];
|
||||
@@ -561,6 +559,12 @@ function convertSchema(schema, ctx) {
|
||||
if (Object.keys(extraMeta).length > 0) {
|
||||
ctx.registry.add(baseSchema, extraMeta);
|
||||
}
|
||||
// Apply description last. `.describe()` clones the schema and sets
|
||||
// `_zod.parent` on the clone, so registry lookups on the returned reference
|
||||
// still resolve `extraMeta` via parent inheritance.
|
||||
if (schema.description) {
|
||||
baseSchema = baseSchema.describe(schema.description);
|
||||
}
|
||||
return baseSchema;
|
||||
}
|
||||
/**
|
||||
@@ -570,15 +574,26 @@ export function fromJSONSchema(schema, params) {
|
||||
if (typeof schema === "boolean") {
|
||||
return schema ? z.any() : z.never();
|
||||
}
|
||||
const version = detectVersion(schema, params?.defaultTarget);
|
||||
const defs = (schema.$defs || schema.definitions || {});
|
||||
// Normalize input via a JSON round-trip. This guarantees the converter
|
||||
// walks a plain, finite, JSON-valid object graph: cyclic inputs fail here,
|
||||
// getter/Proxy-based properties are materialized into static values, and
|
||||
// class instances collapse to plain objects.
|
||||
let normalized;
|
||||
try {
|
||||
normalized = JSON.parse(JSON.stringify(schema));
|
||||
}
|
||||
catch {
|
||||
throw new Error("fromJSONSchema input is not valid JSON (possibly cyclic); use $defs/$ref for recursive schemas");
|
||||
}
|
||||
const version = detectVersion(normalized, params?.defaultTarget);
|
||||
const defs = (normalized.$defs || normalized.definitions || {});
|
||||
const ctx = {
|
||||
version,
|
||||
defs,
|
||||
refs: new Map(),
|
||||
processing: new Set(),
|
||||
rootSchema: schema,
|
||||
rootSchema: normalized,
|
||||
registry: params?.registry ?? globalRegistry,
|
||||
};
|
||||
return convertSchema(schema, ctx);
|
||||
return convertSchema(normalized, ctx);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user