@vcs/check
A small library to check input variables. The main goal is to sanitize user input and
create humanly readable error messages from argument errors. Performance is not
a main target. If checking large amounts of data, use where
callbacks or create a custom input
validation function.
Installation
npm i @vcs/check
Usage
The general idea, is to validate user input:
import { check, Integer, NonEmptyString } from '@vcs/check';
function foo(bar: number, baz: string[], foobar: object): void {
check(bar, Number);
check(bar, 5);
check(bar, Integer);
check(baz, [String]);
check(baz, [NonEmptyString]);
check(foobar, { bar: Number, baz: [Boolean] });
}
You can also check overloaded inputs using oneOf
:
import { check, oneOf } from '@vcs/check';
function foo(
bar: number | string,
baz: number[] | string[],
foobar: object | boolean,
): void {
check(bar, oneOf(Number, String));
check(bar, oneOf(5, '5'));
check(baz, oneOf([Number], [String]));
check(foobar, oneOf({ bar: Number, baz: oneOf(Boolean, String) }, Boolean));
}
Instead of using oneOf<T>(undefined, T)
to define optional or possible inputs, you can
use the maybe
or optional
helpers:
import { check, maybe, optional } from '@vcs/check';
function foo(bar?: number, baz?: number): void {
check(bar, maybe(Number));
check(baz, optional(Number));
}
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 is defined, you would have to use it multiple times to ensure strict keys on nested
objects.
import { check, optional, strict } from '@vcs/check';
type StrictOne = { one: number };
const strictOneMatcher = strict({ one: number });
function foo(bar: StrictOne): void {
check(bar, strictOneMatcher);
}
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) }) });
}
function deepFoo(bar: { foo: { bar: StrictOne } }): void {
check(bar, strict({ foo: strict({ bar: strictOneMatcher }) }));
}
You can create custom matchers with where
. This can provide better type safety or
better performance. For instance, you can use is
as a type guard to transform unknown to a custom
type using where
:
import { is, where } from '@vcs/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 (is(coordinate, where<Coordinate>(isCoordinate))) {
offset(coordinate, 10);
}
}
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:
import { check, recordOf } from '@vcs/check';
function foo(bar: Record<string, string>): void {
check(bar, recordOf(String));
}
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.
import { check, ofEnum } from '@vcs/check';
enum Baz {
ONE = 'one',
TWO = 'two',
THREE = 'three',
}
function foo(bar: unknown): void {
check(bar, ofEnum(Baz));
const baz: Baz = bar;
console.log(baz);
}
If you use literalTypes in your TS code, you can use the ofLiteralType
helper as follows.
import { check, ofLiteralType } from '@vcs/check';
const fooBar = ['one', 'two', 'three'];
type FooBar = (typeof fooBar)[number];
function foo(bar: unknown): void {
check(bar, ofLiteralType(fooBar));
const test: FooBar = bar;
console.log(baz);
}
Furthermore, there are some numeric range & value helpers to ensure numbers
are in a certain validity range, such as inRange
& gte
import { check, gte, inRange, Integer } from '@vcsuite/check';
function foo(bar: number, baz: number): void {
check(bar, inRange(0, 2));
check(bar, inRange(0, 2, Integer));
check(baz, gte(0));
check(baz, gte(0, Integer));
}
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.
import { check, ofType } from '@vcsuite/check';
type Foo = { foo: number };
function foo(bar: unknown): void {
check(bar, ofType<Foo>({ foo: Number }));
const test: Foo = bar;
}