Refactor: Introduce requiredString helper for consistent validation across routes and services
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 8m21s
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 8m21s
This commit is contained in:
@@ -33,6 +33,9 @@ const validatePasswordStrength = (password: string): { isValid: boolean; feedbac
|
||||
return { isValid: true };
|
||||
};
|
||||
|
||||
// Helper for consistent required string validation (handles missing/null/empty)
|
||||
const requiredString = (message: string) => z.preprocess((val) => val ?? '', z.string().min(1, message));
|
||||
|
||||
// --- Zod Schemas for User Routes (as per ADR-003) ---
|
||||
|
||||
const numericIdParam = (key: string) => z.object({
|
||||
@@ -54,17 +57,17 @@ const updatePasswordSchema = z.object({
|
||||
});
|
||||
|
||||
const deleteAccountSchema = z.object({
|
||||
body: z.object({ password: z.string().min(1, "Field 'password' is required.") }),
|
||||
body: z.object({ password: requiredString("Field 'password' is required.") }),
|
||||
});
|
||||
|
||||
const addWatchedItemSchema = z.object({
|
||||
body: z.object({
|
||||
itemName: z.string().min(1, "Field 'itemName' is required."),
|
||||
category: z.string().min(1, "Field 'category' is required."),
|
||||
itemName: requiredString("Field 'itemName' is required."),
|
||||
category: requiredString("Field 'category' is required."),
|
||||
}),
|
||||
});
|
||||
|
||||
const createShoppingListSchema = z.object({ body: z.object({ name: z.string().min(1, "Field 'name' is required.") }) });
|
||||
const createShoppingListSchema = z.object({ body: z.object({ name: requiredString("Field 'name' is required.") }) });
|
||||
|
||||
// Apply the JWT authentication middleware to all routes in this file.
|
||||
const notificationQuerySchema = z.object({
|
||||
@@ -417,7 +420,7 @@ router.delete('/shopping-lists/:listId', validateRequest(shoppingListIdSchema),
|
||||
const addShoppingListItemSchema = shoppingListIdSchema.extend({
|
||||
body: z.object({
|
||||
masterItemId: z.number().int().positive().optional(),
|
||||
customItemName: z.string().min(1).optional(),
|
||||
customItemName: requiredString('customItemName required?'),
|
||||
}).refine(data => data.masterItemId || data.customItemName, { message: 'Either masterItemId or customItemName must be provided.' }),
|
||||
});
|
||||
type AddShoppingListItemRequest = z.infer<typeof addShoppingListItemSchema>;
|
||||
|
||||
Reference in New Issue
Block a user