Skip to content

Commit 7942dc0

Browse files
authored
fix: orgnr validation (#3838)
1 parent e81ca65 commit 7942dc0

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/layout/OrganisationLookup/validation.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@ describe('CheckValidOrgNr', () => {
44
it('should return true when the orgNr is valid', () => {
55
expect(checkValidOrgnNr('043871668')).toBe(true);
66
});
7+
it('should return true when the orgNr is valid', () => {
8+
expect(checkValidOrgnNr('974683520')).toBe(true);
9+
});
10+
it('should return true when the orgNr is valid', () => {
11+
expect(checkValidOrgnNr('900010605')).toBe(true);
12+
});
13+
it('should return true when the orgNr is valid', () => {
14+
expect(checkValidOrgnNr('123778847')).toBe(true);
15+
});
16+
it('should return true when the orgNr is valid', () => {
17+
expect(checkValidOrgnNr('344547211')).toBe(true);
18+
});
19+
it('should return true when the orgNr is valid', () => {
20+
expect(checkValidOrgnNr('542683430')).toBe(true);
21+
});
22+
it('should return true when the orgNr is valid', () => {
23+
expect(checkValidOrgnNr('473324261')).toBe(true);
24+
});
25+
it('should return true when the orgNr is valid', () => {
26+
expect(checkValidOrgnNr('883863631')).toBe(true);
27+
});
28+
it('should return true when the orgNr is valid', () => {
29+
expect(checkValidOrgnNr('594027922')).toBe(true);
30+
});
31+
it('should return true when the orgNr is valid', () => {
32+
expect(checkValidOrgnNr('688701473')).toBe(true);
33+
});
34+
it('should return true when the orgNr is valid', () => {
35+
expect(checkValidOrgnNr('696902453')).toBe(true);
36+
});
37+
it('should return true when the orgNr is valid', () => {
38+
expect(checkValidOrgnNr('899350766')).toBe(true);
39+
});
740
it('should return false when the orgNr is invalid', () => {
841
expect(checkValidOrgnNr('143871668')).toBe(false);
942
});

src/layout/OrganisationLookup/validation.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,20 @@ export function checkValidOrgnNr(orgNr: string): boolean {
4242

4343
const [w1, w2, w3, w4, w5, w6, w7, w8] = [3, 2, 7, 6, 5, 4, 3, 2];
4444
const sum = a1 * w1 + a2 * w2 + a3 * w3 + a4 * w4 + a5 * w5 + a6 * w6 + a7 * w7 + a8 * w8;
45-
const calculatedCheckDigit = 11 - (sum % 11);
45+
46+
let calculatedCheckDigit = mod11(sum);
47+
48+
if (calculatedCheckDigit === 11) {
49+
calculatedCheckDigit = 0;
50+
}
4651

4752
return calculatedCheckDigit === allegedCheckDigit;
4853
}
4954

5055
export const validateOrgnr = ajv.compile(orgNrSchema);
5156

57+
const mod11 = (value: number): number => 11 - (value % 11);
58+
5259
const organisationLookupResponseSchema: JSONSchemaType<OrganisationLookupResponse> = {
5360
type: 'object',
5461
oneOf: [

0 commit comments

Comments
 (0)