chore: bump @kevisual/router to version 0.0.80, update QueryApi to handle optional fields in metadata args, and enhance JSON Schema conversion

This commit is contained in:
2026-02-18 12:58:44 +08:00
parent 73a9868c19
commit 05dace0c79
10 changed files with 183 additions and 37 deletions

View File

@@ -1,21 +1,32 @@
import z, { toJSONSchema } from "zod";
import z from "zod";
import { toJSONSchema, fromJSONSchema } from './test-schema.ts'
const schema = z.object({
name: z.string().describe("The name of the person"),
age: z.number().int().min(0).describe("The age of the person"),
email: z.string().optional().describe("The email address of the person"),
});
console.log("JSON Schema for the person object:");
})
const nameSchema = { name: z.string().optional().describe("The name of the person") }
// console.log("JSON Schema for the person object:");
// console.log(
// JSON.stringify(toJSONSchema(nameSchema), null, 2)
// );
console.log("\n--- 自定义 override ---");
const jsonSchema = toJSONSchema(nameSchema)
console.log(
JSON.stringify(toJSONSchema(schema), null, 2)
JSON.stringify(jsonSchema, null, 2)
);
console.log('shape', schema.shape);
console.log('shape name', schema.shape.name.toJSONSchema());
// console.log('shape', schema.shape);
// console.log('shape name', schema.shape.name.toJSONSchema());
// const jsonSchema = toJSONSchema(schema);
// const schema2 = z.fromJSONSchema(jsonSchema);
let schema2 = fromJSONSchema<false>(jsonSchema);
console.log("\n--- 从 JSON Schema 反向转换回 Zod schema ---");
console.log('schema2 nameSchema', schema2.name.safeParse("John Doe"));
// console.log('schema2', schema2.safeParse({ name: "John Doe", }));
// console.log('schema2 email', schema2.email.safeParse(undefined));
// console.log('schema2 age', schema2.age.safeParse(1));
// // schema2 的类型是 ZodSchema<any>,所以无法在编译时推断出具体类型
// // 这是 fromJSONSchema 的限制 - JSON Schema 转换会丢失 TypeScript 类型信息