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
requiredtree decides whether it must be supplied. An omitted orundefinedfield skips its predicate unless a presence rule requires it. Explicit values such asnulland""are still subject to the rules. - Let the caller decide how to display failures. Only
truepasses. 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
Installation
npm install nspect
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()fortrueor the first failure message. - Use
conform()to return the valid value or throw anError. - Use
inspector()for forms and other long-lived values that benefit from subscriptions, activation, submission, and incremental revalidation.
Documentation
- Getting started
- Design rationale
- Specifications and predicates
- Presence rules and selections
- Inspection and error results
- Stateful inspectors
- Svelte, React, and Vue integrations
- Specma compatibility adapter
- API reference
- Changelog
Core model
Validation is described by three independent trees:
specdescribes whether present values are valid;requireddescribes which values must be present and which empty values are accepted;selectionlimits 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
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