Skip to content

Commit 92eee4c

Browse files
committed
validation package
1 parent 8c429fb commit 92eee4c

File tree

5 files changed

+213
-120
lines changed

5 files changed

+213
-120
lines changed

packages/validator/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![NPM Version](https://img.shields.io/npm/v/%40obosbbl%2Fvalidation)](https://www.npmjs.com/package/@obosbbl/validation)
44

55

6-
A collection of validation utilities for both 🇳🇴 and 🇸🇪 with zero dependencies. Integrates neatly with [Zod](https://github.com/colinhacks/zod).
6+
A collection of validation methods for both 🇳🇴 and 🇸🇪 with zero dependencies. Integrates neatly with [Zod](https://github.com/colinhacks/zod).
77

88
## Install
99

@@ -24,17 +24,35 @@ The package has two entrypoints, one for `no` and one for `se`. That allows you
2424
// 🇳🇴 example
2525
import { postalCodeValidator } from '@obosbbl/validation/no';
2626
postalCodeValidator('0000') // => true
27+
2728
postalCodeValidator('00000') // => false
2829

2930
// 🇸🇪 example
3031
import { postalCodeValidator } from '@obosbbl/validation/no';
3132
postalCodeValidator('00000') // => true
3233
postalCodeValidator('00 000') // => true
34+
3335
postalCodeValidator('000 000') // => false
3436
```
3537

38+
## Strictness
39+
40+
By default, the methods allows formatting characters when validating. If you need strict validation, you can pass the `strict` option to the method.
41+
42+
```js
43+
import { validateOrganizationNumber } from '@obosbbl/validation/no';
44+
45+
validateOrganizationNumber('937 052 766') // true;
46+
47+
validateOrganizationNumber('937052766', { strict: true }) // true;
48+
validateOrganizationNumber('937 052 766', { strict: true }) // false;
49+
```
50+
51+
```js
52+
3653
## Methods
3754

3855
* validatePostalCode
3956
* validatePhoneNumber
57+
* supports mobileOnly option
4058
* validateOrganizationNumber

packages/validator/src/no.ts

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
1+
import type { ValidatorOptions } from './types';
12
import { mod11, stripFormatting } from './utils';
23

3-
type PostalCodeOptions = {
4-
/**
5-
* Disallow formatting characters
6-
* @default false
7-
*/
8-
strict?: boolean;
9-
};
4+
type PostalCodeOptions = ValidatorOptions;
105

116
/**
12-
* Validates the input value as a valid Norwegian postal (zip) code.
13-
* Valid format is `0000`.
7+
* Validates that the input value is a Norwegian postal (zip) code.
8+
* @example
9+
* ```
10+
* validatePostalCode('0000') // => true
11+
* ```
1412
*/
1513
export function validatePostalCode(
1614
value: string,
1715
options: PostalCodeOptions = {},
1816
): boolean {
19-
if (!options.strict) {
17+
if (options.allowFormatting) {
2018
value = stripFormatting(value);
2119
}
2220

2321
return /^\d{4}$/.test(value);
2422
}
2523

26-
type PhoneNumberOptions = {
24+
type PhoneNumberOptions = ValidatorOptions & {
2725
/**
2826
* Whether it should be a mobile number
2927
* @default false
3028
*/
3129
mobileOnly?: boolean;
32-
/**
33-
* Disallow formatting characters
34-
* @default false
35-
*/
36-
strict?: boolean;
3730
};
3831

32+
/**
33+
* Validates that the input value is a Norwegian phone number.
34+
*
35+
* Supports mobile only validation.
36+
* @example
37+
* ```
38+
* validatePhoneNumber('00000000') // => true
39+
* validatePhoneNumber('90000000', { mobileOnly: true }) // => true
40+
* ```
41+
*/
3942
export function validatePhoneNumber(
4043
value: string,
4144
options: PhoneNumberOptions = {},
4245
): boolean {
43-
if (!options.strict) {
46+
if (options.allowFormatting) {
4447
value = stripFormatting(value);
4548
}
4649

@@ -55,11 +58,23 @@ export function validatePhoneNumber(
5558
return isPhoneNumber;
5659
}
5760

61+
type OrganizationNumberOptions = ValidatorOptions;
62+
5863
/**
59-
* Validates the input value as a valid {@link https://www.brreg.no/om-oss/registrene-vare/om-enhetsregisteret/organisasjonsnummeret/ Norwegian organization number}.
60-
* Valid format is 9 digits, spaces allowed, eg `000000000` or `000 000 000`.
64+
* Validates that the input value is a {@link https://www.brreg.no/om-oss/registrene-vare/om-enhetsregisteret/organisasjonsnummeret/ Norwegian organization number}.
65+
* @example
66+
* ```
67+
* validateOrganizationNumber('000000000') // => true
68+
* ```
6169
*/
62-
export function validateOrganizationNumber(value: string): boolean {
70+
export function validateOrganizationNumber(
71+
value: string,
72+
options: PhoneNumberOptions = {},
73+
): boolean {
74+
if (options.allowFormatting) {
75+
value = stripFormatting(value);
76+
}
77+
6378
/** References:
6479
* https://www.brreg.no/om-oss/registrene-vare/om-enhetsregisteret/organisasjonsnummeret/
6580
* https://no.wikipedia.org/wiki/Organisasjonsnummer

packages/validator/src/se.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
1+
import type { ValidatorOptions } from './types';
12
import { stripFormatting } from './utils';
23

3-
type PostalCodeOptions = {
4-
/**
5-
* Disallow formatting characters
6-
* @default false
7-
*/
8-
strict?: boolean;
9-
};
4+
type PostalCodeOptions = ValidatorOptions;
105

116
/**
12-
* Validates the input value as a valid Swedish postal (zip) code.
13-
* Valid format is either `00 000` or `00000`.
7+
* Validates that the input value is a Swedish postal (zip) code.
8+
* @example
9+
* ```
10+
* validatePostalCode('00000') // => true
11+
* ```
1412
*/
1513
export function validatePostalCode(
1614
value: string,
1715
options: PostalCodeOptions = {},
1816
): boolean {
19-
if (!options.strict) {
17+
if (options.allowFormatting) {
2018
value = stripFormatting(value);
2119
}
2220

2321
return /^\d{3} ?\d{2}$/.test(value);
2422
}
2523

26-
type PhoneNumberOptions = {
24+
type PhoneNumberOptions = ValidatorOptions & {
25+
/**
26+
* Whether it should be a mobile number
27+
* @default false
28+
*/
2729
mobileOnly?: boolean;
28-
strict?: boolean;
2930
};
3031

32+
/**
33+
* Validates that the input value is a Swedish phone number.
34+
*
35+
* Supports mobile only validation.
36+
* @example
37+
* ```
38+
* validatePhoneNumber('00000000') // => true
39+
* validatePhoneNumber('000000000') // => true
40+
* validatePhoneNumber('0000000000') // => true
41+
* validatePhoneNumber('0700000000', { mobileOnly: true }) // => true
42+
* ```
43+
*/
3144
export function validatePhoneNumber(
3245
value: string,
3346
options: PhoneNumberOptions = {},
3447
): boolean {
35-
if (!options.strict) {
48+
if (options.allowFormatting) {
3649
value = stripFormatting(value);
3750
}
3851

@@ -46,11 +59,21 @@ export function validatePhoneNumber(
4659
return isPhoneNumber;
4760
}
4861

62+
type OrganizationNumberOptions = ValidatorOptions;
63+
64+
/**
65+
* Validates that the input value is a {@link https://www.skatteverket.se/foretagochorganisationer/foretagare/startaochregistrera/organisationsnummer.4.361dc8c15312eff6fd235d1.html Swedish organization number}.
66+
* @example
67+
* ```
68+
* validateOrganizationNumber('000000000') // => true
69+
* ```
70+
*/
4971
export function validateOrganizationNumber(
5072
value: string,
51-
options: PhoneNumberOptions = {},
73+
options: OrganizationNumberOptions = {},
5274
): boolean {
53-
if (!options.strict) {
75+
// TODO: Implement proper validation
76+
if (options.allowFormatting) {
5477
value = stripFormatting(value);
5578
}
5679

packages/validator/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type ValidatorOptions = {
2+
/**
3+
* Allow formatting characters
4+
* @default false
5+
*/
6+
allowFormatting?: boolean;
7+
};

0 commit comments

Comments
 (0)