Skip to content
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
23 changes: 12 additions & 11 deletions src/layout/OrganisationLookup/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,28 @@ const orgNrSchema: JSONSchemaType<Pick<Organisation, 'orgNr'>> = {
};

export function checkValidOrgnNr(orgNr: string): boolean {
if (orgNr.length !== 9) {
if (orgNr.length !== 9 || !/^\d{9}$/.test(orgNr)) {
return false;
}
const [a1, a2, a3, a4, a5, a6, a7, a8, a9] = orgNr.split('').map(Number);
const allegedCheckDigit = a9;
const orgnr_digits = orgNr.split('').map(Number);
const k1 = orgnr_digits.at(-1)!;

const [w1, w2, w3, w4, w5, w6, w7, w8] = [3, 2, 7, 6, 5, 4, 3, 2];
const sum = a1 * w1 + a2 * w2 + a3 * w3 + a4 * w4 + a5 * w5 + a6 * w6 + a7 * w7 + a8 * w8;
const weights = [3, 2, 7, 6, 5, 4, 3, 2];

let calculatedCheckDigit = mod11(sum);

if (calculatedCheckDigit === 11) {
calculatedCheckDigit = 0;
let sum = 0;
for (let i = 0; i < weights.length; i++) {
sum += orgnr_digits[i] * weights[i];
}

return calculatedCheckDigit === allegedCheckDigit;
let calculated_k1 = modularAdditiveInverse(sum, 11);
calculated_k1 = calculated_k1 % 11;

return calculated_k1 === k1;
}

export const validateOrgnr = ajv.compile(orgNrSchema);

const mod11 = (value: number): number => 11 - (value % 11);
const modularAdditiveInverse = (value: number, base: number): number => base - (value % base);

const organisationLookupResponseSchema: JSONSchemaType<OrganisationLookupResponse> = {
type: 'object',
Expand Down
18 changes: 7 additions & 11 deletions src/layout/PersonLookup/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export function checkValidSsn(ssn: string): boolean {
}

const digits = ssn.split('').map(Number);
const k1 = digits[9];
const k2 = digits[10];
const k1 = digits.at(-2)!;
const k2 = digits.at(-1)!;

// Calculate first control digit (K1)
const weights1 = [3, 7, 6, 1, 8, 9, 4, 5, 2];
Expand All @@ -47,10 +47,8 @@ export function checkValidSsn(ssn: string): boolean {
sum1 += digits[i] * weights1[i];
}

let calculated_k1 = mod11(sum1);
if (calculated_k1 === 11) {
calculated_k1 = 0;
}
let calculated_k1 = modularAdditiveInverse(sum1, 11);
calculated_k1 = calculated_k1 % 11;

// Calculate second control digit (K2)
const weights2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
Expand All @@ -63,16 +61,14 @@ export function checkValidSsn(ssn: string): boolean {
}
}

let calculated_k2 = mod11(sum2);
if (calculated_k2 === 11) {
calculated_k2 = 0;
}
let calculated_k2 = modularAdditiveInverse(sum2, 11);
calculated_k2 = calculated_k2 % 11;

// Validate controls and return result
return k1 === calculated_k1 && k2 === calculated_k2;
}

const mod11 = (value: number): number => 11 - (value % 11);
const modularAdditiveInverse = (value: number, base: number): number => base - (value % base);

export const validateSsn = ajv.compile(ssnSchema);

Expand Down