-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add improved regex for iter/soa institute (#237)
* 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
Showing
4 changed files
with
119 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')), | ||
}); |