# exact-mirror

> Mirror exact value to TypeBox/OpenAPI model

Latest version **1.2.6** (published 2026-09-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install exact-mirror
pnpm add exact-mirror
yarn add exact-mirror
bun add exact-mirror
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.2.6 |
| Published | 2026-09-06 |
| First published | 2025-03-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 40.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 50 |
| Author | saltyAom |
| Maintainers | aomkirby123 |
| Keywords | elysia, exact, mirror, typebox |

## Links

- npm: https://www.npmjs.com/package/exact-mirror
- Repository: https://github.com/elysiajs/exact-mirror
- Issues: https://github.com/elysiajs/exact-mirror/issues
- npm.io page: https://npm.io/package/exact-mirror

## Recent versions

- 1.2.6 (latest) — 2026-09-06
- 1.2.5 — 2026-09-06
- 1.2.4 — 2026-08-06
- 1.2.3 — 2026-08-06
- 1.2.2 — 2026-06-16
- 1.2.1 — 2026-06-15
- 1.2.0 — 2026-06-15
- 1.1.1 — 2026-06-09
- 1.1.0 — 2026-06-09
- 1.0.2 — 2026-06-01
- 1.0.1 — 2026-06-01
- 1.0.0 — 2026-04-05
- 0.2.7 — 2026-02-09
- 0.2.6 — 2026-01-03
- 0.2.5 — 2025-11-26
- … 22 more at https://npm.io/package/exact-mirror/versions

## README

# Exact Mirror

Enforce value to TypeBox/OpenAPI model

By providing model ahead of time, the library will generate a function to mirror a value to an exact type

```
$ bun benchmarks/small

clk: ~3.13 GHz
cpu: Apple M1 Max
runtime: bun 1.2.4 (arm64-darwin)

summary
  Exact Mirror
   556.23x faster than TypeBox Value.Clean
```

## Installation

```bash
# Using either one of the package manager
npm install exact-mirror
yarn add exact-mirror
pnpm add exact-mirror
bun add exact-mirror
```

## Usage

It is designed to be used with [TypeBox](https://github.com/sinclairzx81/typebox) but an OpenAPI schema should also work.

```typescript
import { Type as t } from '@sinclair/typebox'
import { createMirror } from 'exact-mirror'

const shape = t.Object({
	name: t.String(),
	id: t.Number()
})

const value = {
	id: 0,
	name: 'saltyaom',
	// @ts-expect-error
	shoudBeRemoved: true
} satisfies typeof shape.static

const mirror = createMirror(shape)

console.log(mirror(value)) // {"id":0,"name":"saltyaom"}
```

## Decode / Encode

By default `createMirror` only **cleans** a value to the model's shape. Opt into `decode` or `encode` to also apply a TypeBox codec's transform at codec leaves during the same walk — a fast replacement for `Value.Decode` / `Value.Encode` (~350x faster on a nested + array schema).

```typescript
import { Type as t } from 'typebox'
import { Compile } from 'typebox/compile'
import { createMirror } from 'exact-mirror'

const Numeric = t.Union([
	t.Number(),
	t
		.Codec(t.String())
		.Decode((v) => +v)
		.Encode((v) => '' + v)
])

const shape = t.Object({ id: Numeric })

const decode = createMirror(shape, { decode: true, Compile })

decode({ id: '2' }) // { id: 2 } — parsed + cleaned in one pass
decode({ id: 2 }) //  { id: 2 } — already-numeric branch, untouched
```

- `decode: true` applies each codec's `~codec.decode` (parse input).
- `encode: true` applies each codec's `~codec.encode` (the reverse).
- Both default **off** — output is byte-identical to the pure clean.

> Decode/encode is a pure transform: the value is assumed to have already passed `Check`. Validate the input first, then mirror.

---
_Source: https://npm.io/package/exact-mirror · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
