Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

runtypes

Package Overview
Dependencies
Maintainers
1
Versions
77
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

runtypes - npm Package Compare versions

Comparing version 0.1.7 to 0.2.0

67

lib/index.d.ts

@@ -58,41 +58,54 @@ /**

*/
export declare const anything: Runtype<{}>;
export declare const Anything: Runtype<{}>;
/**
* Validates nothing (always fails).
*/
export declare const nothing: Runtype<never>;
export declare const Nothing: Runtype<never>;
/**
* Validates that a value is undefined.
*/
export declare const Undefined: Runtype<undefined>;
/**
* Validates that a value is null.
*/
export declare const Null: Runtype<null>;
/**
* Validates that a value is void (null or undefined).
*/
export declare const Void: Runtype<void>;
/**
* Validates that a value is a boolean.
*/
export declare const boolean: Runtype<boolean>;
export declare const Boolean: Runtype<boolean>;
/**
* Validates that a value is a number.
*/
export declare const number: Runtype<number>;
export declare const Number: Runtype<number>;
/**
* Validates that a value is a string.
*/
export declare const string: Runtype<string>;
export declare const String: Runtype<string>;
/**
* Construct a literal runtype.
*/
export declare function literal<K extends string | number | boolean>(l: K): Runtype<K>;
export declare function Literal<K extends string | number | boolean>(l: K): Runtype<K>;
/**
* Construct an array runtype from a runtype for its elements.
*/
export declare function array<A>(v: Runtype<A>): Runtype<A[]>;
declare function arr<A>(v: Runtype<A>): Runtype<A[]>;
export { arr as Array };
/**
* Construct a tuple runtype from runtypes for each of its elements.
*/
export declare function tuple<A>(a: Runtype<A>, strict?: boolean): Runtype<[A]>;
export declare function tuple<A, B>(a: Runtype<A>, b: Runtype<B>, strict?: boolean): Runtype<[A, B]>;
export declare function tuple<A, B, C>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, strict?: boolean): Runtype<[A, B, C]>;
export declare function tuple<A, B, C, D>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, strict?: boolean): Runtype<[A, B, C, D]>;
export declare function tuple<A, B, C, D, E>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, strict?: boolean): Runtype<[A, B, C, D, E]>;
export declare function tuple<A, B, C, D, E, F>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, f: Runtype<F>, strict?: boolean): Runtype<[A, B, C, D, E, F]>;
export declare function tuple<A, B, C, D, E, F, G>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, f: Runtype<F>, g: Runtype<G>, strict?: boolean): Runtype<[A, B, C, D, E, F, G]>;
export declare function Tuple<A>(a: Runtype<A>, strict?: boolean): Runtype<[A]>;
export declare function Tuple<A, B>(a: Runtype<A>, b: Runtype<B>, strict?: boolean): Runtype<[A, B]>;
export declare function Tuple<A, B, C>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, strict?: boolean): Runtype<[A, B, C]>;
export declare function Tuple<A, B, C, D>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, strict?: boolean): Runtype<[A, B, C, D]>;
export declare function Tuple<A, B, C, D, E>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, strict?: boolean): Runtype<[A, B, C, D, E]>;
export declare function Tuple<A, B, C, D, E, F>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, f: Runtype<F>, strict?: boolean): Runtype<[A, B, C, D, E, F]>;
export declare function Tuple<A, B, C, D, E, F, G>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, f: Runtype<F>, g: Runtype<G>, strict?: boolean): Runtype<[A, B, C, D, E, F, G]>;
/**
* Construct a record runtype from runtypes for its values.
*/
export declare function record<O>(runtypes: {
export declare function Record<O>(runtypes: {
[K in keyof O]: Runtype<O[K]>;

@@ -103,9 +116,17 @@ }): Runtype<O>;

*/
export declare function union(): Runtype<never>;
export declare function union<A>(a: Runtype<A>): Runtype<A>;
export declare function union<A, B>(a: Runtype<A>, b: Runtype<B>): Runtype<A | B>;
export declare function union<A, B, C>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>): Runtype<A | B | C>;
export declare function union<A, B, C, D>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>): Runtype<A | B | C | D>;
export declare function union<A, B, C, D, E>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>): Runtype<A | B | C | D | E>;
export declare function union<A, B, C, D, E, F>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, f: Runtype<F>): Runtype<A | B | C | D | E | F>;
export declare function union<A, B, C, D, E, F, G>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, f: Runtype<F>, g: Runtype<G>): Runtype<A | B | C | D | E | F | G>;
export declare function Union(): Runtype<never>;
export declare function Union<A>(a: Runtype<A>): Runtype<A>;
export declare function Union<A, B>(a: Runtype<A>, b: Runtype<B>): Runtype<A | B>;
export declare function Union<A, B, C>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>): Runtype<A | B | C>;
export declare function Union<A, B, C, D>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>): Runtype<A | B | C | D>;
export declare function Union<A, B, C, D, E>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>): Runtype<A | B | C | D | E>;
export declare function Union<A, B, C, D, E, F>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, f: Runtype<F>): Runtype<A | B | C | D | E | F>;
export declare function Union<A, B, C, D, E, F, G>(a: Runtype<A>, b: Runtype<B>, c: Runtype<C>, d: Runtype<D>, e: Runtype<E>, f: Runtype<F>, g: Runtype<G>): Runtype<A | B | C | D | E | F | G>;
/**
* Constructs a possibly-undefined Runtype.
*/
export declare function Optional<A>(runtype: Runtype<A>): Runtype<A | undefined>;
/**
* Constructs a possibly-recursive Runtype.
*/
export declare function Lazy<A>(fn: () => Runtype<A>): Runtype<A>;

