# n4s

> typed schema validation version of enforce

Latest version **6.2.1** (published 2026-04-07) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.2.1 |
| Published | 2026-04-07 |
| First published | 2019-08-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 2.2 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 2662 |
| Author | ealush |
| Maintainers | ealush |

## Links

- npm: https://www.npmjs.com/package/n4s
- Repository: https://github.com/ealush/vest
- Homepage: https://github.com/ealush/vest#readme
- Issues: https://github.com/ealush/vest.git/issues
- npm.io page: https://npm.io/package/n4s

## Dependencies (2)

- [context](https://npm.io/package/context.md) ^4.0.17
- [vest-utils](https://npm.io/package/vest-utils.md) ^2.0.17

## Recent versions

- 6.2.1 (latest) — 2026-04-07
- 6.2.2-nightly-20260716-9eba (nightly) — 2026-07-16
- 6.1.11-next-20260318-d8c2 (next) — 2026-03-18
- 5.0.28-dev-20241111-bedd (dev) — 2024-11-11
- 2.2.0-rc.1 (rc) — 2020-10-21
- 0.0.5-rc.1 (rc-1) — 2019-08-05
- 6.2.2-nightly-20260715-9eba — 2026-07-15
- 6.2.2-nightly-20260714-9eba — 2026-07-14
- 6.2.2-nightly-20260713-9eba — 2026-07-13
- 6.2.2-nightly-20260712-9eba — 2026-07-12
- 6.2.2-nightly-20260711-7e44 — 2026-07-11
- 6.2.2-nightly-20260710-7e44 — 2026-07-10
- 6.2.2-nightly-20260709-7e44 — 2026-07-09
- 6.2.2-nightly-20260708-7e44 — 2026-07-08
- 6.2.2-nightly-20260707-7e44 — 2026-07-07
- … 327 more at https://npm.io/package/n4s/versions

## README

# n4s (enforce)

`enforce` is Vest's standalone assertion library. It powers Vest's validations and can be used directly to express fluent, chainable rules for any data structure.

## Quick start

```bash
npm i n4s
```

```js
import { enforce } from 'n4s';

enforce('hello').isString().longerThan(2);
enforce(7).isNumber().greaterThan(2).lessThanOrEquals(10);
```

Every rule throws when the assertion fails. Chain as many rules as you need; execution stops on the first failing rule.

## Lazy mode

If you prefer to inspect results without throwing, build a chain and call `.run(value)`:

```js
const numberCheck = enforce.isNumber().greaterThan(0);

const ok = numberCheck.run(5); // { pass: true, type: 5 }
const bad = numberCheck.run('5'); // { pass: false, type: '5' }
```

## Built-in rules

Rules cover common types and comparisons:

- Equality: `equals`, `notEquals`, `numberEquals`, `numberNotEquals`.
- Presence: `isEmpty`, `isNotEmpty`, `isBlank`, `isNotBlank`, `isNullish`, `isNotNullish`.
- Type checks: `isArray`, `isBoolean`, `isNumber`, `isString`, `isNaN`, plus negated forms.
- Comparisons: `greaterThan`, `greaterThanOrEquals`, `lessThan`, `lessThanOrEquals`, `between`, `notBetween`, `longerThan`, `shorterThan`, `lengthEquals`, `lengthNotEquals`.
- String helpers: `startsWith`, `doesNotStartWith`, `endsWith`, `doesNotEndWith`, `matches`, `notMatches`.
- Collections: `includes`, `inside`, `notInside`, `isKeyOf`, `isValueOf`.
- Booleans and truthiness: `isTruthy`, `isFalsy`, `isTrue`, `isFalse`.
- Numeric sanity: `isPositive`, `isNegative`, `isEven`, `isOdd`, `isNumeric`.

Plugins add higher-level checks such as `isEmail` and date helpers (`isDate`, `isAfter`, `isBefore`, `isISO8601`).

## Custom rules

Extend enforce with your own assertions using `enforce.extend`.

```js
import { enforce } from 'n4s';

enforce.extend({
  isBetweenExclusive: (value, min, max) => ({
    pass: value > min && value < max,
    message: () => `${value} must be between ${min} and ${max}`,
  }),
});

enforce(10).isBetweenExclusive(5, 15);
```

Rules can return a boolean or an object with `{ pass, message }`. Custom rules work in both throwing and lazy (`.run`) modes.

## Composing rules

Use compound helpers to combine conditions without writing new functions:

```js
enforce.allOf(enforce.isString(), enforce.longerThan(2)).run('Vest'); // pass: true

enforce.anyOf(enforce.equals('foo'), enforce.equals('bar')).run('bar');
```

`allOf`, `anyOf`, and `noneOf` evaluate other rules and return a single result, enabling reusable rule sets.

## Using with Vest

When imported from `vest`, `enforce` integrates with Vest tests, but it remains a standalone utility you can use anywhere you need fast, expressive assertions.

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