Skip to content

Commit

Permalink
fix: add improved regex for iter/soa institute (#237)
Browse files Browse the repository at this point in the history
* chore: added new regex for ITER

* fix: fix added for others option bug

* chore: optimize zodd schema

* fix: typos

* fix: bug fixes
  • Loading branch information
ayussh-2 authored Oct 29, 2024
1 parent 9e5803e commit 60b3a63
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/app/register/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { useIsLoggedIn } from '@/hooks/useIsLoggedIn';
import { useUserDetails } from '@/hooks/useUserDetails';
import handleLoadingAndToast from '@/utils/handleLoadingToast';
import { uploadToCloudinary } from '@/utils/uploadToCloudinary';
import { useMutation, useSuspenseQuery, skipToken } from '@apollo/client';
import { useMutation, useSuspenseQuery } from '@apollo/client';
import { GET_USER_BY_UID } from '@/graphql/queries/userQueries';

import {
Expand Down
38 changes: 16 additions & 22 deletions src/components/Register/SelectField/SelectField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ function SelectField({
const [searchQuery, setSearchQuery] = useState('');
const ref = useRef(null);
const isOneLine = className?.includes('oneliner');

const isOthers =
(!options.some((option) => option.value === selectedOption) && selectedOption !== '') ||
selectedOption === 'others';
const [isOthers, setIsOthers] = useState(false);

useEffect(() => {
setSelectedOption(value || '');
Expand All @@ -53,31 +50,28 @@ function SelectField({
);
return;
}

setSelectedOption(option);
setIsOpen(false);
setSearchQuery('');
setErrors((prevState) => ({
...prevState,
[name]: '',
}));
setErrors((prev) => ({ ...prev, [name]: '' }));

if (option === '') {
handleSelect((prevState) => ({
...prevState,
[name]: otherInstituteName,
}));
} else {
if (name === 'institute') {
handleSelect((prevState) => ({
...prevState,
instituteId: id,
}));
}
handleSelect((prev) => ({ ...prev, [name]: otherInstituteName }));
return;
}

handleSelect((prevState) => ({
...prevState,
[name]: option,
if (name === 'institute') {
setIsOthers(!id);
handleSelect((prev) => ({
...prev,
instituteId: id || null,
institute: option,
}));
return;
}

handleSelect((prev) => ({ ...prev, [name]: option }));
};

useEffect(() => {
Expand Down
46 changes: 43 additions & 3 deletions src/config/content/Registration/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ export const formFields = [
id: '671a5be86748c70b7f893ccc',
},
{
label: 'Institute of Technical Education and Research SOA',
value: 'Institute of Technical Education and Research SOA',
label: 'Institute of Technical Education and Research (ITER) SOA',
value: 'Institute of Technical Education and Research (ITER) SOA',
id: 'notAllowed',
},
{
Expand Down Expand Up @@ -300,7 +300,7 @@ export const formFields = [
{
label: 'Bank Account Number',
type: 'title',
content: '_00000037377186107',
content: '37377186107',
id: 'bankAccountNumber',
},
{
Expand Down Expand Up @@ -370,11 +370,51 @@ export const campusAbassadorPara = `To become a Campus Ambassador for Innovision
export const notAllowedInstitutes = [
'Siksha O Anusandhan',
"Siksha 'O' Anusandhan",
'Siksha "O" Anusandhan',
'Siksha-O-Anusandhan',
'Siksha o Anusandhan',
'Siksha 0 Anusandhan',

'Siksha O Anusandhan (SOA)',
'Siksha O Anusandhan - SOA',
"Siksha 'O' Anusandhan SOA",
'Siksha 0 Anusandhan SOA',
'Siksha O Anushandhan SOA',
'Siksha-O-Anusandhan SOA',

'Siksha O Anusandhan Institute of Technical Education and Research',
"Siksha 'O' Anusandhan Institute of Technical Education and Research",
'Siksha 0 Anusandhan Institute of Technical Education and Research',
'Siksha-O-Anusandhan Institute of Technical Education and Research',

'SOA',
'S.O.A',
'S.O.A.',
'SoA',

'ITER',
'I.T.E.R',
'I.T.E.R.',

'Institute of Technical Education and Research',
'Institute of Technical Education & Research',
'Institute of Technical Education And Research',

'Institute of Technical Education and Research SOA',
'Institute of Technical Education & Research SOA',
'Institute of Technical Education and Research (SOA)',
'ITER SOA',
'ITER-SOA',
'ITER - SOA',

'Siksha O Anusandhan',
'Siksha O Anusandhaan',
'Sikha O Anusandhan',
'Siksha O Anushandan',

'siksha o anusandhan',
'soa',
'iter',
];

export const undertakingContent = {
Expand Down
90 changes: 59 additions & 31 deletions src/config/zodd/userDetailsSchema.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,79 @@
import { z } from 'zod';

import { notAllowedInstitutes } from '../content/Registration/details';

const PATTERNS = {
NAME: /^[a-zA-Z\s]+$/,
EMAIL: /^[a-z0-9](?:\.?[a-z0-9]){5,}@g(?:oogle)?mail\.com$/,
PHONE: /^\d{10}$/,
REFERRAL: /^\d{10,}$/,
};

const MESSAGES = {
REQUIRED: (field) => `${field} is required`,
INVALID: (field) => `Invalid ${field.toLowerCase()}`,
INSTITUTE_BANNED:
"Students from this institute/university have been officially barred from participating in INNO'24",
PERMISSION_REQUIRED: "You must have permission from your institute's authority",
TERMS_REQUIRED: 'You must agree to the terms and conditions',
GENDER_REQUIRED: 'Gender selection is required and must be either male or female',
};

const normalizeText = (text) => {
return text
.toLowerCase()
.replace(/['"`-]/g, '')
.replace(/\s+/g, ' ')
.trim();
};

const normalizedBlockedInstitutes = new Set(notAllowedInstitutes.map(normalizeText));

const institutionValidation = z
.string()
.min(1, MESSAGES.REQUIRED('Institution name'))
.transform(normalizeText)
.refine((val) => !normalizedBlockedInstitutes.has(val), { message: MESSAGES.INSTITUTE_BANNED });

export const userSchema = z.object({
name: z
name: z.string().min(1, MESSAGES.REQUIRED('Name')).regex(PATTERNS.NAME, MESSAGES.INVALID('name')),

email: z
.string()
.min(1, 'Name is required')
.regex(/^[a-zA-Z\s]+$/, 'Name can only contain letters'),
email: z.string().regex(/^[a-z0-9](\.?[a-z0-9]){5,}@g(oogle)?mail\.com$/, {
message: 'Invalid email address. Please use your Gmail address',
}),
.regex(PATTERNS.EMAIL, MESSAGES.INVALID('email address. Please use your Gmail address')),

phone: z
.string()
.length(10, 'Phone number must be exactly 10 digits')
.regex(/^\d{10}$/, 'Invalid phone number'),
institute: z
.string()
.min(1, 'Institute name is required')
.refine((val) => notAllowedInstitutes.indexOf(val.toUpperCase()) === -1, {
message:
"Students from this institute have been officially barred from participating in INNO'24",
}),
idCard: z.string().url('ID Card is required'),
payment: z.string().url('Payment receipt is required'),
university: z
.string()
.min(1, 'University name is required')
.refine((val) => notAllowedInstitutes.indexOf(val.toUpperCase()) === -1, {
message:
"Students from this institute have been officially barred from participating in INNO'24",
}),
rollNumber: z.string().min(1, 'Roll number is required'),
.regex(PATTERNS.PHONE, MESSAGES.INVALID('phone number')),

institute: institutionValidation,
university: institutionValidation,

idCard: z.string().url(MESSAGES.REQUIRED('ID Card')),

payment: z.string().url(MESSAGES.REQUIRED('Payment receipt')),

rollNumber: z.string().min(1, MESSAGES.REQUIRED('Roll number')),

referralCode: z
.string()
.regex(/^\d+$/, 'Invalid referral code')
.min(10, 'Invalid referral code')
.regex(PATTERNS.REFERRAL, MESSAGES.INVALID('referral code'))
.optional()
.or(z.literal('')),

permission: z.boolean().refine((val) => val === true, {
message: "You must have permission from your institute's authority",
message: MESSAGES.PERMISSION_REQUIRED,
}),

gender: z.enum(['MALE', 'FEMALE'], {
errorMap: () => ({ message: 'Gender selection is required and must be either male or female' }),
errorMap: () => ({ message: MESSAGES.GENDER_REQUIRED }),
}),

undertaking: z.boolean().refine((val) => val === true, {
message: 'You must agree to the terms and conditions',
message: MESSAGES.TERMS_REQUIRED,
}),

campusAmbassador: z.boolean().optional(),
transactionID: z.string().min(1, 'Transaction ID is required'),

transactionID: z.string().min(1, MESSAGES.REQUIRED('Transaction ID')),
});

0 comments on commit 60b3a63

Please sign in to comment.