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

test(zod-form-data.test.ts): add test case for failing on multiple items with correct error #322

Merged
merged 1 commit into from Sep 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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