• Stars
    star
    323
  • Rank 130,051 (Top 3 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created almost 7 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Runtime library to validate data against TypeScript interfaces.

ts-interface-checker

Build Status npm version

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:

// foo.ts
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:

// foo-ti.js
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});                  // OK
Square.check({size: 1, color: "green"});  // OK
Square.check({color: "green"});           // Fails with "value.size is missing"
Square.check({size: 4, color: 5});        // Fails with "value.color is not a string"

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:

// greet.ts
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"]);     // OK
Greeter.methodArgs("greet").check([17]);        // Fails with "value.name is not a string"
Greeter.methodArgs("greet").check([]);          // Fails with "value.name is missing"

Greeter.methodResult("greet").check("hello");   // OK
Greeter.methodResult("greet").check(null);      // Fails with "value is not a string"

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

// color.ts
export type Color = RGB | string;
export type RGB = [number, number, number];
// shape.ts
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);    // Pass in all required type suites.

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"});    // Fails with value.bg is extraneous
Square.strictCheck({size: 1, color: [255,255,255,0.5]});            // Fails with ...value.color[3] is extraneous

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"};
// Type is unknown, so TypeScript will not let you access the members.
console.log(unk.size); // Error: "Object is of type 'unknown'"

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)) {
  // unk does implement Square, but TypeScript is not aware of it.
  console.log(unk.size); // Error: "Object is of type 'unknown'"
}

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 checkers = createCheckers(fooTI) as {Square: CheckerT<Square>};

const unk: unknown = {size: 1, color: "green"};

if (checkers.Square.test(unk)) {
  // TypeScript is now aware that unk implements Square, and allows member access.
  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.

More Repositories

1

grist-core

Grist is the evolution of spreadsheets.
TypeScript
6,985
star
2

asttokens

Annotate Python AST trees with source text and token information
Python
172
star
3

grist-desktop

Desktop Grist, packaged with Electron
TypeScript
166
star
4

ts-interface-builder

Compile TypeScript interfaces into a description that allows runtime validation
TypeScript
132
star
5

mkdocs-windmill

Outstanding mkdocs theme with a focus on navigation and usability
CSS
105
star
6

grist-static

Showing Grist spreadsheets on a static website, without a special backend.
TypeScript
88
star
7

grist-widget

A repository of custom widgets to embed in Grist documents
JavaScript
56
star
8

grist-omnibus

an opinionated Grist+Dex+Traefik package for first-time self-hosters
JavaScript
52
star
9

yaml-cfn

Parser and schema for CloudFormation YAML templates
JavaScript
31
star
10

py_grist_api

Python client for interacting with Grist
Python
19
star
11

grist-help

Grist documentation and help center articles
HTML
13
star
12

grist-api

NodeJS client for interacting with Grist
TypeScript
13
star
13

grainjs

Javascript library from Grist Labs
TypeScript
13
star
14

grist-ee

The source code for self-managed Grist Enterprise.
TypeScript
11
star
15

aws-lambda-upload

Package and upload an AWS lambda with its minimal dependencies
JavaScript
7
star
16

secrets.js

Tiny node tool to share secrets using public key crypto
JavaScript
6
star
17

jupyterlite-widget

Python
3
star
18

grain-rpc

Typed RPC interface on top of an arbitrary communication channel
TypeScript
3
star
19

grist-pug-py-widget

Grist widget to directly develop custom widgets within Gristโ€™s UI.
JavaScript
3
star
20

grist-form-submit

Turn form submissions on webpages into new records in Grist documents
TypeScript
3
star
21

mocha-webdriver

Write Mocha style tests using selenium-webdriver, with many conveniences.
TypeScript
3
star
22

npm-check-shrinkwrap

Quickly check if contents of node_modules correspond to npm-shrinkwrap.json
JavaScript
2
star
23

grist-plugin-examples

2
star
24

collect-js-deps

Collect the minimal list of dependencies required by a JS file.
JavaScript
1
star
25

grist-zapier

Code behind the initial Grist/Zapier integration, exported as a reference.
JavaScript
1
star