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:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user