ts-interface-checker
Runtime library to validate data against TypeScript interfaces.
This package is the runtime support for validators created by
ts-interface-builder.
It allows validating data, such as parsed JSON objects received
over the network, or parsed JSON or YAML files, to check if they satisfy a
TypeScript interface, and to produce informative error messages if they do not.
Installation
npm install --save-dev ts-interface-builder
npm install --save ts-interface-checker
Usage
Suppose you have a TypeScript file defining an interface:
interface Square {
size: number;
color?: string;
}
The first step is to generate some code for runtime checks:
`npm bin`/ts-interface-builder foo.ts
It produces a file like this:
import * as t from "ts-interface-checker";
export const Square = t.iface([], {
"size": "number",
"color": t.opt("string"),
});
...
Now at runtime, to check if a value satisfies the Square interface:
import fooTI from "./foo-ti";
import {createCheckers} from "ts-interface-checker";
const {Square} = createCheckers(fooTI);
Square.check({size: 1});
Square.check({size: 1, color: "green"});
Square.check({color: "green"});
Square.check({size: 4, color: 5});
Note that ts-interface-builder
is only needed for the build-time step, and
ts-interface-checker
is needed at runtime. That's why the recommendation is to npm-install the
former using --save-dev
flag and the latter using --save
.
Checking method calls
If you have an interface with methods, you can validate method call arguments and return values:
interface Greeter {
greet(name: string): string;
}
After generating the runtime code, you can now check calls like:
import greetTI from "./greet-ti";
import {createCheckers} from "ts-interface-checker";
const {Greeter} = createCheckers(greetTI);
Greeter.methodArgs("greet").check(["Bob"]);
Greeter.methodArgs("greet").check([17]);
Greeter.methodArgs("greet").check([]);
Greeter.methodResult("greet").check("hello");
Greeter.methodResult("greet").check(null);
Type suites
If one type refers to a type defined in another file, you need to tell the interface checker about
all type names when you call createCheckers()
. E.g. given
export type Color = RGB | string;
export type RGB = [number, number, number];
import {Color} from "./color";
export interface Square {
size: number;
color?: Color;
}
the produced files color-ti.ts
and shape-ti.ts
do not automatically refer to each other, but
expect you to relate them in createCheckers()
call:
import color from "./color-ti";
import shape from "./shape-ti";
import {createCheckers} from "ts-interface-checker";
const {Square} = createCheckers(shape, color);
Square.check({size: 1, color: [255,255,255]});
Strict checking
You may check that data contains no extra properties. Note that it is not generally recommended as
it this prevents backward compatibility: if you add new properties to an interface, then older
code with strict checks will not accept them.
Following on the example above:
Square.strictCheck({size: 1, color: [255,255,255], bg: "blue"});
Square.strictCheck({size: 1, color: [255,255,255,0.5]});
Type guards
Standard Checker
objects do the type checking logic, but are unable to make the TypeScript
compiler aware that an object of unknown
type implements a certain interface.
Basic code:
const unk: unknown = {size: 1, color: "green"};
console.log(unk.size);
With a Checker
available:
import fooTI from "./foo-ti";
import {createCheckers} from "ts-interface-checker";
const {Square} = createCheckers(fooTI);
const unk: unknown = {size: 1, color: "green"};
if (Square.test(unk)) {
console.log(unk.size);
}
To enable type guard functionality on the existing test
, and strictTest
functions, Checker
objects should be cast to CheckerT<>
using the appropriate type.
Using CheckerT<>
:
import {Square} from "./foo";
import fooTI from "./foo-ti";
import {createCheckers, CheckerT} from "ts-interface-checker";
const {Square} = createCheckers(fooTI) as {Square: CheckerT<Square>};
const unk: unknown = {size: 1, color: "green"};
if (Square.test(unk)) {
console.log(unk.size);
}
Type assertions
CheckerT<>
will eventually support type assertions using the check
and strictCheck
functions,
however, this feature is not yet fully working in TypeScript.