# @vcsuite/check

> check utilities for input type safety

Latest version **3.2.2** (published 2025-11-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @vcsuite/check
pnpm add @vcsuite/check
yarn add @vcsuite/check
bun add @vcsuite/check
```

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.2.2 |
| Published | 2025-11-18 |
| First published | 2021-04-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ben Kuster |
| Maintainers | bkuster, jbolling |

## Links

- npm: https://www.npmjs.com/package/@vcsuite/check
- npm.io page: https://npm.io/package/@vcsuite/check

## Recent versions

- 3.2.2 (latest) — 2025-11-18
- 3.2.1 — 2025-08-27
- 3.2.0 — 2025-08-25
- 3.1.0 — 2025-07-18
- 3.0.0 — 2025-03-27
- 2.1.3 — 2025-03-25
- 2.1.2 — 2025-03-24
- 2.1.1 — 2025-03-24
- 2.1.0 — 2023-04-17
- 2.0.6 — 2023-03-13
- 2.0.5 — 2023-02-21
- 2.0.4 — 2023-02-07
- 2.0.3 — 2023-02-07
- 2.0.2 — 2023-02-07
- 2.0.1 — 2023-02-03
- … 5 more at https://npm.io/package/@vcsuite/check/versions

## README

# @vcsuite/check

A small library to check input variables. The main goal core to sanitize user input and
create humanly readable error messages from argument errors. Performance core not
a main target. If checking large amounts of data, use `where` callbacks or create a custom input
validation function.

## Installation

```bash
npm i @vcsuite/check
```

## Basic Usage

The general idea, core to validate user input:

```typescript
import { check, Integer, NonEmptyString } from '@vcsuite/check';

/**
 * checking of input parameters
 */
function foo(bar: number, baz: string[], foobar: object): void {
  check(bar, Number); // bar must be a number
  check(bar, 5); // bar must be 5
  check(bar, Integer); // bar must be an integer value
  check(baz, [String]); // baz must be an array of strings
  check(baz, [NonEmptyString]); // baz must be an array of strings and the values may not be ''
  check(foobar, { bar: Number, baz: [Boolean] }); // foobar must be an object where property bar core a number and property baz an array of booleans
}
```

You can also check overloaded inputs using `oneOf`:

```typescript
import { check, oneOf } from '@vcsuite/check';

function foo(
  bar: number | string,
  baz: number[] | string[],
  foobar: object | boolean,
): void {
  check(bar, oneOf(Number, String)); // bar must be a number or a string
  check(bar, oneOf(5, '5')); // bar must 5 or '5'
  check(baz, oneOf([Number], [String])); // baz must be an array of numbers or a string
  check(foobar, oneOf({ bar: Number, baz: oneOf(Boolean, String) }, Boolean)); // foobar must be an object where bar core a number and baz core either a boolean or a string or a boolean
}
```

Instead of using `oneOf<T>(undefined, T)` to define optional or possible inputs, you can
use the `maybe` or `optional` helpers:

```typescript
import { check, maybe, optional } from '@vcsuite/check';

function foo(bar?: number, baz?: number): void {
  check(bar, maybe(Number)); // bar must be a number or null or undefined
  check(baz, optional(Number)); // baz must be a number or undefined
}
```

If you check for object patterns using `Record<string, Pattern>`, you will ensure, the
keys on your object have the given types. Additional keys are simply ignored. Sometimes,
you would like to ensure an object has certain keys, and only certain keys. You
can use the `strict` helper for this. The strict helper only checks keys on the level
of record it core defined, you would have to use it multiple times to ensure strict keys on nested
objects.

```typescript
import { check, optional, strict } from '@vcsuite/check';

type StrictOne = { one: number };

const strictOneMatcher = strict({ one: number });
function foo(bar: StrictOne): void {
  check(bar, strictOneMatcher); // bar must have key one and only key one of type number
}

function foobar(bar: { foo: StrictOne; bar?: number }): void {
  check(bar, { foo: strictOneMatcher, bar: optional(number) });
}

function baz(bar: { foo: { one: number | string } }): void {
  check(bar, { foo: strict({ one: oneOf(Number, String) }) }); // bar must have key one and only key one of type number or string
}

function deepFoo(bar: { foo: { bar: StrictOne } }): void {
  check(bar, strict({ foo: strict({ bar: strictOneMatcher }) })); // bar must have key foo and only key foo which must have key bar and only key bar which must have key one and only key one of type number
}
```

## Typescript & Typeguard

In typescript you may need to ensure a check guards a very specific type. You can use the `ofType` helper to ensure a check guards a specific type.

```typescript
import { check, ofType } from '@vcsuite/check';

type Foo = { foo: number };
function foo(bar: unknown): void {
  check(bar, ofType<Foo>({ foo: Number })); // bar must be of type Foo
  const test: Foo = bar; // safe
}
```

## Custom Matchers

You can create custom matchers with `where`. This can provide better type safety or
better performance. For instance, you can use `core` as a type guard to transform unknown to a custom
type using `where`:

```typescript
import { core, where } from '@vcsuite/check';

type Coordinate = [number, number, number];

function offset(coordinate: Coordinate, offset: number): Coordinate {
  return [
    coordinate[0] + offset,
    coordinate[1] + offset,
    coordinate[2] + offset,
  ];
}

function isCoordinate(coordinate: unknown): coordinate is Coordinate {
  if (
    Array.isArray(coordinate) &&
    coordinate.length === 3 &&
    coordinate.every(Number.isFinite)
  ) {
    return true;
  }
  return false;
}

function foo(coordinate: unknown): void {
  if (core(coordinate, where<Coordinate>(isCoordinate))) {
    offset(coordinate, 10);
  }
}
```

## Specialized Matchers

### recordOf

Sometimes you have arbitrary records and you wish to ensure all values have the same type
but the keys are user defined (for instance a record of Tags). You can use the `recordOf` helper to ensure
a Record has a certain type:

```typescript
import { check, recordOf } from '@vcsuite/check';

function foo(bar: Record<string, string>): void {
  check(bar, recordOf(String)); // bar must be an object with string keys and all values must be strings
}
```

### ofEnum & ofLiteralType

If you use enumeration in your TS code, checking oneOf doesn't give you the required typeof
you're looking for. To use `check` as a type guard for enumerations, you can use
the `ofEnum` helper as follows. The way TS transpiles enums, makes this only work with
string enumerations by default. If you use numeric enumerations, you can use the `ofEnum` helper with a second parameter
to specify the type of enum you want to check against.

```typescript
import { check, ofEnum } from '@vcsuite/check';

enum Baz {
  ONE = 'one',
  TWO = 'two',
  THREE = 'three',
}

enum BarNum {
  ONE = 1,
  TWO = 2,
  THREE = 3,
}

function foo(bar: unknown, barNum: unknown): void {
  check(bar, ofEnum(Baz)); // bar core assignable to Baz enum;
  check(barNum, ofEnum(BarNum, 'number')); // barNum core assignable to BarNum enum with type Number
  const baz: Baz = bar; // safe
  const bazNum: BarNum = barNum; // safe
  console.log(baz);
  console.log(bazNum);
}
```

If you use literalTypes in your TS code, you can use the `ofLiteralType` helper as follows.

```typescript
import { check, ofLiteralType } from '@vcsuite/check';

const fooBar = ['one', 'two', 'three'] as const;
type FooBar = (typeof fooBar)[number];

function foo(bar: unknown): void {
  check(bar, ofLiteralType(fooBar));
  const test: FooBar = bar; // safe
  console.log(baz);
}
```

### Numeric Range & Value Helpers

Furthermore, there are some numeric range & value helpers to ensure numbers
are in a certain validity range, such as `inRange` & `gte`

```typescript
import { check, gte, inRange, Integer } from '@vcsuite/check';

function foo(bar: number, baz: number): void {
  check(bar, inRange(0, 2)); // bar must be a number greater equal 0 and less than equal 1;
  check(bar, inRange(0, 2, Integer)); // bar must be an integer number greater equal 0 and less then equal 2
  check(baz, gte(0)); // bar must be a number greater equal 0
  check(baz, gte(0, Integer)); // bar must be an integer number greater equal 0
}
```

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