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

runtypes

Package Overview
Dependencies
Maintainers
2
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 5.0.1 to 5.0.2

1

lib/asynccontract.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncContract = void 0;
var errors_1 = require("./errors");

@@ -4,0 +5,0 @@ function AsyncContract() {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Contract = void 0;
var errors_1 = require("./errors");

@@ -4,0 +5,0 @@ function Contract() {

2

lib/decorator.d.ts

@@ -32,3 +32,3 @@ import { Runtype } from './runtype';

*/
export declare function checked(...runtypes: Runtype[]): (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
export declare function checked(...runtypes: Runtype[]): (target: any, propertyKey: PropKey, descriptor: PropertyDescriptor) => void;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checked = exports.check = void 0;
var errors_1 = require("./errors");

@@ -4,0 +5,0 @@ var prototypes = new WeakMap();

@@ -6,6 +6,8 @@ "use strict";

({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);

@@ -17,2 +19,3 @@ function __() { this.constructor = d; }

Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationError = void 0;
var ValidationError = /** @class */ (function (_super) {

@@ -19,0 +22,0 @@ __extends(ValidationError, _super);

@@ -1,2 +0,2 @@

export { Runtype, Static } from './runtype';
export type { Runtype, Static } from './runtype';
export * from './reflect';

@@ -3,0 +3,0 @@ export * from './result';

"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./contract"));
__export(require("./asynccontract"));
__export(require("./match"));
__export(require("./errors"));
__export(require("./types/unknown"));
__export(require("./types/never"));
__export(require("./types/void"));
exports.Brand = exports.InstanceOf = exports.Null = exports.Undefined = exports.Literal = void 0;
__exportStar(require("./reflect"), exports);
__exportStar(require("./result"), exports);
__exportStar(require("./contract"), exports);
__exportStar(require("./asynccontract"), exports);
__exportStar(require("./match"), exports);
__exportStar(require("./errors"), exports);
__exportStar(require("./types/unknown"), exports);
__exportStar(require("./types/never"), exports);
__exportStar(require("./types/void"), exports);
var literal_1 = require("./types/literal");
exports.Literal = literal_1.Literal;
exports.Undefined = literal_1.Undefined;
exports.Null = literal_1.Null;
__export(require("./types/boolean"));
__export(require("./types/number"));
__export(require("./types/string"));
__export(require("./types/symbol"));
__export(require("./types/array"));
__export(require("./types/tuple"));
__export(require("./types/record"));
__export(require("./types/dictionary"));
__export(require("./types/union"));
__export(require("./types/intersect"));
__export(require("./types/function"));
Object.defineProperty(exports, "Literal", { enumerable: true, get: function () { return literal_1.Literal; } });
Object.defineProperty(exports, "Undefined", { enumerable: true, get: function () { return literal_1.Undefined; } });
Object.defineProperty(exports, "Null", { enumerable: true, get: function () { return literal_1.Null; } });
__exportStar(require("./types/boolean"), exports);
__exportStar(require("./types/number"), exports);
__exportStar(require("./types/string"), exports);
__exportStar(require("./types/symbol"), exports);
__exportStar(require("./types/array"), exports);
__exportStar(require("./types/tuple"), exports);
__exportStar(require("./types/record"), exports);
__exportStar(require("./types/dictionary"), exports);
__exportStar(require("./types/union"), exports);
__exportStar(require("./types/intersect"), exports);
__exportStar(require("./types/function"), exports);
var instanceof_1 = require("./types/instanceof");
exports.InstanceOf = instanceof_1.InstanceOf;
__export(require("./types/lazy"));
__export(require("./types/constraint"));
Object.defineProperty(exports, "InstanceOf", { enumerable: true, get: function () { return instanceof_1.InstanceOf; } });
__exportStar(require("./types/lazy"), exports);
__exportStar(require("./types/constraint"), exports);
var brand_1 = require("./types/brand");
exports.Brand = brand_1.Brand;
__export(require("./decorator"));
Object.defineProperty(exports, "Brand", { enumerable: true, get: function () { return brand_1.Brand; } });
__exportStar(require("./decorator"), exports);
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.match = void 0;
function match() {

@@ -4,0 +5,0 @@ var cases = [];

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.innerValidate = exports.create = void 0;
var index_1 = require("./index");

@@ -4,0 +5,0 @@ var show_1 = require("./show");

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Array = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Boolean = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

import { Runtype, Static } from '../runtype';
export declare const RuntypeName: unique symbol;
declare const RuntypeName: unique symbol;
export interface Brand<B extends string, A extends Runtype> extends Runtype<Static<A> & {

@@ -11,1 +11,2 @@ [RuntypeName]: B;

export declare function Brand<B extends string, A extends Runtype>(brand: B, entity: A): Brand<B, A>;
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Brand = void 0;
var runtype_1 = require("../runtype");
exports.RuntypeName = Symbol('RuntypeName');
function Brand(brand, entity) {

@@ -6,0 +6,0 @@ return runtype_1.create(function (value) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Guard = exports.Constraint = void 0;
var runtype_1 = require("../runtype");

@@ -28,2 +29,3 @@ var string_1 = require("./string");

exports.Constraint = Constraint;
exports.Guard = function (guard, options) { return unknown_1.Unknown.withGuard(guard, options); };
var Guard = function (guard, options) { return unknown_1.Unknown.withGuard(guard, options); };
exports.Guard = Guard;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Dictionary = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ var show_1 = require("../show");

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Function = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstanceOf = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ function InstanceOf(ctor) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Intersect = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ function Intersect() {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Lazy = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Null = exports.Undefined = exports.Literal = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Never = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Number = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Partial = exports.Record = exports.InternalRecord = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ var util_1 = require("../util");

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.String = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Symbol = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

@@ -34,11 +34,41 @@ import { Runtype, Static } from '../runtype';

}
export interface Tuple8<A extends Runtype, B extends Runtype, C extends Runtype, D extends Runtype, E extends Runtype, F extends Runtype, G extends Runtype, H extends Runtype> extends Runtype<[Static<A>, Static<B>, Static<C>, Static<D>, Static<E>, Static<F>, Static<G>, Static<H>]> {
export interface Tuple8<A extends Runtype, B extends Runtype, C extends Runtype, D extends Runtype, E extends Runtype, F extends Runtype, G extends Runtype, H extends Runtype> extends Runtype<[
Static<A>,
Static<B>,
Static<C>,
Static<D>,
Static<E>,
Static<F>,
Static<G>,
Static<H>
]> {
tag: 'tuple';
components: [A, B, C, D, E, F, G, H];
}
export interface Tuple9<A extends Runtype, B extends Runtype, C extends Runtype, D extends Runtype, E extends Runtype, F extends Runtype, G extends Runtype, H extends Runtype, I extends Runtype> extends Runtype<[Static<A>, Static<B>, Static<C>, Static<D>, Static<E>, Static<F>, Static<G>, Static<H>, Static<I>]> {
export interface Tuple9<A extends Runtype, B extends Runtype, C extends Runtype, D extends Runtype, E extends Runtype, F extends Runtype, G extends Runtype, H extends Runtype, I extends Runtype> extends Runtype<[
Static<A>,
Static<B>,
Static<C>,
Static<D>,
Static<E>,
Static<F>,
Static<G>,
Static<H>,
Static<I>
]> {
tag: 'tuple';
components: [A, B, C, D, E, F, G, H, I];
}
export interface Tuple10<A extends Runtype, B extends Runtype, C extends Runtype, D extends Runtype, E extends Runtype, F extends Runtype, G extends Runtype, H extends Runtype, I extends Runtype, J extends Runtype> extends Runtype<[Static<A>, Static<B>, Static<C>, Static<D>, Static<E>, Static<F>, Static<G>, Static<H>, Static<I>, Static<J>]> {
export interface Tuple10<A extends Runtype, B extends Runtype, C extends Runtype, D extends Runtype, E extends Runtype, F extends Runtype, G extends Runtype, H extends Runtype, I extends Runtype, J extends Runtype> extends Runtype<[
Static<A>,
Static<B>,
Static<C>,
Static<D>,
Static<E>,
Static<F>,
Static<G>,
Static<H>,
Static<I>,
Static<J>
]> {
tag: 'tuple';

@@ -45,0 +75,0 @@ components: [A, B, C, D, E, F, G, H, I, J];

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tuple = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ var array_1 = require("./array");

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Union = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ var show_1 = require("../show");

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Unknown = void 0;
var runtype_1 = require("../runtype");

@@ -4,0 +5,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Void = void 0;
var unknown_1 = require("./unknown");

@@ -4,0 +5,0 @@ /**

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasKey = void 0;
// Type guard to determine if an object has a given key

@@ -4,0 +5,0 @@ // If this feature gets implemented, we can use `in` instead: https://github.com/Microsoft/TypeScript/issues/10485

{
"name": "runtypes",
"version": "5.0.1",
"version": "5.0.2",
"description": "Runtime validation for static types",

@@ -17,8 +17,8 @@ "main": "./lib/index.js",

"devDependencies": {
"@types/jest": "24.0.18",
"coveralls": "^3.0.6",
"jest": "24.9.0",
"prettier": "^1.19.1",
"ts-jest": "^24.1.0",
"typescript": "3.7.5"
"@types/jest": "26.0.20",
"coveralls": "^3.1.0",
"jest": "26.6.3",
"prettier": "^2.2.1",
"ts-jest": "^26.5.3",
"typescript": "4.2.3"
},

@@ -25,0 +25,0 @@ "keywords:": [

@@ -161,3 +161,3 @@ # Runtypes [![Build Status](https://travis-ci.org/pelotom/runtypes.svg?branch=master)](https://travis-ci.org/pelotom/runtypes) [![Coverage Status](https://coveralls.io/repos/github/pelotom/runtypes/badge.svg?branch=master)](https://coveralls.io/github/pelotom/runtypes?branch=master)

}
}
};
```

@@ -296,2 +296,56 @@

## Branded types
Branded types is a way to emphasize the uniqueness of a type. This is useful [until we have nominal types](https://github.com/microsoft/TypeScript/pull/33038):
```ts
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');
// Static type OK, runtime OK
signIn(username, password);
// Static type ERROR, runtime OK
signIn(password, username);
// Static type ERROR, runtime OK
signIn('someone@example.com', '12345678');
```
Branded types are like [opaque types](https://flow.org/en/docs/types/opaque-types) and work as expected, except it is impossible to use as a key of an object type:
```ts
const StringBranded = String.withBrand('StringBranded');
type StringBranded = Static<typeof StringBranded>;
// Then the type `StringBranded` is computed as:
// string & { [RuntypeName]: "StringBranded" }
// TS1023: An index signature parameter type must be either `string` or `number`.
type SomeObject1 = { [K: StringBranded]: number };
// Both of these result in empty object type i.e. `{}`
type SomeObject2 = { [K in StringBranded]: number };
type SomeObject3 = Record<StringBranded, number>;
// You can do like this, but...
const key = StringBranded.check("key");
const SomeRecord = Record({ [key]: Number });
// This type results in { [x: string]: number }
type SomeRecord = Static<typeof SomeRecord>;
// So you have to use `Map` to achieve strongly-typed branded keys
type SomeMap = Map<StringBranded, number>;
```
## Optional values

@@ -353,9 +407,9 @@

mass: Number,
}).asReadonly()
}).asReadonly();
type Asteroid = Static<typeof Asteroid>;
// { readonly type: 'asteroid', readonly location: Vector, readonly mass: number }
Static<typeof Asteroid> // { readonly type: 'asteroid', readonly location: Vector, readonly mass: number }
const AsteroidArray = Array(Asteroid).asReadonly()
Static<typeof AsteroidArray> // ReadonlyArray<Asteroid>
const AsteroidArray = Array(Asteroid).asReadonly();
type AsteroidArray = Static<typeof AsteroidArray>;
// ReadonlyArray<Asteroid>
```

@@ -362,0 +416,0 @@

@@ -13,5 +13,3 @@ 'use strict';

const npmBinPath = execSync('npm bin')
.toString()
.trim();
const npmBinPath = execSync('npm bin').toString().trim();

@@ -18,0 +16,0 @@ const command = [

@@ -78,3 +78,3 @@ import { Runtype } from './runtype';

descriptor.value = function(...args: any[]) {
descriptor.value = function (...args: any[]) {
runtypes.forEach((type, typeIndex) => {

@@ -81,0 +81,0 @@ const parameterIndex = validParameterIndices[typeIndex];

@@ -1,2 +0,2 @@

export { Runtype, Static } from './runtype';
export type { Runtype, Static } from './runtype';
export * from './reflect';

@@ -3,0 +3,0 @@ export * from './result';

import { Runtype, Static, create } from '../runtype';
export const RuntypeName = Symbol('RuntypeName');
declare const RuntypeName: unique symbol;
export interface Brand<B extends string, A extends Runtype>
extends Runtype<
Static<A> & {
[RuntypeName]: B;
}
// TODO: replace it by nominal type when it has been released
// https://github.com/microsoft/TypeScript/pull/33038
Static<A> & { [RuntypeName]: B }
> {

@@ -11,0 +11,0 @@ tag: 'brand';

@@ -60,4 +60,3 @@ import { Runtype, Static, create, innerValidate } from '../runtype';

G extends Runtype
>
extends Runtype<
> extends Runtype<
Static<A> & Static<B> & Static<C> & Static<D> & Static<E> & Static<F> & Static<G>

@@ -78,4 +77,3 @@ > {

H extends Runtype
>
extends Runtype<
> extends Runtype<
Static<A> & Static<B> & Static<C> & Static<D> & Static<E> & Static<F> & Static<G> & Static<H>

@@ -97,4 +95,3 @@ > {

I extends Runtype
>
extends Runtype<
> extends Runtype<
Static<A> &

@@ -125,4 +122,3 @@ Static<B> &

J extends Runtype
>
extends Runtype<
> extends Runtype<
Static<A> &

@@ -129,0 +125,0 @@ Static<B> &

@@ -78,4 +78,3 @@ import { Runtype, Static, create, innerValidate } from '../runtype';

H extends Runtype
>
extends Runtype<
> extends Runtype<
[Static<A>, Static<B>, Static<C>, Static<D>, Static<E>, Static<F>, Static<G>, Static<H>]

@@ -97,4 +96,3 @@ > {

I extends Runtype
>
extends Runtype<
> extends Runtype<
[

@@ -127,4 +125,3 @@ Static<A>,

J extends Runtype
>
extends Runtype<
> extends Runtype<
[

@@ -131,0 +128,0 @@ Static<A>,

@@ -75,4 +75,3 @@ import { Runtype as Rt, Static, create, innerValidate } from '../runtype';

H extends Rt
>
extends Rt<
> extends Rt<
Static<A> | Static<B> | Static<C> | Static<D> | Static<E> | Static<F> | Static<G> | Static<H>

@@ -95,4 +94,3 @@ > {

I extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -124,4 +122,3 @@ | Static<B>

J extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -155,4 +152,3 @@ | Static<B>

K extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -188,4 +184,3 @@ | Static<B>

L extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -223,4 +218,3 @@ | Static<B>

M extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -260,4 +254,3 @@ | Static<B>

N extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -299,4 +292,3 @@ | Static<B>

O extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -340,4 +332,3 @@ | Static<B>

P extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -383,4 +374,3 @@ | Static<B>

Q extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -428,4 +418,3 @@ | Static<B>

R extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -475,4 +464,3 @@ | Static<B>

S extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -524,4 +512,3 @@ | Static<B>

T extends Rt
>
extends Rt<
> extends Rt<
| Static<A>

@@ -528,0 +515,0 @@ | Static<B>

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