# nspect

> Composable validation with same shape value and schema

Latest version **0.2.0** (published 2026-11-27) · ISC license · 0 weekly downloads

## Install

```sh
npm install nspect
pnpm add nspect
yarn add nspect
bun add nspect
```

## Health

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

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

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2026-11-27 |
| First published | 2024-03-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 95.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | David Savoie |
| Maintainers | davidsavoie1 |
| Keywords | spec, schema, validation, composition, ensure, conform, validate, predicates |

## Links

- npm: https://www.npmjs.com/package/nspect
- Repository: https://github.com/davidsavoie1/nspect
- Homepage: https://davidsavoie1.github.io/nspect
- Issues: https://github.com/davidsavoie1/nspect/issues
- npm.io page: https://npm.io/package/nspect

## Dependencies (1)

- [fast-equals](https://npm.io/package/fast-equals.md) ^6.4.1

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 0.2.0 (latest) — 2026-11-27
- 0.1.1 — 2024-04-29
- 0.1.0 — 2024-04-29
- 0.0.5 — 2024-03-29
- 0.0.4 — 2024-03-28
- 0.0.3 — 2024-03-26
- 0.0.2 — 2024-03-18
- 0.0.1 — 2024-03-18

## README

> **AI-generated documentation.** This page was initially written by an AI and may be revised manually. It is published as-is for transparency.

# NSpect

NSpect is a composable validation library for JavaScript data. A specification has the same shape as the value it validates, so rules remain close to the fields they describe and can be combined without introducing a separate schema language.

NSpect supports synchronous and asynchronous predicates, objects, arrays and maps, conditional rules, partial validation, structured error reports, and incremental revalidation. Its React, Vue, and Svelte integrations connect existing forms to an inspector.

## Why NSpect?

The same data often goes through several workflows. A profile editor validates the fields the user changes. Account creation also requires certain fields. The server may add an asynchronous check before saving. NSpect lets all three workflows share the same nested spec.

- **Extend a complete spec with `and()`.** Add rules at any depth. When two specs define the same field, both rules apply, and sibling rules carry over.
- **Choose required fields per workflow.** A field's spec describes what makes its value valid. A separate `required` tree decides whether it must be supplied. An omitted or `undefined` field skips its predicate unless a presence rule requires it. Explicit values such as `null` and `""` are still subject to the rules.
- **Let the caller decide how to display failures.** Only `true` passes. A predicate can return a message, a translation key, or an object containing a key and interpolation parameters. Falsy results get a default reason.

This design carries forward an idea from Clojure Spec, by way of Specma. Value rules can be composed and reused independently of which keys a workflow requires. [Design rationale and profile example](https://github.com/davidsavoie1/nspect/blob/main/docs/rationale.md)

## Installation

```bash
npm install nspect
```

```js
import { inspect } from "nspect";

const spec = {
  name: (value) => value?.trim().length > 0 || "must not be empty",
  age: (value) => value >= 18 || "must be at least 18",
};

const inspection = inspect({
  spec,
  required: { name: true, age: true },
  value: { name: "Ada", age: 17 },
});

inspection.valid; // false
inspection.error.pathname; // "age"
inspection.error.message; // "age must be at least 18"
```

`inspect()` returns an inspection immediately when all rules are synchronous. It returns a promise only when validation encounters an asynchronous predicate.

## Choose an entry point

- Use `inspect()` when you need detailed errors.
- Use `inspectSync()` when asynchronous validation must be rejected.
- Use `isValid()` for a boolean answer.
- Use `check()` for `true` or the first failure message.
- Use `conform()` to return the valid value or throw an `Error`.
- Use `inspector()` for forms and other long-lived values that benefit from subscriptions, activation, submission, and incremental revalidation.

## Documentation

- [Getting started](https://github.com/davidsavoie1/nspect/blob/main/docs/getting-started.md)
- [Design rationale](https://github.com/davidsavoie1/nspect/blob/main/docs/rationale.md)
- [Specifications and predicates](https://github.com/davidsavoie1/nspect/blob/main/docs/specifications-and-predicates.md)
- [Presence rules and selections](https://github.com/davidsavoie1/nspect/blob/main/docs/presence-and-selection.md)
- [Inspection and error results](https://github.com/davidsavoie1/nspect/blob/main/docs/inspection-results.md)
- [Stateful inspectors](https://github.com/davidsavoie1/nspect/blob/main/docs/stateful-inspectors.md)
- [Svelte, React, and Vue integrations](https://github.com/davidsavoie1/nspect/blob/main/docs/framework-integrations.md)
- [Specma compatibility adapter](https://github.com/davidsavoie1/nspect/blob/main/docs/specma-compatibility.md)
- [API reference](https://github.com/davidsavoie1/nspect/blob/main/docs/api-reference.md)
- [Changelog](https://github.com/davidsavoie1/nspect/blob/main/CHANGELOG.md)

## Core model

Validation is described by three independent trees:

- `spec` describes whether present values are valid;
- `required` describes which values must be present and which empty values are accepted;
- `selection` limits validation and projection to a subset of the value.

Predicates return `true` when valid. Any other result signals invalidity. If a predicate throws or rejects during inspection, NSpect reports a validation error using the first non-blank string from the error's `reason` or `message`, or the configured `failed` fallback. NSpect also logs the original error when `console.error` is available.

## Supported collection shapes

Specs, presence rules, and selections can follow JavaScript objects, arrays, and `Map` instances. Use `spread()` to define a fallback rule for every entry in a collection and `key()` to validate entry keys rather than their values.

## Package entry points

```js
import { inspect, inspector } from "nspect";
import * as specma from "nspect/specma";
```

## Framework integrations

Each framework entry point exports `useFormInspector` to validate an existing form draft and `useInspector` to observe an inspector you already manage.

| Framework | Entry point     | Connect a form                                       | Read its cursor                   |
| --------- | --------------- | ---------------------------------------------------- | --------------------------------- |
| React     | `nspect/react`  | `useFormInspector(draft, options)`                   | `form.cursor`                     |
| Vue       | `nspect/vue`    | `useFormInspector(() => draft.value, () => options)` | `form.cursor.value` in JavaScript |
| Svelte 5  | `nspect/svelte` | `useFormInspector(() => draft, () => options)`       | `$form`                           |

All three form helpers expose the inspector methods. Call `form.submit()` to validate and submit the current draft. [Framework integration guide](https://github.com/davidsavoie1/nspect/blob/main/docs/framework-integrations.md)

## License

[ISC](https://github.com/davidsavoie1/nspect/blob/main/LICENSE)

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