# @alt-point/active-models
**Reactive, self-validating DTO models built on `Proxy` and TypeScript decorators**
Describe a structure once β get validation on write, integrity control, events, change tracking and mapping
into other shapes.
[](https://www.npmjs.com/package/@alt-point/active-models)
[](https://www.npmjs.com/package/@alt-point/active-models)
[](https://bundlephobia.com/package/@alt-point/active-models)
[](https://www.typescriptlang.org/)
[](LICENSE.txt)
[](https://github.com/alt-point/active-models/actions/workflows/ci.yml)
[](https://alt-point.github.io/active-models/en/)
[](docs/en/testing.md)
[](docs/en/testing.md)
[](docs/en/testing.md#mutation-testing)
[](CHANGELOG.md)
[](AGENTS.md)
**[π Documentation](https://alt-point.github.io/active-models/en/)** Β·
**[π Quick start](#quick-start)** Β·
**[π§© Options reference](docs/en/active-field-options.md)** Β·
**[π Migrating to 6.0](docs/en/migration-6.md)** Β·
**[π·πΊ Π ΡΡΡΠΊΠΈΠΉ](README.md)**
---
## Why
Data from APIs, forms, `localStorage` and WebSockets arrives at runtime β and no TypeScript type guarantees
it. `ActiveModel` catches problems at assignment time, not where they finally blow up:
| | Problem | Solution |
|---|---|---|
| π‘οΈ | External data overwrites protected fields | `readonly`, `fillable`, `protected`, `hidden` |
| β
| Invalid values end up in the model | `validator`, `setter`/`getter`, nested `factory` |
| π | You need to know what changed, and when | events `beforeSetValue`, `afterSetValue`, `nulling`, `touched`, `created` |
| π | "Are there unsaved changes?" | `isTouched()`, `getRaw()` |
| π | One model, many representations | `mapTo()` into a DTO / view-model / payload |
| 𧬠| Safe copies and snapshots | `clone()`, `makeFreeze()`, `toJSON()` |
## Quick start
```bash
bun add @alt-point/active-models # or: npm i / yarn add / pnpm add
```
> Requires `"experimentalDecorators": true` in `tsconfig.json` (legacy TypeScript decorators).
> There is no root import: every capability is its own subpath (`/ActiveModel`, `/decorators`, `/scalars/Money`, ...) and
> only what you import is bundled. The list: [Imports and tree-shaking](docs/en/imports.md).
> Native TC39 decorators support is planned.
```ts
import { ActiveModel } from '@alt-point/active-models/ActiveModel'
import { ActiveField } from '@alt-point/active-models/decorators'
import { EventType } from '@alt-point/active-models/types'
class Address extends ActiveModel {
@ActiveField() city: string = ''
}
class User extends ActiveModel {
@ActiveField({ readonly: true }) id: string = ''
@ActiveField({ validator: (_m, prop, v) => { if (!v) throw new TypeError(`${prop} is required`) } })
name: string = 'Guest'
@ActiveField({ hidden: true }) passwordHash: string = ''
@ActiveField({ factory: Address }) address?: Address // a nested model
}
// Always build models through the factory, not `new User(data)`
const user = User.create({ id: '1', name: 'Ann', address: { city: 'Berlin' } }, { tracked: true })
user.on(EventType.afterSetValue, ({ prop, value }) => console.log(prop, 'β', value))
user.name = 'Bob' // "name β Bob"
user.name = '' // TypeError: name is required
user.id = '2' // silently ignored: readonly
user.address!.city = 'Munich' // bubbles up as the parent's touched
user.isTouched() // true β the state differs from the initial one
JSON.stringify(user) // {"id":"1","name":"Bob","address":{"city":"Munich"}} β no passwordHash
```
Class-level subscriptions apply to **every** instance (and subclasses) at once:
```ts
User.on(EventType.created, ({ target }) => audit('user created', (target as User).id))
```
## Features
Integrity and validation
- `@ActiveField({ readonly, fillable, protected, hidden })` β fine-grained control per field;
- `validator`, `setter`, `getter`, defaults (`value`/`attribute`, copied per instance);
- `factory: Model` / `[Model, () => default]` β nested models and lists validated at any depth.
Events
- field hooks: `@ActiveField({ on: {...}, once: {...} })`;
- instance: `model.on()` / `model.once()`; class: `Model.on()` / `Model.once()` (inherited by subclasses);
- `touched` bubbles up from nested models; a throwing listener doesn't stop the others;
- typed payloads: `EventPayloads`, `EventListener`.
Data and copies
- `create` / `createLazy` / `asyncCreate` / `createFromCollection` (+ `Lazy`/`async` variants);
- `fill()`, `clone()`, `makeFreeze()`, `toJSON()` (`Date`, `Set`, `Map` handled correctly);
- `isTouched()` + `getRaw()` β change tracking and the source data (`create(data, { tracked: true })`).
Mapping and utilities
- `Decimal` / `Money` / `LocalDate` + `coerce: Money` β exact money, decimals and timezone-free dates;
- `ActiveCollection` / `ActiveMap` / `ActiveSet` / `@ActiveField({ container })` β an array that holds nothing but the model; sorting and binary search;
- `Model.mapTo(Target, handler)` / `model.mapTo(Target)` β several projections of one model;
- `CallableModel` β objects you can call like functions (no `unsafe-eval`, CSP-safe).
## CallableModel
`CallableModel` is a base class whose instances can be called like functions (handy for Nuxt.js/Vue.js
plugins; built without `extends Function`, so it works under a strict CSP):
```ts
import { CallableModel } from '@alt-point/active-models/CallableModel'
class Notify extends CallableModel {
__call (message: string) { return this.success(message) }
success (message: string) { alert(message) }
silent (message: string) { console.log('Silent message:', message) }
}
// plugin: inject('notify', new Notify()) β this.$notify('Alert!'); this.$notify.silent('...')
```
## Documentation
| Section | About |
|---|---|
| [Decorators example](docs/en/active-model-with-decorators.md) | the basic workflow on an order model |
| [`@ActiveField()` reference](docs/en/active-field-options.md) | every option: what it does and what it affects |
| [Model lifecycle](docs/en/model-lifecycle.md) | creation/write/delete diagrams and every event |
| [Advanced features](docs/en/active-model-advanced.md) | validators, hooks, `new` vs `create` vs `fill`, `clone`, `makeFreeze` |
| [Change tracking](docs/en/dirty-tracking.md) | `isTouched()` and `getRaw()` with Vue and React examples |
| [Model mapping](docs/en/mapping.md) | `mapTo()`: compared to class-transformer and AutoMapper |
| [Invariants, transactions, undo](docs/en/integrity.md) | `transaction()` with rollback, `changes()`/`revert()`, `undo()`/`redo()` |
| [Validation and normalization](docs/en/validation.md) | rules, `validate()`, `coerce`, `trim`, state transitions, strict mode |
| [Scalars](docs/en/scalars.md) | `Decimal`, `Money` (with `allocate()`), `LocalDate`, custom value classes |
| [Comparison with native code](docs/en/comparison.md) | eight tasks: the ActiveModel solution and the same by hand, in tabs |
| [Imports and tree-shaking](docs/en/imports.md) | a subpath per capability, no root import, bundle sizes |
| [ActiveCollection / Map / Set](docs/en/collections.md) | containers of the declared model only: sorting, binary search, unique keys |
| [Node.js example](docs/en/node-example.md) | a server on plain `node:http` |
| [Known limitations](docs/en/limitations.md) | what doesn't work as you'd expect β and what to do |
| [Migrating to 6.0](docs/en/migration-6.md) Β· [to 5.0](docs/en/migration-5.md) Β· [to 4.0](docs/en/migration.md) Β· [CHANGELOG](CHANGELOG.md) | what changed and how to upgrade |
| [Testing and quality](docs/en/testing.md) | coverage, performance budgets, mutation testing |
| [API reference](https://alt-point.github.io/active-models/en/api/) | generated from TSDoc |
## For AI agents
The package ships [`AGENTS.md`](AGENTS.md) β a compact API cheat sheet and the rules that prevent most
mistakes (e.g. "build models with `create()`, never `new`"). There is also an
[`llms.txt`](https://alt-point.github.io/active-models/llms.txt) index. Point your agent at it from your own
`CLAUDE.md`:
```md
Before working with models, read node_modules/@alt-point/active-models/AGENTS.md.
```
## Quality
**547** unit tests Β· **99 %** coverage Β· **95 %** mutation score (Stryker, gate at 90 %) Β· performance budgets
and memory-leak checks Β· ESLint Β· strict [SemVer](CHANGELOG.md#versioning-policy).
```bash
bun run test # unit
bun run test:coverage # + coverage thresholds
bun run test:perf # performance budgets
bun run test:mutation # mutation testing
```
## Contributing
Issues and PRs are welcome. Before a PR: `bun run lint && bun run typecheck && bun run test`. Rules for
contributors and agents live in [`AGENTS.md`](AGENTS.md#contributing-to-this-repo).
## License
[MIT](LICENSE.txt) Β© [alt-point](https://alt-point.com/) Β·
author β [Alex D. Bubenchikov](https://t.me/surrealistik), [surrealistik@alt-point.com](mailto:surrealistik@alt-point.com?subject=ActiveModels)