JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 41
  • Score
    100M100P100Q64786F
  • License MIT

A small TypeScript library for designing models with built-in validity.

Package Exports

  • self-assert

Readme

self-assert

self-assert logo

Design objects that are fully responsible for their validity

Introduction

self-assert is a small TypeScript library that helps you model domain rules inside your objects — not as external validators, but as collaborators in their own creation.

This library encourages a mindset where rules are expressed in terms of the domain, and objects are created complete, valid, and meaningful from the start.

Installation

Install self-assert with npm:

npm install self-assert

Quick Start

A common workflow is:

  1. Define a single main factory method that validates parameters before creating a new instance of the object.
  2. Use Assertion.requiring(...) to define the rules.
  3. Use Ruleset.ensureAll(...) to execute those rules inside the factory method.
  4. Any additional static creation methods should delegate to the main one.
class Person {
  static readonly nameNotBlank = Assertion.requiring(
    "name.notBlank",
    "Name must not be blank",
    Requirements.isNotBlank
  );

  static named(name: string, ...) {
    Ruleset.ensureAll(this.nameNotBlank.evaluateFor(name), ...otherRules);
    return new this(name, ...);
  }

  protected constructor(protected name: string, ...) {}
}

If any assertion fails, a RulesBroken error is thrown. This ensures your objects are complete and valid from the beginning.

Please refer to the documentation or examples/ for more details and use cases, such as:

  • assistants to guide form completion
  • validating async rules
  • a built-in set of reusable Requirements for common checks

Contributing

Contributions are welcome! See Contributors' Guide for details.

Monorepo Structure

This package is part of the self-assert monorepo.

License

MIT