Runtime library to validate data against TypeScript interfaces.
Installations
npm install ts-interface-checker
Releases
Unable to fetch releases
Developer
gristlabs
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
14.17.0
NPM Version
7.11.2
Statistics
323 Stars
77 Commits
18 Forks
8 Watching
11 Branches
9 Contributors
Updated on 02 Aug 2024
Languages
TypeScript (62.51%)
JavaScript (37.49%)
Total Downloads
Cumulative downloads
Total Downloads
590,980,521
Last day
-4.7%
1,848,619
Compared to previous day
Last week
2.6%
10,630,579
Compared to previous week
Last month
8.2%
44,099,093
Compared to previous month
Last year
152.8%
403,734,571
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
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
1npm install --save-dev ts-interface-builder 2npm install --save ts-interface-checker
Usage
Suppose you have a TypeScript file defining an interface:
1// foo.ts 2interface Square { 3 size: number; 4 color?: string; 5}
The first step is to generate some code for runtime checks:
1`npm bin`/ts-interface-builder foo.ts
It produces a file like this:
1// foo-ti.js 2import * as t from "ts-interface-checker"; 3 4export const Square = t.iface([], { 5 "size": "number", 6 "color": t.opt("string"), 7}); 8...
Now at runtime, to check if a value satisfies the Square interface:
1import fooTI from "./foo-ti"; 2import {createCheckers} from "ts-interface-checker"; 3 4const {Square} = createCheckers(fooTI); 5 6Square.check({size: 1}); // OK 7Square.check({size: 1, color: "green"}); // OK 8Square.check({color: "green"}); // Fails with "value.size is missing" 9Square.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:
1// greet.ts 2interface Greeter { 3 greet(name: string): string; 4}
After generating the runtime code, you can now check calls like:
1import greetTI from "./greet-ti"; 2import {createCheckers} from "ts-interface-checker"; 3 4const {Greeter} = createCheckers(greetTI); 5 6Greeter.methodArgs("greet").check(["Bob"]); // OK 7Greeter.methodArgs("greet").check([17]); // Fails with "value.name is not a string" 8Greeter.methodArgs("greet").check([]); // Fails with "value.name is missing" 9 10Greeter.methodResult("greet").check("hello"); // OK 11Greeter.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
1// color.ts 2export type Color = RGB | string; 3export type RGB = [number, number, number];
1// shape.ts 2import {Color} from "./color"; 3export interface Square { 4 size: number; 5 color?: Color; 6}
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:
1import color from "./color-ti"; 2import shape from "./shape-ti"; 3import {createCheckers} from "ts-interface-checker"; 4 5const {Square} = createCheckers(shape, color); // Pass in all required type suites. 6 7Square.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:
1Square.strictCheck({size: 1, color: [255,255,255], bg: "blue"}); // Fails with value.bg is extraneous 2Square.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:
1const unk: unknown = {size: 1, color: "green"}; 2// Type is unknown, so TypeScript will not let you access the members. 3console.log(unk.size); // Error: "Object is of type 'unknown'"
With a Checker
available:
1import fooTI from "./foo-ti"; 2import {createCheckers} from "ts-interface-checker"; 3 4const {Square} = createCheckers(fooTI); 5 6const unk: unknown = {size: 1, color: "green"}; 7 8if (Square.test(unk)) { 9 // unk does implement Square, but TypeScript is not aware of it. 10 console.log(unk.size); // Error: "Object is of type 'unknown'" 11}
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<>
:
1import {Square} from "./foo"; 2import fooTI from "./foo-ti"; 3import {createCheckers, CheckerT} from "ts-interface-checker"; 4 5const checkers = createCheckers(fooTI) as {Square: CheckerT<Square>}; 6 7const unk: unknown = {size: 1, color: "green"}; 8 9if (checkers.Square.test(unk)) { 10 // TypeScript is now aware that unk implements Square, and allows member access. 11 console.log(unk.size); 12}
Type assertions
CheckerT<>
will eventually support type assertions using the check
and strictCheck
functions,
however, this feature is not yet fully working in TypeScript.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0
Reason
Found 7/24 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 19 are checked with a SAST tool
Reason
22 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-2j2x-2gpw-g8fm
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-g6ww-v8xp-vmwg
- Warn: Project is vulnerable to: GHSA-g954-5hwp-pp24
- Warn: Project is vulnerable to: GHSA-h755-8qp9-cq85
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
Score
2
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to ts-interface-checker
dk-checker-remove-extraneous
Utility for removing extraneous params via ts-interface-checker
dk-request
Request utility with validations based on Axios & ts-interface-checker
fork-ts-checker-webpack-plugin
Runs typescript type checker and linter on separate process.
ts-interface-builder
Compile TypeScript interfaces into a description that allows runtime validation