Skip to content

Commit

Permalink
Merge pull request #322 from maorun/main
Browse files Browse the repository at this point in the history
test(zod-form-data.test.ts): add test case for failing on multiple items with correct error
  • Loading branch information
airjp73 committed Sep 22, 2023
2 parents b3dffbb + 47580c7 commit 6f3cc12
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions packages/zod-form-data/src/zod-form-data.test.ts
@@ -1,11 +1,11 @@
import { TestFormData } from "@remix-validated-form/test-utils";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { z } from "zod";
import { z, ZodError } from "zod";
import { zfd } from "./";

const expectError = (schema: z.Schema<any>, val: any) => {
const expectError = (schema: z.Schema<any>, val: any, error?: ZodError) => {
expect(schema.safeParse(val)).toMatchObject({
// error: expect.any(z.ZodError),
error: error ? error : expect.any(z.ZodError),
success: false,
});
};
Expand Down Expand Up @@ -155,6 +155,31 @@ describe("zod helpers", () => {
expectError(s, "12");
expect(s.parse("13")).toEqual([13]);
});
it("should fail on multiple items with correct error", () => {
const s = zfd.repeatableOfType(zfd.numeric(z.number().positive()));
expectError(
s,
["adsf", -123],
new ZodError([
{
code: "invalid_type",
expected: "number",
received: "string",
path: [0],
message: "Expected number, received string",
},
{
code: "too_small",
minimum: 0,
type: "number",
inclusive: false,
message: "Number must be greater than 0",
path: [1],
},
])
);
expect(s.parse("13")).toEqual([13]);
});
});

describe("file", () => {
Expand Down

1 comment on commit 6f3cc12

@vercel
Copy link

@vercel vercel bot commented on 6f3cc12 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.