Skip to content

Commit c846d8e

Browse files
Better typing for updateWishlistSchema
1 parent 6f16318 commit c846d8e

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

‎src/app/api/wishlists/[wishlistId]/route.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { updateWishlistSchema } from "@/schema/wishlist";
22
import UserService from "@/services/userService";
33
import { WishlistService } from "@/services/wishlistService";
44
import { ArgumentError, errorResponse, ok } from "@/util/errors";
5-
import { $Enums } from "@prisma/client";
65
import { NextRequest } from "next/server";
76

87
/**
@@ -39,9 +38,8 @@ export async function PATCH(
3938
}
4039

4140
await WishlistService.updateWishlist({
42-
...parsed.data,
4341
id: Number(wishlistId),
44-
priority: parsed.data.priority as $Enums.RequestPriority,
42+
...parsed.data,
4543
});
4644

4745
return ok();

‎src/schema/wishlist.ts‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { UpdateWishlistData } from "@/types/api/wishlist.types";
12
import { $Enums } from "@prisma/client";
2-
import { z } from "zod";
3+
import { z, ZodObject } from "zod";
34

45
const createObj = {
56
name: z.string().min(3).max(100),
@@ -14,15 +15,17 @@ const createObj = {
1415
export const createWishlistSchema = z.object(createObj);
1516

1617
/**
17-
* The create schema, but every field is optional and there's a mandatory ID field.
18-
* I tried to automatically map fields from createObj, but it caused type errors.
18+
* The create schema, but every field is optional.
19+
*
1920
* @see createWishlistSchema
2021
*/
21-
export const updateWishlistSchema = z.object({
22-
id: z.number().min(1),
23-
name: createObj.name.optional(),
24-
unitSize: createObj.unitSize.optional(),
25-
quantity: createObj.quantity.optional(),
26-
priority: createObj.priority.optional(),
27-
comments: createObj.comments.optional(),
22+
export const updateWishlistSchema: ZodObject<
23+
Partial<typeof createObj>, // Input type
24+
"strip",
25+
z.ZodTypeAny,
26+
Omit<UpdateWishlistData, "id"> // Output type
27+
> = z.object({
28+
...Object.fromEntries(
29+
Object.entries(createObj).map(([key, value]) => [key, value.optional()])
30+
),
2831
});

0 commit comments

Comments
 (0)