|
| 1 | +# @obosbbl/validation |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@obosbbl/validation) |
| 4 | + |
| 5 | + |
| 6 | +A collection of validation methods 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 { validateOrganizationNumber } from '@obosbbl/validation/no'; |
| 26 | +validateOrganizationNumber('937052766') // => true |
| 27 | + |
| 28 | +validateOrganizationNumber('000') // => false |
| 29 | + |
| 30 | +// 🇸🇪 example |
| 31 | +import { validateOrganizationNumber } from '@obosbbl/validation/se'; |
| 32 | +validateOrganizationNumber('5592221054') // => true |
| 33 | + |
| 34 | +validateOrganizationNumber('000') // => false |
| 35 | +``` |
| 36 | + |
| 37 | +## Strictness and formatting characters |
| 38 | + |
| 39 | +The methods are "strict" by default, meaning no formatting characters in the input is allowed. |
| 40 | +This is preferrable, for instance when doing server-side validation, where the input is often expected to be a "clean" value. |
| 41 | + |
| 42 | +If you want to allow formatting characters in the input, you can pass `allowFormatting: true` in the options object to the method. |
| 43 | +Note that this currently allows any formatting characters, not just the just the "expected" ones for the input type. |
| 44 | + |
| 45 | + |
| 46 | +```js |
| 47 | +import { validateOrganizationNumber } from '@obosbbl/validation/no'; |
| 48 | + |
| 49 | +validateOrganizationNumber('937052766') // true |
| 50 | + |
| 51 | +// formatting characters disallowed by default |
| 52 | +validateOrganizationNumber('937 052 766') // false; |
| 53 | + |
| 54 | +// allow formatting characters |
| 55 | +validateOrganizationNumber('937 052 766', { allowFormatting: true }) // true; |
| 56 | +``` |
| 57 | + |
| 58 | +## Methods |
| 59 | + |
| 60 | +* validatePostalCode |
| 61 | +* validatePhoneNumber |
| 62 | + * supports mobileOnly option |
| 63 | +* validateOrganizationNumber |
| 64 | + * 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. |
| 65 | +* validateObosMembershipNumber |
| 66 | + |
| 67 | + |
| 68 | +## Example usage with Zod |
| 69 | + |
| 70 | +```js |
| 71 | +import { z } from 'zod'; |
| 72 | +import { validatePhoneNumber } from '@obosbbl/validation/no'; |
| 73 | + |
| 74 | +const mobileOnlySchema = z.object({ |
| 75 | + name: z.string(), |
| 76 | + phoneNumber: z |
| 77 | + .string() |
| 78 | + .refine( |
| 79 | + (val) => validatePhoneNumber(val, { mobileOnly: true }), |
| 80 | + 'Telefonnummeret er ikke et gyldig mobilnummer', |
| 81 | + ), |
| 82 | +}); |
| 83 | + |
| 84 | +const validData = { |
| 85 | + name: 'Kari Nordmann', |
| 86 | + phoneNumber: '92345678', |
| 87 | +}; |
| 88 | + |
| 89 | +mobileOnlySchema.parse(validData); // => { name: 'Kari Nordmann', phoneNumber: '92345678' } |
| 90 | + |
| 91 | +const invalidData = { |
| 92 | + name: 'Ola Nordmann', |
| 93 | + phoneNumber: '22865500', |
| 94 | +} |
| 95 | + |
| 96 | +mobileOnlySchema.parse(invalidData); // => throws ZodError |
| 97 | +``` |
0 commit comments