Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(transfer-alert): improve validation #496

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions packages/app-builder/src/models/transfer-alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type TransferAlertUpdateAsBeneficiaryBodyDto,
type TransferAlertUpdateAsSenderBodyDto,
} from 'marble-api/generated/transfercheck-api';
import { z } from 'zod';

export const transferAlerStatuses = [
'pending',
Expand Down Expand Up @@ -70,9 +71,9 @@ export function adaptTransferAlertBeneficiary(
export interface TransferAlertCreateBody {
transferId: string;
message: string;
transferEndToEndId: string;
beneficiaryIban: string;
senderIban: string;
transferEndToEndId?: string;
beneficiaryIban?: string;
senderIban?: string;
}

export function adaptTransferAlertCreateBodyDto(
Expand All @@ -81,9 +82,9 @@ export function adaptTransferAlertCreateBodyDto(
return {
transfer_id: createTransferAlert.transferId,
message: createTransferAlert.message,
transfer_end_to_end_id: createTransferAlert.transferEndToEndId,
beneficiary_iban: createTransferAlert.beneficiaryIban,
sender_iban: createTransferAlert.senderIban,
transfer_end_to_end_id: createTransferAlert.transferEndToEndId ?? '',
beneficiary_iban: createTransferAlert.beneficiaryIban ?? '',
sender_iban: createTransferAlert.senderIban ?? '',
};
}

Expand All @@ -99,10 +100,10 @@ export function adaptTransferAlertUpdateAsSenderBodyDto(
body: TransferAlertUpdateAsSenderBody,
): TransferAlertUpdateAsSenderBodyDto {
return {
message: body.message,
transfer_end_to_end_id: body.transferEndToEndId,
beneficiary_iban: body.beneficiaryIban,
sender_iban: body.senderIban,
message: body.message ?? '',
transfer_end_to_end_id: body.transferEndToEndId ?? '',
beneficiary_iban: body.beneficiaryIban ?? '',
sender_iban: body.senderIban ?? '',
};
}

Expand All @@ -118,3 +119,19 @@ export function adaptUpdateTransferAlertAsBeneficiaryDto(
status: body.status,
};
}

export const messageSchema = z
.string({ required_error: 'required' })
.max(1000, { message: 'max 1000 characters' });

export const transferEndToEndIdSchema = z
.string()
.max(100, { message: 'max 100 characters' });

export const senderIbanSchema = z
.string()
.max(34, { message: 'max 34 characters' });

export const beneficiaryIbanSchema = z
.string()
.max(34, { message: 'max 34 characters' });
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import { FormInput } from '@app-builder/components/Form/FormInput';
import { FormLabel } from '@app-builder/components/Form/FormLabel';
import { FormTextArea } from '@app-builder/components/Form/FormTextArea';
import { setToastMessage } from '@app-builder/components/MarbleToaster';
import {
beneficiaryIbanSchema,
messageSchema,
senderIbanSchema,
transferEndToEndIdSchema,
} from '@app-builder/models/transfer-alert';
import { serverServices } from '@app-builder/services/init.server';
import { getRoute } from '@app-builder/utils/routes';
import { conform, useForm } from '@conform-to/react';
import { getFieldsetConstraint, parse } from '@conform-to/zod';
import { parse } from '@conform-to/zod';
import { type ActionFunctionArgs, json } from '@remix-run/node';
import { useFetcher } from '@remix-run/react';
import * as React from 'react';
Expand All @@ -18,10 +24,10 @@ import { z } from 'zod';

const createAlertFormSchema = z.object({
transferId: z.string(),
message: z.string({ required_error: 'required' }),
transferEndToEndId: z.string(),
senderIban: z.string(),
beneficiaryIban: z.string(),
message: messageSchema,
transferEndToEndId: transferEndToEndIdSchema.optional(),
senderIban: senderIbanSchema.optional(),
beneficiaryIban: beneficiaryIbanSchema.optional(),
});

type CreateAlertForm = z.infer<typeof createAlertFormSchema>;
Expand Down Expand Up @@ -121,7 +127,6 @@ function CreateAlertContent({
id: formId,
defaultValue: { ...defaultValue, message: '' },
lastSubmission: fetcher.data?.submission,
constraint: getFieldsetConstraint(createAlertFormSchema),
onValidate({ formData }) {
return parse(formData, {
schema: createAlertFormSchema,
Expand All @@ -138,6 +143,17 @@ function CreateAlertContent({
<ModalV2.Title>{t('transfercheck:alert.create.title')}</ModalV2.Title>
<div className="flex flex-col gap-6 p-6">
<input {...conform.input(fields.transferId, { type: 'hidden' })} />
<FormField
config={fields.message}
className="flex flex-col items-start gap-2"
>
<FormLabel>{t('transfercheck:alert.create.message')}</FormLabel>
<FormTextArea
className="w-full"
placeholder={t('transfercheck:alert.create.message.placeholder')}
/>
<FormError />
</FormField>
<FormField
config={fields.transferEndToEndId}
className="flex flex-col items-start gap-2"
Expand Down Expand Up @@ -179,17 +195,6 @@ function CreateAlertContent({
/>
<FormError />
</FormField>
<FormField
config={fields.message}
className="flex flex-col items-start gap-2"
>
<FormLabel>{t('transfercheck:alert.create.message')}</FormLabel>
<FormTextArea
className="w-full"
placeholder={t('transfercheck:alert.create.message.placeholder')}
/>
<FormError />
</FormField>

<div className="flex flex-1 flex-row gap-2">
<ModalV2.Close
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import { FormInput } from '@app-builder/components/Form/FormInput';
import { FormLabel } from '@app-builder/components/Form/FormLabel';
import { FormTextArea } from '@app-builder/components/Form/FormTextArea';
import { setToastMessage } from '@app-builder/components/MarbleToaster';
import {
beneficiaryIbanSchema,
messageSchema,
senderIbanSchema,
transferEndToEndIdSchema,
} from '@app-builder/models/transfer-alert';
import { serverServices } from '@app-builder/services/init.server';
import { getRoute } from '@app-builder/utils/routes';
import { conform, useForm } from '@conform-to/react';
import { getFieldsetConstraint, parse } from '@conform-to/zod';
import { parse } from '@conform-to/zod';
import { type ActionFunctionArgs, json } from '@remix-run/node';
import { useFetcher } from '@remix-run/react';
import * as React from 'react';
Expand All @@ -18,10 +24,10 @@ import { z } from 'zod';

const updateAlertFormSchema = z.object({
alertId: z.string(),
message: z.string({ required_error: 'required' }),
transferEndToEndId: z.string(),
senderIban: z.string(),
beneficiaryIban: z.string(),
message: messageSchema,
transferEndToEndId: transferEndToEndIdSchema.optional(),
senderIban: senderIbanSchema.optional(),
beneficiaryIban: beneficiaryIbanSchema.optional(),
});

type UpdateAlertForm = z.infer<typeof updateAlertFormSchema>;
Expand Down Expand Up @@ -121,7 +127,6 @@ function UpdateAlertContent({
id: formId,
defaultValue,
lastSubmission: fetcher.data?.submission,
constraint: getFieldsetConstraint(updateAlertFormSchema),
onValidate({ formData }) {
return parse(formData, {
schema: updateAlertFormSchema,
Expand All @@ -138,6 +143,17 @@ function UpdateAlertContent({
<ModalV2.Title>{t('transfercheck:alert.update.title')}</ModalV2.Title>
<div className="flex flex-col gap-6 p-6">
<input {...conform.input(fields.alertId, { type: 'hidden' })} />
<FormField
config={fields.message}
className="flex flex-col items-start gap-2"
>
<FormLabel>{t('transfercheck:alert.update.message')}</FormLabel>
<FormTextArea
className="w-full"
placeholder={t('transfercheck:alert.update.message.placeholder')}
/>
<FormError />
</FormField>
<FormField
config={fields.transferEndToEndId}
className="flex flex-col items-start gap-2"
Expand Down Expand Up @@ -179,17 +195,6 @@ function UpdateAlertContent({
/>
<FormError />
</FormField>
<FormField
config={fields.message}
className="flex flex-col items-start gap-2"
>
<FormLabel>{t('transfercheck:alert.update.message')}</FormLabel>
<FormTextArea
className="w-full"
placeholder={t('transfercheck:alert.update.message.placeholder')}
/>
<FormError />
</FormField>

<div className="flex flex-1 flex-row gap-2">
<ModalV2.Close
Expand Down