Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publish: 2.5.2 #61

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions scripts/accelerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import { ZodAcceleratorError } from "./error";
import { zodSchemaIsAsync } from "./utils/zodSchemaIsAsync";

declare module "zod" {
interface ZodType {
accelerator?: ZodAcceleratorParser<this>;
interface ZodType<
Output = any,
Def extends zod.ZodTypeDef = zod.ZodTypeDef,
Input = Output,
> {
accelerator?: ZodAcceleratorParser<
ZodType<Output, Def, Input>
>;
}
}

Expand Down
57 changes: 57 additions & 0 deletions scripts/accelerators/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,61 @@ describe("object type", () => {
]);
}
});
it("input merged object", () => {
const schemaA = zod.object({ test1: zod.string() });

const schemaB = zod.object({ test2: zod.number() });

const schema = schemaA.merge(schemaB).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"],
},
]);
}
});
});
Loading