Add IBAN validation using iban.js library

Implemented optional IBAN validation for the account form profile:
- Installed iban.js package for robust IBAN validation
- Added Zod validation with IBAN.isValid() for format checking
- Normalizes IBAN (removes spaces, uppercase) before storage
- Validates country-specific formats and checksums
- Added validation error messages in English and Croatian

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Knee Cola
2025-11-17 20:23:11 +01:00
parent 86fa6a67fe
commit 216b08c12b
5 changed files with 38 additions and 4 deletions

View File

@@ -10,6 +10,7 @@ import { IntlTemplateFn } from '@/app/i18n';
import { getTranslations, getLocale } from "next-intl/server";
import { revalidatePath } from 'next/cache';
import { gotoHomeWithMessage } from './navigationActions';
import * as IBAN from 'iban';
export type State = {
errors?: {
@@ -29,7 +30,17 @@ const FormSchema = (t: IntlTemplateFn) => z.object({
firstName: z.string().optional(),
lastName: z.string().optional(),
address: z.string().optional(),
iban: z.string().optional(),
iban: z.string()
.optional()
.refine(
(val) => {
if (!val || val.trim() === '') return true;
// Remove spaces and validate using iban.js library
const cleaned = val.replace(/\s/g, '').toUpperCase();
return IBAN.isValid(cleaned);
},
{ message: t("iban-invalid") }
),
});
/**
@@ -73,6 +84,9 @@ export const updateUserProfile = withUser(async (user: AuthenticatedUser, prevSt
const { firstName, lastName, address, iban } = validatedFields.data;
// Normalize IBAN: remove spaces and convert to uppercase
const normalizedIban = iban ? iban.replace(/\s/g, '').toUpperCase() : null;
// Update the user profile in MongoDB
const dbClient = await getDbClient();
const { id: userId } = user;
@@ -82,7 +96,7 @@ export const updateUserProfile = withUser(async (user: AuthenticatedUser, prevSt
firstName: firstName || null,
lastName: lastName || null,
address: address || null,
iban: iban || null,
iban: normalizedIban,
};
await dbClient.collection<UserProfile>("users")