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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ This a monorepo of OBOS' open source frontend modules.

## Packages

* [format](./packages/format)
* [validation](./packages/validation)
* [format](./packages/format) - A collection of formatting methods for 🇳🇴 and 🇸🇪.
* [validation](./packages/validation) - A collection of validation methods for 🇳🇴 and 🇸🇪.

## Contributing

Expand Down Expand Up @@ -34,4 +34,4 @@ pnpm build

### Releases and changelogs

We use an automated release process based on [changesets](https://github.com/changesets/changesets) and a [Github action](./.github/workflows/release.yml) to version, release and publish the packages. Meaningful changes should be documented by running pnpm changeset and be a part of the pull request. Remember to follow semver.
We use an automated release process based on [changesets](https://github.com/changesets/changesets) and a [Github action](./.github/workflows/release.yml) to version, release and publish the packages. Meaningful changes should be documented by running `pnpm changeset` and be a part of the pull request. Remember to follow semver.
113 changes: 103 additions & 10 deletions packages/validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ validateOrganizationNumber('000') // => false

## Strictness and formatting characters

The methods are "strict" by default, meaning no formatting characters in the input is allowed.
This is preferrable, for instance when doing server-side validation, where the input is often expected to be a "clean" value.
The methods are "strict" by default, meaning no formatting characters in the input is allowed. This even applies to separators such as in Swedish national identity numbers.

If you want to allow formatting characters in the input, you can pass `allowFormatting: true` in the options object to the method.
When doing server-side validation, for instance, before insertion into a database, strictness is often preferrable. The value is often expected to be a "clean" value in standardized format.

On the client side, formatting characters could be allowed, as they are more user-friendly, for instance, allowing the user to input their phone number in their preferred format.

If you want to allow formatting characters in the value, you can pass `allowFormatting: true` in the options object to the method.


```js
Expand All @@ -56,13 +59,103 @@ validateOrganizationNumber('937 052 766', { allowFormatting: true }) // true;

## Methods

* validateNationalIdentityNumber
* validatePostalCode
* validatePhoneNumber
* 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
### validateNationalIdentityNumber()

Validates that the value is a valid national identity number.

Validation is done for the both checksum and if the date is a valid date.
It accepts both fødselsnummer and d-nummer for Norway, and personnummer and samordningsnummer for Sweden.

By default, both the short (10 digit) and long (12 digit) format is allowed for Sweden. You can use the `format` option to specify the format to validate.

```js
// 🇳🇴 example
import { validateNationalIdenityNumber } from '@obosbbl/validation/no';
validateNationalIdenityNumber('DDMMYYXXXX') // => true

// 🇸🇪 example
import { validateNationalIdentityNumber } from "@obosbbl/validation/se";
// short
validatePersonalIdentityNumber('YYMMDDXXXX') // => true
// long
validateOrganizationNumber('YYYYMMDDXXXX'') // => true

// separator (formatting) is important for Swedish national identity numbers
validatePersonalIdentityNumber('YYMMDD-XXXX', { allowFormatting: true }) // => true
validateOrganizationNumber('YYYY-MMDDXXXX', { allowFormatting: true })) // => true

// specific format
validatePersonalIdentityNumber('YYMMDDXXXX', { format: 'short' }) // => true
validatePersonalIdentityNumber('YYMMDDXXXX', { format: 'long' }) // => false

validatePersonalIdentityNumber('YYYYMMDDXXXX', { format: 'long' }) // => true
```

> [!TIP]
> Did you know that you cannot assume that the date in the number is person's date of birth? See [Skatteetaten fødselsnummer](https://www.skatteetaten.no/person/folkeregister/identitetsnummer/fodselsnummer/).

### validatePhoneNumber()

Validates that the value is a valid phone number. Specify `mobileOnly` to only allow mobile numbers.

```js
// 🇳🇴 example
import { validatePhoneNumber } from '@obosbbl/validation/no';
validatePhoneNumber('00000000') // => true
validatePhoneNumber('90000000', { mobileOnly: true }) // => true

// 🇸🇪 example
import { validatePhoneNumber } from '@obosbbl/validation/se';
validatePhoneNumber('00000000') // => true
validatePhoneNumber('000000000') // => true
validatePhoneNumber('0000000000') // => true
validatePhoneNumber('0700000000', { mobileOnly: true }) // => true
```

### validatePostalCode()

Validates that the value is a valid postal code.

```js
// 🇳🇴 example
import { validatePostalCode } from '@obosbbl/validation/no';
validatePostalCode('0000') // => true

// 🇸🇪 example
import { validatePostalCode } from '@obosbbl/validation/se';
validatePostalCode('00000') // => true
```

### validateOrganizationNumber()

Validates that the value is a valid organization number. Validates the checksum of the number.

```js
// 🇳🇴 example
import { validateOrganizationNumber } from '@obosbbl/validation/no';
validateOrganizationNumber('937052766') // => true

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

### validateObosMembershipNumber()

Validates that the value is a valid OBOS membership number.

> [!NOTE]
> There is no difference between a Norwegian and Swedish OBOS membership number. The method in use is in fact the same one, re-exported for the different locales.

```js
// 🇳🇴 example
import { validateObosMembershipNumber } from '@obosbbl/validation/no';
validateObosMembershipNumber('0000000') // => true

// 🇸🇪 example
import { validateObosMembershipNumber } from '@obosbbl/validation/se';
validateObosMembershipNumber('0000000') // => true
```


## Example usage with Zod
Expand Down
2 changes: 1 addition & 1 deletion packages/validation/src/no.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type OrganizationNumberOptions = ValidatorOptions;
*/
export function validateOrganizationNumber(
value: string,
options: PhoneNumberOptions = {},
options: OrganizationNumberOptions = {},
): boolean {
if (options.allowFormatting) {
// biome-ignore lint/style/noParameterAssign:
Expand Down