Runtypes
Safely bring untyped data into the fold
Runtypes allow you to take values about which you have no assurances and check that they conform to some type A
.
This is done by means of composable type validators of primitives, literals, arrays, tuples, records, unions,
intersections and more.
Installation
npm install --save runtypes
Example
Suppose you have objects which represent asteroids, planets, ships and crew members. In TypeScript, you might write their types like so:
type Vector = [number, number, number];
type Asteroid = {
type: 'asteroid';
location: Vector;
mass: number;
};
type Planet = {
type: 'planet';
location: Vector;
mass: number;
population: number;
habitable: boolean;
};
type Rank = 'captain' | 'first mate' | 'officer' | 'ensign';
type CrewMember = {
name: string;
age: number;
rank: Rank;
home: Planet;
};
type Ship = {
type: 'ship';
location: Vector;
mass: number;
name: string;
crew: CrewMember[];
};
type SpaceObject = Asteroid | Planet | Ship;
If the objects which are supposed to have these shapes are loaded from some external source, perhaps a JSON file, we need to
validate that the objects conform to their specifications. We do so by building corresponding Runtype
s in a very straightforward
manner:
import { Boolean, Number, String, Literal, Array, Tuple, Record, Union } from 'runtypes';
const Vector = Tuple(Number, Number, Number);
const Asteroid = Record({
type: Literal('asteroid'),
location: Vector,
mass: Number,
});
const Planet = Record({
type: Literal('planet'),
location: Vector,
mass: Number,
population: Number,
habitable: Boolean,
});
const Rank = Union(
Literal('captain'),
Literal('first mate'),
Literal('officer'),
Literal('ensign'),
);
const CrewMember = Record({
name: String,
age: Number,
rank: Rank,
home: Planet,
});
const Ship = Record({
type: Literal('ship'),
location: Vector,
mass: Number,
name: String,
crew: Array(CrewMember),
});
const SpaceObject = Union(Asteroid, Planet, Ship);
(See the examples directory for an expanded version of this.)
Now if we are given a putative SpaceObject
we can validate it like so:
const spaceObject = SpaceObject.check(obj);
If the object doesn't conform to the type specification, check
will throw an exception.
Static type inference
In TypeScript, the inferred type of Asteroid
in the above example is
Runtype<{
type: 'asteroid'
location: [number, number, number]
mass: number
}>
That is, it's a Runtype<Asteroid>
, and you could annotate it as such. But we don't really have to define the
Asteroid
type in TypeScript at all now, because the inferred type is correct. Defining each of your types
twice, once at the type level and then again at the value level, is a pain and not very DRY.
Fortunately you can define a static Asteroid
type which is an alias to the Runtype
-derived type like so:
import { Static } from 'runtypes';
type Asteroid = Static<typeof Asteroid>;
which achieves the same result as
type Asteroid = {
type: 'asteroid';
location: [number, number, number];
mass: number;
};
Type guards
In addition to providing a check
method, runtypes can be used as type guards:
function disembark(obj: {}) {
if (SpaceObject.guard(obj)) {
if (obj.type === 'ship') {
obj.crew = [];
}
}
}
Pattern matching
The Union
runtype offers the ability to do type-safe, exhaustive case analysis across its variants using the match
method:
const isHabitable = SpaceObject.match(
asteroid => false,
planet => planet.habitable,
ship => true,
);
if (isHabitable(spaceObject)) {
}
There's also a top-level match
function which allows testing an ad-hoc sequence of runtypes:
const makeANumber = match(
[Number, n => n * 3],
[Boolean, b => (b ? 1 : 0)],
[String, s => s.length],
);
makeANumber(9);
To allow the function to be applied to anything and then handle match failures, simply use an Unknown
case at the end:
const makeANumber = match(
[Number, n => n * 3],
[Boolean, b => (b ? 1 : 0)],
[String, s => s.length],
[Unknown, () => 42],
);
Constraint checking
Beyond mere type checking, we can add arbitrary runtime constraints to a Runtype
:
const Positive = Number.withConstraint(n => n > 0);
Positive.check(-3);
You can provide more descriptive error messages for failed constraints by returning
a string instead of false
:
const Positive = Number.withConstraint(n => n > 0 || `${n} is not positive`);
Positive.check(-3);
You can set a custom name for your runtype, which will be used in default error
messages and reflection, by using the name
prop on the optional options
parameter:
const C = Number.withConstraint(n => n > 0, { name: 'PositiveNumber' });
To change the type, there are two ways to do it: passing a type guard function
to a new Runtype.withGuard()
method, or using the familiar
Runtype.withConstraint()
method. (Both methods also accept an options
parameter to optionally set the name.)
Using a type guard function is the easiest option to change the static type,
because TS will infer the desired type from the return type of the guard
function.
const B = Unknown.withGuard(Buffer.isBuffer);
type T = Static<typeof B>;
However, if you want to return a custom error message from your constraint
function, you can't do this with a type guard because these functions can only
return boolean values. Instead, you can roll your own constraint function and
use the withConstraint<T>()
method. Remember to specify the type parameter for
the Constraint
because it can't be inferred from your check function!
const check = (o: any) => Buffer.isBuffer(o) || 'Dude, not a Buffer!';
const B = Unknown.withConstraint<Buffer>(check);
type T = Static<typeof B>;
One important choice when changing Constraint
static types is choosing the
correct underlying type. The implementation of Constraint
will validate the
underlying type before running your constraint function. So it's important to
use a lowest-common-denominator type that will pass validation for all expected
inputs of your constraint function or type guard. If there's no obvious
lowest-common-denominator type, you can always use Unknown
as the underlying
type, as shown in the Buffer
examples above.
Speaking of base types, if you're using a type guard function and your base type
is Unknown
, then there's a convenience runtype Guard
available, which is a
shorthand for Unknown.withGuard
.
const B = Guard(Buffer.isBuffer);
type T = Static<typeof B>;
Function contracts
Runtypes along with constraint checking are a natural fit for enforcing function
contracts. You can construct a contract from Runtype
s for the parameters and
return type of the function:
const divide = Contract(
Number,
Number.withConstraint(n => n !== 0 || 'division by zero'),
Number,
).enforce((n, m) => n / m);
divide(10, 2);
divide(10, 0);
Branded types
Branded types is a way to emphasize the uniqueness of a type. This is useful until we have nominal types:
const Username = String.withBrand('Username');
const Password = String.withBrand('Password').withConstraint(
str => str.length >= 8 || 'Too short password',
);
const signIn = Contract(Username, Password, Unknown).enforce((username, password) => {
});
const username = Username.check('someone@example.com');
const password = Password.check('12345678');
signIn(username, password);
signIn(password, username);
signIn('someone@example.com', '12345678');
Branded types are like opaque types and work as expected, except it is impossible to use as a key of an object type:
const StringBranded = String.withBrand('StringBranded');
type StringBranded = Static<typeof StringBranded>;
type SomeObject1 = { [K: StringBranded]: number };
type SomeObject2 = { [K in StringBranded]: number };
type SomeObject3 = Record<StringBranded, number>;
const key = StringBranded.check('key');
const SomeRecord = Record({ [key]: Number });
type SomeRecord = Static<typeof SomeRecord>;
type SomeMap = Map<StringBranded, number>;
Optional values
Runtypes can be used to represent a variable that may be undefined.
Union(String, Undefined);
String.Or(Undefined);
Optional(String);
String.optional();
The last syntax is not any shorter than writing Optional(String)
when you import Optional
directly from runtypes
, but if you use scoped import i.e. import * as rt from 'runtypes'
, it would look better to write rt.String.optional()
rather than rt.Optional(rt.String)
.
If a Record
may or may not have some properties, we can declare the optional properties using Record({ x: Optional(String) })
(or formerly Partial({ x: String })
). Optional properties validate successfully if they are absent or undefined or the type specified.
const RegisteredShip = Ship.And(
Record({
isRegistered: Literal(true),
shipClass: Optional(Union(Literal('military'), Literal('civilian'))),
rank: Optional(Rank.Or(Null)),
}),
);
There's a difference between Union(String, Undefined)
and Optional(String)
iff they are used within a Record
; the former means "it must be present, and must be string
or undefined
", while the latter means "it can be present or missing, but must be string
or undefined
if present".
Note that null
is a quite different thing than undefined
in JS and TS. If your Record
has properties which can be null, then use the Null
runtype explicitly.
const MilitaryShip = Ship.And(
Record({
shipClass: Literal('military'),
lastDeployedTimestamp: Number.Or(Null),
}),
);
Readonly records and arrays
Array and Record runtypes have a special function .asReadonly()
, that creates a new runtype where the values are readonly.
For example:
const Asteroid = Record({
type: Literal('asteroid'),
location: Vector,
mass: Number,
}).asReadonly();
type Asteroid = Static<typeof Asteroid>;
const AsteroidArray = Array(Asteroid).asReadonly();
type AsteroidArray = Static<typeof AsteroidArray>;
Related libraries
- runtypes-generate Generates random data by
Runtype
for property-based testing - rest.ts Allows building type safe and runtime-checked APIs