Repository navigation
Improve and add more SSN related Zod schemas #160
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
base: main
Are you sure you want to change the base?
Changes from all commits
0ce896e
9fe925b
08fd17a
b4a0304
b23ce7e
5d5d880
f54282e
96ee776
c9464e4
02544f0
5799883
4a0e0f5
d012187
35d55ae
ace5cc9
5478f3a
7eab39b
c3c6f4e
4d92c31
7fa4642
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,8 @@ export function isSsnValid(ssn: string, ageSanityCheckForExams = false): boolean | |
| } | ||
|
|
||
| const centurySeparator21st = ['A', 'B', 'C', 'D', 'E', 'F'] | ||
| const specialFakeSsnToBeAllowed = '010101-0101' | ||
| const centurySeparator20th = ['-', 'Y', 'X', 'W', 'V', 'U'] | ||
|
adetabs marked this conversation as resolved.
|
||
|
|
||
| const matched = ssn | ||
| .trim() | ||
| .match(/^([0-9]{6})([-+]|[A-F]|[a-f]|[Yy]|[Xx]|[Ww]|[Vv]|[Uu])([\d]{3}|U[\d]{2})([\dA-Ya-y])$/) | ||
|
|
@@ -56,10 +57,11 @@ export function isSsnValid(ssn: string, ageSanityCheckForExams = false): boolean | |
| // We don't want to always do these checks, for example if we validate | ||
| // data which is not related to current matricular exams | ||
| if (ageSanityCheckForExams) { | ||
| if (ssn.trim() === specialFakeSsnToBeAllowed) return true | ||
| const specialFakeSsnToBeAllowed = '010101-0101' | ||
| if (specialFakeSsnToBeAllowed === ssn.trim().toUpperCase()) return true | ||
| // Do not allow people over hundred years old (accidental use of - instead of A) | ||
| if (centurySeparator === '+') return false | ||
| if (centurySeparator === '-' && birthDateYear < currentYear) return false | ||
| if (centurySeparator20th.includes(centurySeparator) && birthDateYear < currentYear) return false | ||
|
|
||
| // Do not allow people not yet born | ||
| if (centurySeparator21st.includes(centurySeparator) && birthDateYear > currentYear) return false | ||
|
|
@@ -69,14 +71,23 @@ export function isSsnValid(ssn: string, ageSanityCheckForExams = false): boolean | |
| return checkCode === checkCodes[checkCodeIndex] | ||
| } | ||
|
|
||
| export const zodSsn = z.string().refine(isSsnValid, { message: 'Invalid ssn' }).toUpperCase() | ||
| export const ssnSchema = z.string().trim().refine(isSsnValid, { error: 'SSN must be valid' }).toUpperCase() | ||
|
|
||
| export const ssnSchemaWithAgeChecks = z | ||
| .string() | ||
| .trim() | ||
| .refine(isSsnValid, { error: 'SSN must be valid', abort: true }) | ||
| .refine(ssn => isSsnValid(ssn, true), { error: 'SSN fails age checks' }) | ||
| .toUpperCase() | ||
|
|
||
| export function isFakeYtlSsn(ssn: string): boolean { | ||
| const fakeYtlSsnRegEx = /\d{6}(AU|-U)\d{2}\w$/ | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clanker huomasi tässä myös ongelman että tuossa menee läpi esim. |
||
|
|
||
| return fakeYtlSsnRegEx.test(ssn) | ||
| } | ||
|
|
||
| export const fakeYtlSsnSchema = z.string().trim().refine(isFakeYtlSsn, { error: 'Invalid YTL fake SSN' }).toUpperCase() | ||
|
|
||
| export function isValidEmail(email: string): boolean { | ||
| const emailRegex = | ||
| /^[-!#$%&'*+/0-9=?A-Z^_a-z`{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Onko noi aina vain tyhjiä merkkijonoja vai voiko syötteenä olla esim.
' '? Koska siihenhän toiz.literal('')ei osu? Oletan että tää on ok tällaisenaan.