diff --git a/README.md b/README.md index d0eab94..e41c514 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/packages/validation/README.md b/packages/validation/README.md index 70e44ce..d378576 100644 --- a/packages/validation/README.md +++ b/packages/validation/README.md @@ -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 @@ -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 diff --git a/packages/validation/src/no.ts b/packages/validation/src/no.ts index 313958f..fe99f4c 100644 --- a/packages/validation/src/no.ts +++ b/packages/validation/src/no.ts @@ -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: