Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "runtypes", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Runtime validation for static types", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
125
README.md
@@ -20,40 +20,36 @@ # Runtypes [![Build Status](https://travis-ci.org/pelotom/runtypes.svg?branch=master)](https://travis-ci.org/pelotom/runtypes) [![Coverage Status](https://coveralls.io/repos/pelotom/runtypes/badge.svg?branch=master)](https://coveralls.io/github/pelotom/runtypes?branch=master) | ||
```ts | ||
type Vector = [number, number, number] | ||
type Vector = [number, number, number]; | ||
type Asteroid = { | ||
type: 'asteroid' | ||
location: Vector | ||
mass: number | ||
} | ||
type: 'asteroid'; | ||
location: Vector; | ||
mass: number; | ||
}; | ||
type Planet = { | ||
type: 'planet' | ||
location: Vector | ||
mass: number | ||
population: number | ||
habitable: boolean | ||
} | ||
type: 'planet'; | ||
location: Vector; | ||
mass: number; | ||
population: number; | ||
habitable: boolean; | ||
}; | ||
type Rank = | ||
| 'captain' | ||
| 'first mate' | ||
| 'officer' | ||
| 'ensign' | ||
type Rank = 'captain' | 'first mate' | 'officer' | 'ensign'; | ||
type CrewMember = { | ||
name: string | ||
age: number | ||
rank: Rank | ||
home: Planet | ||
} | ||
name: string; | ||
age: number; | ||
rank: Rank; | ||
home: Planet; | ||
}; | ||
type Ship = { | ||
type: 'ship' | ||
location: Vector | ||
mass: number | ||
name: string | ||
crew: CrewMember[] | ||
} | ||
type: 'ship'; | ||
location: Vector; | ||
mass: number; | ||
name: string; | ||
crew: CrewMember[]; | ||
}; | ||
type SpaceObject = Asteroid | Planet | Ship | ||
type SpaceObject = Asteroid | Planet | Ship; | ||
``` | ||
@@ -66,5 +62,5 @@ | ||
```ts | ||
import { Boolean, Number, String, Literal, Array, Tuple, Record, Union } from 'runtypes' | ||
import { Boolean, Number, String, Literal, Array, Tuple, Record, Union } from 'runtypes'; | ||
const Vector = Tuple(Number, Number, Number) | ||
const Vector = Tuple(Number, Number, Number); | ||
@@ -75,3 +71,3 @@ const Asteroid = Record({ | ||
mass: Number, | ||
}) | ||
}); | ||
@@ -84,3 +80,3 @@ const Planet = Record({ | ||
habitable: Boolean, | ||
}) | ||
}); | ||
@@ -92,3 +88,3 @@ const Rank = Union( | ||
Literal('ensign'), | ||
) | ||
); | ||
@@ -100,3 +96,3 @@ const CrewMember = Record({ | ||
home: Planet, | ||
}) | ||
}); | ||
@@ -109,5 +105,5 @@ const Ship = Record({ | ||
crew: Array(CrewMember), | ||
}) | ||
}); | ||
const SpaceObject = Union(Asteroid, Planet, Ship) | ||
const SpaceObject = Union(Asteroid, Planet, Ship); | ||
``` | ||
@@ -121,3 +117,3 @@ | ||
// spaceObject: SpaceObject | ||
const spaceObject = SpaceObject.check(obj) | ||
const spaceObject = SpaceObject.check(obj); | ||
``` | ||
@@ -145,5 +141,5 @@ | ||
```ts | ||
import { Static } from 'runtypes' | ||
import { Static } from 'runtypes'; | ||
type Asteroid = Static<typeof Asteroid> | ||
type Asteroid = Static<typeof Asteroid>; | ||
``` | ||
@@ -155,6 +151,6 @@ | ||
type Asteroid = { | ||
type: 'asteroid' | ||
coordinates: [number, number, number] | ||
mass: number | ||
} | ||
type: 'asteroid'; | ||
coordinates: [number, number, number]; | ||
mass: number; | ||
}; | ||
``` | ||
@@ -168,12 +164,24 @@ | ||
function disembark(obj: {}) { | ||
if (SpaceObject.guard(obj)) { | ||
// obj: SpaceObject | ||
if (obj.type === 'ship') { | ||
// obj: Ship | ||
obj.crew = [] | ||
} | ||
if (SpaceObject.guard(obj)) { | ||
// obj: SpaceObject | ||
if (obj.type === 'ship') { | ||
// obj: 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: | ||
```ts | ||
const isHabitable = SpaceObject.match([asteroid => false, planet => planet.habitable]); | ||
if (isHabitable(spaceObject)) { | ||
// ... | ||
} | ||
``` | ||
## Constraint checking | ||
@@ -184,5 +192,5 @@ | ||
```ts | ||
const Positive = Number.withConstraint(n => n > 0) | ||
const Positive = Number.withConstraint(n => n > 0); | ||
Positive.check(-3) // Throws error: Failed constraint check | ||
Positive.check(-3); // Throws error: Failed constraint check | ||
``` | ||
@@ -193,7 +201,6 @@ | ||
```ts | ||
const Positive = Number.withConstraint(n => n > 0 || `${n} is not positive`) | ||
const Positive = Number.withConstraint(n => n > 0 || `${n} is not positive`); | ||
Positive.check(-3) // Throws error: -3 is not positive | ||
Positive.check(-3); // Throws error: -3 is not positive | ||
``` | ||
@@ -213,8 +220,8 @@ | ||
// Return type: | ||
Number | ||
).enforce((n, m) => n / m) | ||
Number, | ||
).enforce((n, m) => n / m); | ||
divide(10, 2) // 5 | ||
divide(10, 2); // 5 | ||
divide(10, 0) // Throws error: division by zero | ||
divide(10, 0); // Throws error: division by zero | ||
``` | ||
@@ -224,2 +231,2 @@ | ||
- [runtypes-generate](https://github.com/typeetfunc/runtypes-generate) Generates random data by `Runtype` for property-based testing | ||
* [runtypes-generate](https://github.com/typeetfunc/runtypes-generate) Generates random data by `Runtype` for property-based testing |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114965
218