Package Exports
- self-assert
Readme
self-assert
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:
- Define a single main factory method that validates parameters before creating a new instance of the object.
- Use
Assertion.requiring(...)
to define the rules. - Use
Ruleset.ensureAll(...)
to execute those rules inside the factory method. - 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.