Skip to content

Commit

Permalink
Merge pull request #67 from TheYoxy/main
Browse files Browse the repository at this point in the history
feat: added handling for date in createFormData
  • Loading branch information
AlemTuzlak authored Feb 13, 2024
2 parents 3c07728 + 214c935 commit 9f065b3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
58 changes: 57 additions & 1 deletion src/utilities/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { array, boolean, number, object, string } from "zod";
import { array, boolean, coerce, date, number, object, string } from "zod";
import {
createFormData,
generateFormData,
Expand Down Expand Up @@ -41,6 +41,12 @@ describe("createFormData", () => {
const formData = createFormData(undefined as any);
expect(formData).toBeTruthy();
});

it("should handle Date data", () => {
const date = "2024-01-01T00:00:00.000Z";
const formData = createFormData({ date: new Date(date) }, false);
expect(formData.get("date")).toEqual(date);
});
});

describe("parseFormData", () => {
Expand Down Expand Up @@ -232,6 +238,56 @@ describe("getFormDataFromSearchParams", () => {
});

describe("validateFormData", () => {
it("should return an error when parsing date object without coerce", async () => {
const formData = createFormData({
date: new Date(2024, 1, 1),
});
const returnData = await validateFormData(
formData,
zodResolver(object({ date: date() })),
);
expect(returnData.errors).toStrictEqual({
date: {
message: "Expected date, received string",
type: "invalid_type",
ref: undefined,
},
});
expect(returnData.data).toStrictEqual(undefined);
});

it("should return an error when parsing date object with coerce", async () => {
const formData = createFormData({
date: new Date(2024, 1, 1),
});
const returnData = await validateFormData(
formData,
zodResolver(object({ date: coerce.date() })),
);
expect(returnData.errors).toStrictEqual({
date: {
message: "Invalid date",
type: "invalid_date",
ref: undefined,
},
});
expect(returnData.data).toStrictEqual(undefined);
});

it("should return a correct value when formatting as object", async () => {
const formData = createFormData({
date: new Date(2024, 1, 1),
}, false);
const returnData = await validateFormData(
formData,
zodResolver(object({ date: coerce.date() })),
);
expect(returnData.errors).toStrictEqual(undefined);
expect(returnData.data).toStrictEqual({
date: new Date(2024, 1, 1),
});
});

it("should return an empty error object and valid data if there are no errors", async () => {
const formData = {
name: "John Doe",
Expand Down
18 changes: 8 additions & 10 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
FieldValues,
Resolver,
FieldErrors,
DeepRequired,
} from "react-hook-form";
import type { FieldValues, Resolver, FieldErrors } from "react-hook-form";

const tryParseJSON = (jsonString: string) => {
try {
Expand Down Expand Up @@ -167,10 +162,13 @@ export const createFormData = <T extends FieldValues>(
if (stringifyAll) {
formData.append(key, JSON.stringify(value));
} else {
formData.append(
key,
typeof value === "string" ? value : JSON.stringify(value),
);
if (typeof value === "string") {
formData.append(key, value);
} else if (value instanceof Date) {
formData.append(key, value.toISOString());
} else {
formData.append(key, JSON.stringify(value));
}
}
}
});
Expand Down

0 comments on commit 9f065b3

Please sign in to comment.