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
33 changes: 33 additions & 0 deletions src/layout/OrganisationLookup/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@ describe('CheckValidOrgNr', () => {
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('043871668')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('974683520')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('900010605')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('123778847')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('344547211')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('542683430')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('473324261')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('883863631')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('594027922')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('688701473')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('696902453')).toBe(true);
});
it('should return true when the orgNr is valid', () => {
expect(checkValidOrgnNr('899350766')).toBe(true);
});
it('should return false when the orgNr is invalid', () => {
expect(checkValidOrgnNr('143871668')).toBe(false);
});
Expand Down
9 changes: 8 additions & 1 deletion src/layout/OrganisationLookup/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,20 @@ export function checkValidOrgnNr(orgNr: string): boolean {

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 calculatedCheckDigit = 11 - (sum % 11);

let calculatedCheckDigit = mod11(sum);

if (calculatedCheckDigit === 11) {
calculatedCheckDigit = 0;
}

return calculatedCheckDigit === allegedCheckDigit;
}

export const validateOrgnr = ajv.compile(orgNrSchema);

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

const organisationLookupResponseSchema: JSONSchemaType<OrganisationLookupResponse> = {
type: 'object',
oneOf: [
Expand Down
Loading