Skip to content

Commit b444dd8

Browse files
authored
validation: add validateObosMembershipNumber() (#20)
1 parent 0e1e89f commit b444dd8

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

.changeset/cool-planes-exercise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@obosbbl/validation": minor
3+
---
4+
5+
feat: add method `validateObosMembershipNumber()` to validate if the input value is a valid OBOS membership number.

packages/validation/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ validateOrganizationNumber('937052766') // => true
2828
validateOrganizationNumber('000') // => false
2929

3030
// 🇸🇪 example
31-
import { validateOrganizationNumer } from '@obosbbl/validation/se';
31+
import { validateOrganizationNumber } from '@obosbbl/validation/se';
3232
validateOrganizationNumber('5592221054') // => true
3333

3434
validateOrganizationNumber('000') // => false
@@ -47,8 +47,10 @@ Note that this currently allows any formatting characters, not just the just the
4747
import { validateOrganizationNumber } from '@obosbbl/validation/no';
4848

4949
validateOrganizationNumber('937052766') // true
50+
5051
// formatting characters disallowed by default
5152
validateOrganizationNumber('937 052 766') // false;
53+
5254
// allow formatting characters
5355
validateOrganizationNumber('937 052 766', { allowFormatting: true }) // true;
5456
```
@@ -60,6 +62,8 @@ validateOrganizationNumber('937 052 766', { allowFormatting: true }) // true;
6062
* supports mobileOnly option
6163
* validateOrganizationNumber
6264
* Check digit verification is currently only implemented for Norwegian organization numbers. For Swedish organiation numbers, we only check the length of the input. PRs are welcome to fix this.
65+
* validateObosMembershipNumber
66+
6367

6468
## Example usage with Zod
6569

packages/validation/src/no.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,22 @@ export function validateOrganizationNumber(
8484
*/
8585
return mod11(value, [3, 2, 7, 6, 5, 4, 3, 2]);
8686
}
87+
88+
/**
89+
* Validates that the input value is an OBOS membership number.
90+
* @example
91+
* ```
92+
* validateObosMembershipNumber('0000000') // => true
93+
* ```
94+
*/
95+
export function validateObosMembershipNumber(
96+
value: string,
97+
options: PostalCodeOptions = {},
98+
): boolean {
99+
if (options.allowFormatting) {
100+
// biome-ignore lint/style/noParameterAssign:
101+
value = stripFormatting(value);
102+
}
103+
104+
return /^\d{7}$/.test(value);
105+
}

packages/validation/src/se.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ export function validateOrganizationNumber(
8383

8484
return /^\d{10}$/.test(value);
8585
}
86+
87+
// just reexport the no method for API feature parity
88+
export { validateObosMembershipNumber } from './no';

packages/validation/src/validation.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ describe('no', () => {
4444
])('validateOrganizationNumber(%s) -> %s', (input, expected, options) => {
4545
expect(no.validateOrganizationNumber(input, options)).toBe(expected);
4646
});
47+
48+
test.each([
49+
['1234567', true, undefined],
50+
// too short
51+
['123456', false, undefined],
52+
// too long
53+
['12345678', false, undefined],
54+
55+
// formatting
56+
['123 45 67', false, { allowFormatting: false }],
57+
['123 45 67', true, { allowFormatting: true }],
58+
])('validateObosMembershipNumber(%s) -> %s', (input, expected, options) => {
59+
expect(no.validateObosMembershipNumber(input, options)).toBe(expected);
60+
});
4761
});
4862

4963
describe('se', () => {
@@ -101,4 +115,18 @@ describe('se', () => {
101115
])('validateOrganizationNumber(%s) -> %s', (input, expected, options) => {
102116
expect(se.validateOrganizationNumber(input, options)).toBe(expected);
103117
});
118+
119+
test.each([
120+
['1234567', true, undefined],
121+
// too short
122+
['123456', false, undefined],
123+
// too long
124+
['12345678', false, undefined],
125+
126+
// formatting
127+
['123 45 67', false, { allowFormatting: false }],
128+
['123 45 67', true, { allowFormatting: true }],
129+
])('validateObosMembershipNumber(%s) -> %s', (input, expected, options) => {
130+
expect(se.validateObosMembershipNumber(input, options)).toBe(expected);
131+
});
104132
});

0 commit comments

Comments
 (0)