From 47580c733ca0d87ae713678c5595d00cebc69df7 Mon Sep 17 00:00:00 2001 From: Marco Driemel Date: Fri, 1 Sep 2023 17:12:47 +0200 Subject: [PATCH] test(zod-form-data.test.ts): add test case for failing on multiple items with correct error The test case was added to ensure that the zod-form-data library correctly handles validation errors when multiple items fail validation. This helps to improve the reliability and accuracy of the library when dealing with repeatable fields. --- .../zod-form-data/src/zod-form-data.test.ts | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/zod-form-data/src/zod-form-data.test.ts b/packages/zod-form-data/src/zod-form-data.test.ts index 5883ebd6..539f47c8 100644 --- a/packages/zod-form-data/src/zod-form-data.test.ts +++ b/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, val: any) => { +const expectError = (schema: z.Schema, val: any, error?: ZodError) => { expect(schema.safeParse(val)).toMatchObject({ - // error: expect.any(z.ZodError), + error: error ? error : expect.any(z.ZodError), success: false, }); }; @@ -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", () => {