npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

kanun

v0.1.3

Published

Framework-agnostic TypeScript-first validation library.

Readme

Kanun

Validation Package Version Downloads Tests License

Framework-agnostic TypeScript-first validation library. Provides a rich set of built-in rules, custom rule support, nested data validation, conditional rules, localized error messages, and a flexible API for validating complex data structures in Node.js applications.

Installation

npm install kanun

Features

  • Rule-based validation — Supports common rules like required, min, max, email, url, numeric, boolean, in, regex, etc.

  • Nested data validation — Dot notation for nested objects (user.email, items.*.price).

  • Custom error messages — Per-rule and per-field message overrides.

  • Batch validation — Validate multiple datasets or groups in one call.

  • Conditional validation — Rules that only apply when other fields meet conditions (required_if, sometimes, exclude_unless).

  • Implicit rules — Rules that run even when attributes are missing (e.g., accepted, required).

  • Custom rules — Define user-provided validation rules as functions or classes.

  • Async rules — Support for async validation (e.g., checking uniqueness in a database).

  • Attribute sanitization — Optional transformation (e.g., trimming, normalizing case) before validation.

  • Dynamic rule sets — Rules can be generated at runtime (e.g., based on user roles).

  • Dependent rules — Rules that reference other fields dynamically.

  • Localized error messages — Built-in support for localization and i18n message templates.

  • Structured errors — Validation errors returned as structured objects or flat key–message pairs.

  • Fail-fast mode — Option to stop at the first failure or collect all errors.

  • Human-readable summaries — Helper for formatting readable validation reports.

  • TypeScript-first design — Full type inference for rules, messages, and validated data.

  • Chainable API — Optional fluent syntax for building validators.

Usage

Quick Start

import { Validator } from 'kanun';

const validator = new Validator(
  { email: '[email protected]', age: 20 },
  { email: 'required|email', age: 'required|numeric|min:18' },
);

const passes = await validator.passes();

if (!passes) {
  console.log(validator.errors().all());
}

Throwing Validation Errors

import { Validator, ValidationException } from 'kanun';

const validator = Validator.make({ email: '' }, { email: 'required|email' });

try {
  const data = await validator.validate();
  console.log(data);
} catch (error) {
  if (error instanceof ValidationException) {
    console.log(error.errors());
  }
}

Custom Messages

const validator = new Validator(
  { email: '' },
  { email: 'required|email' },
  {
    'email.required': 'Email is required.',
    'email.email': 'Email must be valid.',
  },
);

Nested and Array Validation

const validator = new Validator(
  {
    user: { name: { first: 'John', last: '' } },
    users: [{ email: '[email protected]' }, { email: 'bad' }],
  },
  {
    'user.name.first': 'required|min:2',
    'user.name.last': 'required|min:2',
    'users.*.email': 'required|email',
  },
);

Custom Rule Class

import { ValidationRule, Validator } from 'kanun';

class StartsWithKanun extends ValidationRule {
  validate(
    attribute: string,
    value: any,
    fail: (message: string) => void,
  ): void {
    if (typeof value !== 'string' || !value.startsWith('kanun')) {
      fail(`The ${attribute} must start with kanun.`);
    }
  }
}

const validator = new Validator(
  { slug: 'kanun' },
  { slug: ['required', new StartsWithKanun()] },
);

Database-backed exists and unique

import { IDatabaseDriver, Validator } from 'kanun';

class AppDatabaseDriver extends IDatabaseDriver {
  async exists({ table, column, value, ignore }) {
    const row = await db.table(table).where(column, value).first();
    if (!row) return false;
    if (ignore != null && String(row.id) === String(ignore)) return false;
    return true;
  }
}

const driver = new AppDatabaseDriver();

Validator.useDatabase(driver);

const validator = new Validator(
  { username: 'legacy' },
  { username: 'unique:users,username' },
);

For the complete guide and API reference, visit the docs site: https://arcstack-hq.github.io/kanun

Code of Conduct

In order to ensure that the Kanun community is welcoming to all, please review and abide by the Code of Conduct.

Security Vulnerabilities

If you discover a security vulnerability within Kanun, please send an e-mail to Legacy via [email protected]. All security vulnerabilities will be promptly addressed.

License

Kanun and all Arcstack packages are open source and licensed under the MIT license.