Files
memecoin-botV2/.pnpm-store/v10/files/a7/498cf06aa55e92fb1942826e28b6711bf5763deffedb75ebae312a4da759c6539352609767ff43a5951aea93eec8c7bca8e78b9955f832b4354a7521763c89

60 lines
1.4 KiB
Plaintext

import { expect, test } from "vitest";
import * as z from "zod/v4";
test("safeExtend chaining preserves and overrides properties", () => {
const schema1 = z.object({
email: z.string(),
});
const schema2 = schema1.safeExtend({
email: schema1.shape.email.check(z.email()),
});
const schema3 = schema2.safeExtend({
email: schema2.shape.email.or(z.literal("")),
});
schema3.parse({ email: "test@example.com" });
});
test("extend with constructor field in shape", () => {
const baseSchema = z.object({
name: z.string(),
});
const extendedSchema = baseSchema.extend({
constructor: z.string(),
age: z.number(),
});
const result = extendedSchema.parse({
name: "John",
constructor: "Person",
age: 30,
});
expect(result).toEqual({
name: "John",
constructor: "Person",
age: 30,
});
const testCases = [
{ name: "Test", constructor: 123, age: 25 },
{ name: "Test", constructor: null, age: 25 },
{ name: "Test", constructor: true, age: 25 },
{ name: "Test", constructor: {}, age: 25 },
];
for (const testCase of testCases) {
const anyConstructorSchema = baseSchema.extend({
constructor: z.any(),
age: z.number(),
});
expect(() => anyConstructorSchema.parse(testCase)).not.toThrow();
const parsed = anyConstructorSchema.parse(testCase);
expect(parsed).toEqual(testCase);
}
});