File tree Expand file tree Collapse file tree 8 files changed +120
-0
lines changed Expand file tree Collapse file tree 8 files changed +120
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ This a monorepo of OBOS' open source frontend modules.
66## Packages
77
88* [ format] ( ./packages/format )
9+ * [ validation] ( ./packages/validation )
910
1011## Contributing
1112
Original file line number Diff line number Diff line change 1+ # @obosbbl/validation
2+
3+ [ ![ NPM Version] ( https://img.shields.io/npm/v/%40obosbbl%2Fvalidation )] ( https://www.npmjs.com/package/@obosbbl/validation )
4+
5+
6+ A collection of validation utilities for both 🇳🇴 and 🇸🇪 with zero dependencies.
7+
8+ ## Install
9+
10+ ``` sh
11+ # npm
12+ npm install @obosbbl/validation
13+
14+ # pnpm
15+ pnpm add @obosbbl/validation
16+ ```
17+
18+ ## Usage
19+
20+ The package has two entrypoints, one for ` no ` and one for ` se ` . That allows you to import for only the locale you need.
21+
22+
23+ ``` js
24+ // 🇳🇴 example
25+ import { postalCodeValidator } from ' @obosbbl/validation/no' ;
26+ postalCodeValidator (' 0000' ) // => true
27+ postalCodeValidator (' 00000' ) // => false
28+
29+ // 🇸🇪 example
30+ import { postalCodeValidator } from ' @obosbbl/validation/no' ;
31+ postalCodeValidator (' 00000' ) // => true
32+ postalCodeValidator (' 00 000' ) // => true
33+ postalCodeValidator (' 000 000' ) // => false
34+ ```
35+
36+ ## Methods
37+
38+ * validatePostalCode
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " @obosbbl/validation" ,
3+ "version" : " 0.0.0" ,
4+ "description" : " A collection of validation methods for OBOS" ,
5+ "repository" : {
6+ "url" : " https://github.com/code-obos/public-frontend-modules"
7+ },
8+ "license" : " MIT" ,
9+ "sideEffects" : false ,
10+ "type" : " module" ,
11+ "exports" : {
12+ "./no" : {
13+ "types" : " ./dist/no.d.mts" ,
14+ "default" : " ./dist/no.mjs"
15+ },
16+ "./se" : {
17+ "types" : " ./dist/se.d.mts" ,
18+ "default" : " ./dist/se.mjs"
19+ }
20+ },
21+ "files" : [
22+ " dist"
23+ ],
24+ "scripts" : {
25+ "build" : " bunchee"
26+ },
27+ "dependencies" : {}
28+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Validates the input value as a valid Norwegian postal (zip) code.
3+ * Valid format is `0000`.
4+ */
5+ export function postalCodeValidator ( value : string ) : boolean {
6+ return / ^ \d { 4 } $ / . test ( value ) ;
7+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Validates the input value as a valid Swedish postal (zip) code.
3+ * Valid format is either `00 000` or `00000`.
4+ */
5+ export function postalCodeValidator ( value : string ) : boolean {
6+ return / ^ \d { 3 } ? \d { 2 } $ / . test ( value ) ;
7+ }
Original file line number Diff line number Diff line change 1+ import { describe , expect , test } from 'vitest' ;
2+ import { postalCodeValidator as postalCodeValidatorNo } from './no' ;
3+ import { postalCodeValidator as postalCodeValidatorSe } from './se' ;
4+
5+ describe ( 'Norwegian' , ( ) => {
6+ describe ( 'postalCodeValidator()' , ( ) => {
7+ test ( 'correctly validates postal codes' , ( ) => {
8+ expect ( postalCodeValidatorNo ( '1067' ) ) . toBeTruthy ( ) ;
9+
10+ expect ( postalCodeValidatorNo ( '1.67' ) ) . toBeFalsy ( ) ;
11+ expect ( postalCodeValidatorNo ( '10677' ) ) . toBeFalsy ( ) ;
12+ expect ( postalCodeValidatorNo ( Number . NaN . toString ( ) ) ) . toBeFalsy ( ) ;
13+ expect ( postalCodeValidatorNo ( 'not a number' ) ) . toBeFalsy ( ) ;
14+ } ) ;
15+ } ) ;
16+ } ) ;
17+
18+ describe ( 'Swedish' , ( ) => {
19+ describe ( 'postalCodeValidator()' , ( ) => {
20+ test ( 'correctly validates postal codes' , ( ) => {
21+ expect ( postalCodeValidatorSe ( '100 26' ) ) . toBeTruthy ( ) ;
22+ expect ( postalCodeValidatorSe ( '10026' ) ) . toBeTruthy ( ) ;
23+
24+ expect ( postalCodeValidatorSe ( '100426' ) ) . toBeFalsy ( ) ;
25+ expect ( postalCodeValidatorSe ( '177' ) ) . toBeFalsy ( ) ;
26+ expect ( postalCodeValidatorSe ( Number . NaN . toString ( ) ) ) . toBeFalsy ( ) ;
27+ expect ( postalCodeValidatorSe ( 'not a number' ) ) . toBeFalsy ( ) ;
28+ } ) ;
29+ } ) ;
30+ } ) ;
Original file line number Diff line number Diff line change 1+ {
2+ "extends" : " ../../tsconfig.json" ,
3+ "include" : [" ./src/*" ],
4+ "compilerOptions" : {
5+ "outDir" : " dist"
6+ }
7+ }
You can’t perform that action at this time.
0 commit comments