Skip to content

Overview

Introduction

1918js library contains validation utilities for:

  • PESEL
  • REGON
  • NIP

Each of those modules exposes a simple functional API.

Installation

sh
$ npm add 1918js
sh
$ pnpm add 1918js
sh
$ yarn add 1918js
sh
$ bun add 1918js
sh
$ deno add 1918js

Basic usage

WARNING

Never store polish identifiers as numbers.

This causes issues with leading zeros.

REGON example

INFO

Usage is the same for all identifiers.

Error is always returned. Never thrown.

validate-user-input-example.ts
ts
import { validateRegon } from "1918js"

declare const someUnknownUserInput: unknown;

const result = validateRegon(someUnknownUserInput)

if(result.ok) {
  console.log("happy path :)")
  console.log("our value: ", result.value)
}

if(!result.ok) {
  console.log("you know what path this is :(")

  // error is accessed as a value, never thrown
  console.log("error code", result.error.code)
}

Advanced usage

Zod validator wrapper

zod-validator-example.ts
ts
import z from "zod"
import { validateRegon } from "1918js"

const regonSchema = z.any().superRefine((regonValue, ctx) => {
  const result = validateRegon(regonValue);

  if (!result.ok) {
    ctx.addIssue({
      code: "custom",
      messsage: "REGON is not valid",
    });
  }
});

const myFormSchema = z.object({
  regon: regonSchema,
  // some other fields...
})

Value object wrapper

You can also create your custom value-object wrapper.

value-object-wrapper-example.ts
ts
import { validateRegon } from "1918js"

class Regon {
  #value: string;

  private constructor(regon: string) {
    this.#value = regon;
  }

  static tryParse(regonCandidate: unknown) {
    const result = validateRegon(regonCandidate)

    if (!result.ok) return result;

    return ok(new Regon(result.value))
  }

  asString(): string {
    return this.#value;
  }

  equals(other: unknown): other is Regon {
    if (!(other instanceof Regon)) return false;

    return this.#value === other.#value;
  }
}