@@ -10,13 +10,37 @@ "use strict";

*/
exports.anything = runtype(function (x) { return x; });
exports.Anything = runtype(function (x) { return x; });
/**
* Validates nothing (always fails).
*/
exports.nothing = runtype(function (x) {
exports.Nothing = runtype(function (x) {
throw new ValidationError('Expected nothing but got something');
});
/**
* Validates that a value is undefined.
*/
exports.Undefined = runtype(function (x) {
if (x !== undefined)
throw new ValidationError("Expected undefined but was " + typeof x);
return x;
});
/**
* Validates that a value is null.
*/
exports.Null = runtype(function (x) {
if (x !== null)
throw new ValidationError("Expected null but was " + typeof x);
return x;
});
/**
* Validates that a value is void (null or undefined).
*/
exports.Void = runtype(function (x) {
if (x !== undefined && x !== null)
throw new ValidationError("Expected null but was " + typeof x);
return x;
});
/**
* Validates that a value is a boolean.
*/
exports.boolean = runtype(function (x) {
exports.Boolean = runtype(function (x) {
if (typeof x !== 'boolean')

@@ -29,3 +53,3 @@ throw new ValidationError("Expected boolean but was " + typeof x);

*/
exports.number = runtype(function (x) {
exports.Number = runtype(function (x) {
if (typeof x !== 'number')

@@ -38,3 +62,3 @@ throw new ValidationError("Expected number but was " + typeof x);

*/
exports.string = runtype(function (x) {
exports.String = runtype(function (x) {
if (typeof x !== 'string')

@@ -47,3 +71,3 @@ throw new ValidationError("Expected string but was " + typeof x);

*/
function literal(l) {
function Literal(l) {
return runtype(function (x) {

@@ -55,7 +79,7 @@ if (x !== l)

}
exports.literal = literal;
exports.Literal = Literal;
/**
* Construct an array runtype from a runtype for its elements.
*/
function array(v) {
function arr(v) {
return runtype(function (xs) {

@@ -71,4 +95,4 @@ if (!(xs instanceof Array))

}
exports.array = array;
function tuple() {
exports.Array = arr;
function Tuple() {
var args = [];

@@ -81,3 +105,3 @@ for (var _i = 0; _i < arguments.length; _i++) {

var runtypes;
if (exports.boolean.guard(lastArg)) {
if (exports.Boolean.guard(lastArg)) {
strict = lastArg;

@@ -91,3 +115,3 @@ runtypes = args.slice(0, args.length - 1);

return runtype(function (x) {
var xs = array(exports.anything).coerce(x);
var xs = arr(exports.Anything).coerce(x);
if (strict ? xs.length !== runtypes.length : xs.length < runtypes.length)

@@ -100,7 +124,7 @@ throw new ValidationError("Expected array of " + runtypes.length + " but was " + xs.length);

}
exports.tuple = tuple;
exports.Tuple = Tuple;
/**
* Construct a record runtype from runtypes for its values.
*/
function record(runtypes) {
function Record(runtypes) {
return runtype(function (x) {

@@ -115,4 +139,4 @@ if (typeof x !== 'object')

}
exports.record = record;
function union() {
exports.Record = Record;
function Union() {
var runtypes = [];

@@ -131,3 +155,22 @@ for (var _i = 0; _i < arguments.length; _i++) {

}
exports.union = union;
exports.Union = Union;
/**
* Constructs a possibly-undefined Runtype.
*/
function Optional(runtype) {
return Union(runtype, exports.Undefined);
}
exports.Optional = Optional;
/**
* Constructs a possibly-recursive Runtype.
*/
function Lazy(fn) {
var cached;
return runtype(function (x) {
if (!cached)
cached = fn();
return cached.coerce(x);
});
}
exports.Lazy = Lazy;
function runtype(coerce) {

@@ -134,0 +177,0 @@ var witness = undefined;

{
"name": "runtypes",
"version": "0.1.7",
"version": "0.2.0",
"description": "Runtime type validation for (Java|Type)Script",

@@ -5,0 +5,0 @@ "main": "./lib/index.js",

@@ -67,30 +67,30 @@ # Runtypes [![Build Status](https://travis-ci.org/pelotom/runtypes.svg?branch=master)](https://travis-ci.org/pelotom/runtypes)

```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)
const Asteroid = record({
type: literal('asteroid'),
const Asteroid = Record({
type: Literal('asteroid'),
location: Vector,
mass: number,
mass: Number,
})
const Planet = record({
type: literal('planet'),
const Planet = Record({
type: Literal('planet'),
location: Vector,
mass: number,
population: number,
habitable: boolean,
mass: Number,
population: Number,
habitable: Boolean,
})
const Rank = union(
literal('captain'),
literal('first mate'),
literal('officer'),
literal('ensign'),
const Rank = Union(
Literal('captain'),
Literal('first mate'),
Literal('officer'),
Literal('ensign'),
)
const CrewMember = record({
name: string,
age: number,
const CrewMember = Record({
name: String,
age: Number,
rank: Rank,

@@ -100,11 +100,11 @@ home: Planet,

const Ship = record({
type: literal('ship'),
const Ship = Record({
type: Literal('ship'),
location: Vector,
mass: number,
name: string,
crew: array(CrewMember),
mass: Number,
name: String,
crew: Array(CrewMember),
})
const SpaceObject = union(Asteroid, Planet, Ship)
const SpaceObject = Union(Asteroid, Planet, Ship)
```

@@ -111,0 +111,0 @@

@@ -1,26 +0,33 @@

import { Runtype, anything, nothing, boolean, number, string, literal, array, record, tuple, union } from './index'
import { Runtype, Anything, Nothing, Undefined, Null, Void, Boolean, Number, String, Literal, Array, Record, Tuple, Union, Optional, Lazy } from './index'
const boolTuple = tuple(boolean, boolean, boolean)
const record1 = record({ boolean, number })
const union1 = union(literal(3), string, boolTuple, record1)
const boolTuple = Tuple(Boolean, Boolean, Boolean)
const record1 = Record({ Boolean, Number })
const union1 = Union(Literal(3), String, boolTuple, record1)
const Person = Lazy(() => Record({ name: String, likes: Array(Person) }))
const runtypes = {
anything,
nothing,
boolean,
true: literal(true),
false: literal(false),
number,
3: literal(3),
42: literal(42),
string,
'hello world': literal('hello world'),
boolArray: array(boolean),
Anything,
Nothing,
Undefined,
Null,
Void,
Boolean,
true: Literal(true),
false: Literal(false),
Number,
3: Literal(3),
42: Literal(42),
OptionalNumber: Optional(Number),
String,
'hello world': Literal('hello world'),
boolArray: Array(Boolean),
boolTuple,
record1,
union1,
Person,
}
tuple(boolean, boolean, boolean).coerce([true, false, true])
Tuple(Boolean, Boolean, Boolean).coerce([true, false, true])

@@ -32,14 +39,18 @@ type RuntypeName = keyof typeof runtypes

const testValues: { value: {}, passes: RuntypeName[] }[] = [
{ value: true, passes: ['boolean', 'true'] },
{ value: false, passes: ['boolean', 'false'] },
{ value: 3, passes: ['number', '3', 'union1'] },
{ value: 42, passes: ['number', '42'] },
{ value: 'hello world', passes: ['string', 'hello world', 'union1'] },
{ value: undefined, passes: ['Undefined', 'Void', 'OptionalNumber'] },
{ value: null, passes: ['Null', 'Void'] },
{ value: true, passes: ['Boolean', 'true'] },
{ value: false, passes: ['Boolean', 'false'] },
{ value: 3, passes: ['Number', '3', 'union1', 'OptionalNumber'] },
{ value: 42, passes: ['Number', '42', 'OptionalNumber'] },
{ value: 'hello world', passes: ['String', 'hello world', 'union1'] },
{ value: [true, false, true], passes: ['boolArray', 'boolTuple', 'union1'] },
{ value: { boolean: true, number: 3 }, passes: ['record1', 'union1'] },
{ value: { Boolean: true, Number: 3 }, passes: ['record1', 'union1'] },
{ value: { name: 'Jimmy', likes: [{ name: 'Peter', likes: [] }] }, passes: ['Person'] },
]
for (const { value, passes } of testValues) {
describe(JSON.stringify(value), () => {
const shouldPass: { [_ in RuntypeName]?: boolean } = { anything: true }
const valueName = value === undefined ? 'undefined' : JSON.stringify(value)
describe(valueName, () => {
const shouldPass: { [_ in RuntypeName]?: boolean } = { Anything: true }
for (const name of passes)

@@ -46,0 +57,0 @@ shouldPass[name] = true

@@ -67,3 +67,3 @@ /**

*/
export const anything: Runtype<{}> = runtype(x => x)
export const Anything: Runtype<{}> = runtype(x => x)

@@ -73,3 +73,3 @@ /**

*/
export const nothing: Runtype<never> = runtype(x => {
export const Nothing: Runtype<never> = runtype(x => {
throw new ValidationError('Expected nothing but got something')

@@ -79,5 +79,32 @@ })

/**
* Validates that a value is undefined.
*/
export const Undefined: Runtype<undefined> = runtype(x => {
if (x !== undefined)
throw new ValidationError(`Expected undefined but was ${typeof x}`)
return x
})
/**
* Validates that a value is null.
*/
export const Null: Runtype<null> = runtype(x => {
if (x !== null)
throw new ValidationError(`Expected null but was ${typeof x}`)
return x
})
/**
* Validates that a value is void (null or undefined).
*/
export const Void: Runtype<void> = runtype(x => {
if (x !== undefined && x !== null)
throw new ValidationError(`Expected null but was ${typeof x}`)
return x
})
/**
* Validates that a value is a boolean.
*/
export const boolean: Runtype<boolean> = runtype(x => {
export const Boolean: Runtype<boolean> = runtype(x => {
if (typeof x !== 'boolean')

@@ -91,3 +118,3 @@ throw new ValidationError(`Expected boolean but was ${typeof x}`)

*/
export const number: Runtype<number> = runtype(x => {
export const Number: Runtype<number> = runtype(x => {
if (typeof x !== 'number')

@@ -101,3 +128,3 @@ throw new ValidationError(`Expected number but was ${typeof x}`)

*/
export const string: Runtype<string> = runtype(x => {
export const String: Runtype<string> = runtype(x => {
if (typeof x !== 'string')

@@ -111,3 +138,3 @@ throw new ValidationError(`Expected string but was ${typeof x}`)

*/
export function literal<K extends string | number | boolean>(l: K): Runtype<K> {
export function Literal<K extends string | number | boolean>(l: K): Runtype<K> {
return runtype(x => {

@@ -123,3 +150,3 @@ if (x !== l)

*/
export function array<A>(v: Runtype<A>): Runtype<A[]> {
function arr<A>(v: Runtype<A>): Runtype<A[]> {
return runtype(xs => {

@@ -133,2 +160,3 @@ if (!(xs instanceof Array))

}
export { arr as Array }

@@ -138,7 +166,7 @@ /**

*/
export function tuple<A>(
export function Tuple<A>(
a: Runtype<A>,
strict?: boolean
): Runtype<[A]>
export function tuple<A, B>(
export function Tuple<A, B>(
a: Runtype<A>,

@@ -148,3 +176,3 @@ b: Runtype<B>,

): Runtype<[A, B]>
export function tuple<A, B, C>(
export function Tuple<A, B, C>(
a: Runtype<A>,

@@ -155,3 +183,3 @@ b: Runtype<B>,

): Runtype<[A, B, C]>
export function tuple<A, B, C, D>(
export function Tuple<A, B, C, D>(
a: Runtype<A>,

@@ -163,3 +191,3 @@ b: Runtype<B>,

): Runtype<[A, B, C, D]>
export function tuple<A, B, C, D, E>(
export function Tuple<A, B, C, D, E>(
a: Runtype<A>,

@@ -172,3 +200,3 @@ b: Runtype<B>,

): Runtype<[A, B, C, D, E]>
export function tuple<A, B, C, D, E, F>(
export function Tuple<A, B, C, D, E, F>(
a: Runtype<A>,

@@ -182,3 +210,3 @@ b: Runtype<B>,

): Runtype<[A, B, C, D, E, F]>
export function tuple<A, B, C, D, E, F, G>(
export function Tuple<A, B, C, D, E, F, G>(
a: Runtype<A>,

@@ -193,7 +221,7 @@ b: Runtype<B>,

): Runtype<[A, B, C, D, E, F, G]>
export function tuple(...args: any[]) {
export function Tuple(...args: any[]) {
const lastArg = args[args.length - 1]
let strict: boolean
let runtypes: Runtype<{}>[]
if (boolean.guard(lastArg)) {
if (Boolean.guard(lastArg)) {
strict = lastArg

@@ -206,3 +234,3 @@ runtypes = args.slice(0, args.length - 1)

return runtype(x => {
const xs = array(anything).coerce(x)
const xs = arr(Anything).coerce(x)
if (strict ? xs.length !== runtypes.length : xs.length < runtypes.length)

@@ -219,3 +247,3 @@ throw new ValidationError(`Expected array of ${runtypes.length} but was ${xs.length}`)

*/
export function record<O>(runtypes: {[K in keyof O]: Runtype<O[K]> }): Runtype<O> {
export function Record<O>(runtypes: {[K in keyof O]: Runtype<O[K]> }): Runtype<O> {
return runtype<O>(x => {

@@ -236,12 +264,12 @@ if (typeof x !== 'object')

*/
export function union(
export function Union(
): Runtype<never>
export function union<A>(
export function Union<A>(
a: Runtype<A>,
): Runtype<A>
export function union<A, B>(
export function Union<A, B>(
a: Runtype<A>,
b: Runtype<B>,
): Runtype<A | B>
export function union<A, B, C>(
export function Union<A, B, C>(
a: Runtype<A>,

@@ -251,3 +279,3 @@ b: Runtype<B>,

): Runtype<A | B | C>
export function union<A, B, C, D>(
export function Union<A, B, C, D>(
a: Runtype<A>,

@@ -258,3 +286,3 @@ b: Runtype<B>,

): Runtype<A | B | C | D>
export function union<A, B, C, D, E>(
export function Union<A, B, C, D, E>(
a: Runtype<A>,

@@ -266,3 +294,3 @@ b: Runtype<B>,

): Runtype<A | B | C | D | E>
export function union<A, B, C, D, E, F>(
export function Union<A, B, C, D, E, F>(
a: Runtype<A>,

@@ -275,3 +303,3 @@ b: Runtype<B>,

): Runtype<A | B | C | D | E | F>
export function union<A, B, C, D, E, F, G>(
export function Union<A, B, C, D, E, F, G>(
a: Runtype<A>,

@@ -285,3 +313,3 @@ b: Runtype<B>,

): Runtype<A | B | C | D | E | F | G>
export function union(...runtypes: Runtype<any>[]) {
export function Union(...runtypes: Runtype<any>[]) {
return runtype(x => {

@@ -295,2 +323,21 @@ for (const { guard } of runtypes)

/**
* Constructs a possibly-undefined Runtype.
*/
export function Optional<A>(runtype: Runtype<A>): Runtype<A | undefined> {
return Union(runtype, Undefined)
}
/**
* Constructs a possibly-recursive Runtype.
*/
export function Lazy<A>(fn: () => Runtype<A>): Runtype<A> {
let cached: Runtype<A>
return runtype(x => {
if (!cached)
cached = fn()
return cached.coerce(x)
})
}
function runtype<A>(coerce: (x: {}) => A): Runtype<A> {

@@ -297,0 +344,0 @@ let witness: A = undefined as any as A

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc