From b06a89d65746e93945b17d55a048d4a96a5e1641 Mon Sep 17 00:00:00 2001 From: Johannes Haukland <42615991+HauklandJ@users.noreply.github.com> Date: Fri, 7 Nov 2025 10:10:34 +0100 Subject: [PATCH] fix: orgnr validation (#3838) --- .../OrganisationLookup/validation.test.ts | 33 +++++++++++++++++++ src/layout/OrganisationLookup/validation.ts | 9 ++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/layout/OrganisationLookup/validation.test.ts b/src/layout/OrganisationLookup/validation.test.ts index 66b50103a9..5781972137 100644 --- a/src/layout/OrganisationLookup/validation.test.ts +++ b/src/layout/OrganisationLookup/validation.test.ts @@ -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); }); diff --git a/src/layout/OrganisationLookup/validation.ts b/src/layout/OrganisationLookup/validation.ts index 0f44271860..b04b8cccea 100644 --- a/src/layout/OrganisationLookup/validation.ts +++ b/src/layout/OrganisationLookup/validation.ts @@ -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 = { type: 'object', oneOf: [