Files
SnowLuma/.pnpm-store/v11/files/54/2033b76eeafcb7918705fd6ff1a75adfd02ef7a6ff19fb74142725807b47b4172a18bde58e1d31b8cec1112523bdf57a0887d07e418601f7245786cb715193
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

55 lines
2.0 KiB
Plaintext

import { describe, expect, expectTypeOf, test } from "vitest";
import * as z from "zod/mini";
describe("mini deepPartial", () => {
test("makes a ZodMiniObject's fields optional recursively", () => {
const schema = z.object({
name: z.string(),
profile: z.object({ bio: z.string(), age: z.number() }),
});
const partial = z.deepPartial(schema);
expectTypeOf(partial).toExtend<z.ZodMiniObject>();
expect(partial.parse({})).toEqual({});
expect(partial.parse({ profile: {} })).toEqual({ profile: {} });
expect(partial.parse({ name: "x", profile: { bio: "b" } })).toEqual({
name: "x",
profile: { bio: "b" },
});
});
test("preserves ZodMiniObject structural type (shape accessible)", () => {
const schema = z.object({ a: z.string(), nested: z.object({ b: z.number() }) });
const partial = z.deepPartial(schema);
expectTypeOf(partial.shape.a).toExtend<z.ZodMiniOptional<z.ZodMiniString>>();
expectTypeOf(partial.shape.nested).toExtend<z.ZodMiniOptional<z.ZodMiniObject>>();
});
test("tuple elements stay required", () => {
const schema = z.tuple([z.object({ a: z.string() }), z.string()]);
const partial = z.deepPartial(schema);
expect(() => partial.parse([{}])).toThrow();
expect(partial.parse([{}, "x"])).toEqual([{}, "x"]);
});
test("ZodMiniCodec preserved through deepPartial", () => {
const schema = z.codec(z.object({ a: z.string() }), z.object({ a: z.string() }), {
decode: (v) => v,
encode: (v) => v,
});
const partial = z.deepPartial(schema);
expectTypeOf(partial).toExtend<z.ZodMiniCodec>();
});
test("recursive getter-based object is typeable", () => {
const Category = z.object({
name: z.string(),
get subcategories() {
return z.optional(z.array(Category));
},
});
const partial = z.deepPartial(Category);
expectTypeOf(partial).toExtend<z.ZodMiniObject>();
expect(partial.parse({ subcategories: [{}] })).toEqual({ subcategories: [{}] });
});
});