1.1.4 • Published 2 years ago

rtti v1.1.4

Weekly downloads
323
License
MIT
Repository
github
Last release
2 years ago

rtti

Runtime type validation for JavaScript and TypeScript programs.

This library bring the benefits of TypeScript's type system to runtime code. By declaring types using runtime constructs, it is possible to add an extra level of runtime type safety that static checking alone cannot provide. For example:

  • ensure that a parsed JSON string produces a value that conforms to an expected schema
  • verify that a HTTP request body conforms to an expected schema
  • ensure that a HTTP response body does not send additional properties other that those intended for the client

There is no need to declare any type twice (i.e., once for JS and once TS), since the TypeScript type can be inferred from the example property of any given TypeInfo value.

Installation

npm install rtti

Usage Example

import {t} from 'rtti';

const myType = t.union(
    t.unit('foo'),
    t.unit('bar')
);

// prints: "foo" | "bar"
console.log(myType.toString());

// prints: true
console.log(myType.isValid('foo'));

// prints: false
console.log(myType.isValid('baz'));

// prints: {
//     isValid: false,
//     errors: [
//         {path: '^', message: 'The value "baz" does not conform to the union type'}
//     ]
// }
console.log(myType.check('baz'));

// TypeScript only - static type inference:
type MyType = typeof myType.example; // type MyType = "foo" | "bar"

API

NOTE: The v0.4 API is deprecated but still supported.

t.string, t.object(...), etc

Construct a TypeInfo instance that matches a particular set of runtime values.


myType.assertValid(value: unknown): void

Ensures the given value matches the given type, otherwise throws an error. The error object has an errors property containing details about the validation error(s).


myType.check(value: unknown): {isValid: boolean, errors: Array<{path: string, message: string}>}

Returns a list of descriptive validation errors explaining why the given value does not match the given type.


myType.example

An example value that conforms to the given TypeInfo type. The TypeScript type can be inferred from this property.


myType.isValid(value: unknown): boolean

Returns true if the given value matches the given type, or false otherwise.


myType.sanitize(value: typeof myType.example): typeof myType.example

Returns a copy of the given value, but where any properties not declared in type have been removed.


myType.toJsonSchema(type: TypeInfo): unknown

Returns a JSON schema representation of the given type.


myType.toString(): string

Returns a descriptive string for the given type.


TypeInfo

An object used by the RTTI library to describes a set of matching runtime values. These objects may be created using the t.<kind> syntax. See the following table for examples.

Supported Types

PRIMITIVE JAVASCRIPT TYPES
DatatypeExample RTTI DeclarationTS TypeMatching JS ValuesNon-Matching JS Values
Booleant.booleanbooleantrue, false0, '', 'yes', null
Datet.dateDatenew Date()'2020-01-01'
Nullt.nullnullnullundefined, 0
Numbert.numbernumber42, 3.14'three', false
Stringt.stringstring'foo', '1:1'42, {foo: 1}
Undefinedt.undefinedundefinedundefinednull, 0
COMPOUND JAVASCRIPT TYPES
DatatypeExample RTTI DeclarationTS TypeMatching JS ValuesNon-Matching JS Values
Arrayt.array(t.number)number[][1, 2, 3]123, [1, 'a']
Objectt.object({foo: t.string, isBar: t.optional(t.boolean)}){foo: string, isBar?: boolean}{foo: 'foo'}, {foo: 'x', isBar: true}{bar: 'bar'}, {foo: true}
ADDITIONAL TYPESCRIPT TYPES
DatatypeExample RTTI DeclarationTS TypeMatching JS ValuesNon-Matching JS Values
Anyt.anyany42, 'foo', null, [1, 2], {}-
Branded Stringt.brandedString('usd')
Intersectiont.intersection(t.object({foo: t.string}), t.object({bar: t.number})){foo: string} & {bar: number}{foo: 'abc', bar: 42}{bar: 42}
Nevert.nevernever-42, 'foo', null, [1, 2], {}
Tuplet.tuple(t.string, t.number)[string, number]['foo', 42]['foo'], ['foo', 'bar'], ['foo', 4, 2]
Uniont.union(t.object({foo: t.string}), t.object({bar: t.number})){foo: string} | {bar: number}{foo: 'abc'}, {bar: 42}{baz: 0}, {foo: 42}
Unit Typet.unit('foo')'foo''foo''bar', 'abc', 42
Unknownt.unknown42, 'foo', null, [1, 2], {}-
1.1.4

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.0-alpha.0

3 years ago

0.4.0

3 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago