Skip to content

Commit

Permalink
fix: .merge results in Typescript error
Browse files Browse the repository at this point in the history
  • Loading branch information
roncohen committed Nov 21, 2024
1 parent c08ac95 commit f9203bc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion scripts/accelerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { zodSchemaIsAsync } from "./utils/zodSchemaIsAsync";

declare module "zod" {
interface ZodType {
accelerator?: ZodAcceleratorParser<this>;
accelerator?: ZodAcceleratorParser;
}
}

Expand Down
58 changes: 58 additions & 0 deletions scripts/accelerators/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,62 @@ describe("object type", () => {
]);
}
});

it("input merged object", () => {
const schema = zod.object({
test1: zod.string(),
}).merge(zod.object({
test2: zod.number(),
})).strict();

const accelerateSchema = ZodAccelerator.build(schema);
let data: any = {
test1: "test",
test2: 1,
};

expect(accelerateSchema.parse(data)).toStrictEqual(schema.parse(data));

data = {
test1: "test",
};

try {
accelerateSchema.parse(data);
throw new Error();
} catch (error: any) {
const err: ZodAcceleratorError = error;
expect(err).instanceOf(ZodAcceleratorError);
expect(schema.safeParse(data).success).toBe(false);
expect(err.issues).toStrictEqual([
{
code: "custom",
message: ".test2 : Input is not a Number.",
path: ["test2"],
},
]);
}

data = {
test1: "test",
test2: 1,
test3: 1,
};

try {
accelerateSchema.parse(data);
throw new Error();
} catch (error: any) {
const err: ZodAcceleratorError = error;
expect(err).instanceOf(ZodAcceleratorError);
expect(schema.safeParse(data).success).toBe(false);
expect(err.issues).toStrictEqual([
{
code: "custom",
message: ".test3 : Input Object has key to many.",
path: ["test3"],
},
]);
}
});
});

0 comments on commit f9203bc

Please sign in to comment.