Skip to content

Commit

Permalink
feat: restrict certain special characters from entering in public pro…
Browse files Browse the repository at this point in the history
…ject name
  • Loading branch information
junminahn committed Mar 12, 2024
1 parent d637413 commit d938ca9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 0 additions & 2 deletions app/api/private-cloud/decision/[licencePlate]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ export async function POST(req: NextRequest, { params }: { params: Params }) {
const parsedBody = PrivateCloudDecisionRequestBodySchema.safeParse(body);

if (!parsedParams.success) {
console.log(parsedParams.error.message);
return new Response(parsedParams.error.message, { status: 400 });
}

if (!parsedBody.success) {
console.log(parsedBody.error.message);
return new Response(parsedBody.error.message, { status: 400 });
}

Expand Down
18 changes: 14 additions & 4 deletions components/form/ProjectDescriptionPublic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { useFormContext } from 'react-hook-form';
import { providers, ministriesNames } from '@/constants';
import AGMinistryCheckBox from '@/components/form/AGMinistryCheckBox';

function stripSpecialCharacters(text: string) {
const pattern = /[^A-Za-z0-9///.:+=@_ ]/g;
return text.replace(pattern, '');
}

export default function ProjectDescriptionPublic({
mode,
disabled,
Expand All @@ -17,6 +22,8 @@ export default function ProjectDescriptionPublic({
const {
register,
formState: { errors },
getValues,
setValue,
} = useFormContext();

return (
Expand All @@ -33,7 +40,6 @@ export default function ProjectDescriptionPublic({
Cloud Accelerator Service team. Reach out to
{
<a className="text-blue-600 dark:text-blue-500 hover:underline" href="mailto:[email protected]">
{' '}
[email protected]{' '}
</a>
}
Expand All @@ -57,11 +63,15 @@ export default function ProjectDescriptionPublic({
? 'disabled:bg-slate-50 disabled:text-slate-500 disabled:border-slate-200 disabled:shadow-noneinvalid:border-pink-500 invalid:text-pink-600 focus:invalid:border-pink-500 focus:invalid:ring-pink-500'
: '',
)}
{...register('name')}
{...register('name', {
onChange: (e) => {
setValue('name', stripSpecialCharacters(e.target.value));
},
})}
/>
</div>
<p className={classNames(errors.name ? 'text-red-400' : '', 'mt-3 text-sm leading-6 text-gray-600')}>
Please provide a descriptive product name with no acronyms {errors.name?.message?.toString()}
Please provide a descriptive product name with no acronyms. (Only /. : + = @ _ special symbols are allowed)
</p>
</div>

Expand Down Expand Up @@ -113,7 +123,7 @@ export default function ProjectDescriptionPublic({
</select>

<p className={classNames(errors.ministry ? 'text-red-400' : '', 'mt-3 text-sm leading-6 text-gray-600')}>
Select the government ministry that this product belongs to
Select the government ministry that this product belongs to.
</p>
{['create', 'edit'].includes(mode) && <AGMinistryCheckBox disabled={disabled} />}
</div>
Expand Down
1 change: 0 additions & 1 deletion emails/_components/ProductDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default function ProductDetails({
expenseAuthority?: User | null;
licencePlate?: string;
}) {
console.log('expenseAuthority', expenseAuthority);
return (
<div>
<Heading className="text-lg">Product Details</Heading>
Expand Down
5 changes: 1 addition & 4 deletions schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ export const PublicCloudCreateRequestBodySchema = z.object({
name: z
.string()
.min(1, { message: 'Name is required.' })
.refine(
(value) => !/[!#$%^&*()_\-\[\]{};'"\\|,<>\?]/g.test(value),
'Only /. : + = @ _ special symbols are allowed',
),
.refine((value) => !/[^A-Za-z0-9///.:+=@_ ]/g.test(value), 'Only /. : + = @ _ special symbols are allowed'),
accountCoding: z
.string()
.refine((value) => /^[0-9A-Z\s]+$/.test(value), 'Account Coding should contain only uppercase characters, digits')
Expand Down

0 comments on commit d938ca9

Please sign in to comment.