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
5 changes: 5 additions & 0 deletions .changeset/cool-planes-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@obosbbl/validation": minor
---

feat: add method `validateObosMembershipNumber()` to validate if the input value is a valid OBOS membership number.
6 changes: 5 additions & 1 deletion packages/validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ validateOrganizationNumber('937052766') // => true
validateOrganizationNumber('000') // => false

// 🇸🇪 example
import { validateOrganizationNumer } from '@obosbbl/validation/se';
import { validateOrganizationNumber } from '@obosbbl/validation/se';
validateOrganizationNumber('5592221054') // => true

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

validateOrganizationNumber('937052766') // true

// formatting characters disallowed by default
validateOrganizationNumber('937 052 766') // false;

// allow formatting characters
validateOrganizationNumber('937 052 766', { allowFormatting: true }) // true;
```
Expand All @@ -60,6 +62,8 @@ validateOrganizationNumber('937 052 766', { allowFormatting: true }) // true;
* supports mobileOnly option
* validateOrganizationNumber
* 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.
* validateObosMembershipNumber


## Example usage with Zod

Expand Down
19 changes: 19 additions & 0 deletions packages/validation/src/no.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,22 @@ export function validateOrganizationNumber(
*/
return mod11(value, [3, 2, 7, 6, 5, 4, 3, 2]);
}

/**
* Validates that the input value is an OBOS membership number.
* @example
* ```
* validateObosMembershipNumber('0000000') // => true
* ```
*/
export function validateObosMembershipNumber(
value: string,
options: PostalCodeOptions = {},
): boolean {
if (options.allowFormatting) {
// biome-ignore lint/style/noParameterAssign:
value = stripFormatting(value);
}

return /^\d{7}$/.test(value);
}
3 changes: 3 additions & 0 deletions packages/validation/src/se.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ export function validateOrganizationNumber(

return /^\d{10}$/.test(value);
}

// just reexport the no method for API feature parity
export { validateObosMembershipNumber } from './no';
28 changes: 28 additions & 0 deletions packages/validation/src/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ describe('no', () => {
])('validateOrganizationNumber(%s) -> %s', (input, expected, options) => {
expect(no.validateOrganizationNumber(input, options)).toBe(expected);
});

test.each([
['1234567', true, undefined],
// too short
['123456', false, undefined],
// too long
['12345678', false, undefined],

// formatting
['123 45 67', false, { allowFormatting: false }],
['123 45 67', true, { allowFormatting: true }],
])('validateObosMembershipNumber(%s) -> %s', (input, expected, options) => {
expect(no.validateObosMembershipNumber(input, options)).toBe(expected);
});
});

describe('se', () => {
Expand Down Expand Up @@ -101,4 +115,18 @@ describe('se', () => {
])('validateOrganizationNumber(%s) -> %s', (input, expected, options) => {
expect(se.validateOrganizationNumber(input, options)).toBe(expected);
});

test.each([
['1234567', true, undefined],
// too short
['123456', false, undefined],
// too long
['12345678', false, undefined],

// formatting
['123 45 67', false, { allowFormatting: false }],
['123 45 67', true, { allowFormatting: true }],
])('validateObosMembershipNumber(%s) -> %s', (input, expected, options) => {
expect(se.validateObosMembershipNumber(input, options)).toBe(expected);
});
});