Socket
Socket
Sign inDemoInstall

simple-runtypes

Package Overview
Dependencies
0
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 6.3.0 to 7.0.0

dist/dictionary.d.ts

15

CHANGELOG.md

@@ -0,1 +1,16 @@

### 7.0.0
- rename `nullable` to `nullOr` to be consistent with `undefinedOr`
(because undefinable is not a good name)
- add `undefinedOr` that works like the old `optional` type
- real optional keys in `record`s:
Change `optional` to be only usable in `record`.
Using it will result in the key to be infered as optional:
`record({key: optional(number())})` results in `{key?: number}` as the type
(old behavior would infer `{key: undefined | number}`, you can get the old
behaviour by using `undefinedOr` instead of `optional`)
- add `dictionary` and remove `numberIndex` and `stringIndex`
Use `dictionary(stringAsInteger(), otherType)` to replace `numberIndex` and
`dictionary(string(), otherType)` to replace `stringIndex`.
### 6.3.0

@@ -2,0 +17,0 @@

8

dist/index.d.ts
export { getFormattedError, getFormattedErrorPath, getFormattedErrorValue, } from './runtypeError';
export { Fail, Runtype, RuntypeError, RuntypeUsageError } from './runtype';
export { Fail, OptionalRuntype, Runtype, RuntypeError, RuntypeUsageError, } from './runtype';
export { createError, runtype, use, ValidationResult } from './custom';

@@ -18,3 +18,4 @@ export { any } from './any';

export { integer } from './integer';
export { nullable } from './nullable';
export { nullOr } from './nullOr';
export { undefinedOr } from './undefinedOr';
export { optional } from './optional';

@@ -25,4 +26,3 @@ export { stringAsInteger } from './stringAsInteger';

export { record, sloppyRecord } from './record';
export { stringIndex } from './stringIndex';
export { numberIndex } from './numberIndex';
export { dictionary } from './dictionary';
export { intersection } from './intersection';

@@ -29,0 +29,0 @@ export { omit } from './omit';

@@ -1,5 +0,11 @@

import { Runtype } from './runtype';
import { OptionalRuntype, Runtype } from './runtype';
/**
* Optional (?)
* Optional (?), only usable within `record`
*
* Marks the key its used on as optional, e.g.:
*
* record({foo: optional(string())})
*
* => {foo?: string}
*/
export declare function optional<A>(t: Runtype<A>): Runtype<undefined | A>;
export declare function optional<A>(t: Runtype<A>): OptionalRuntype<A>;

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

import { Runtype } from './runtype';
import { Runtype, OptionalRuntype, Collapse, Unpack } from './runtype';
export declare function getRecordFields(r: Runtype<any>): {

@@ -14,5 +14,11 @@ [key: string]: Runtype<any>;

*/
export declare function record<T extends object>(typemap: {
[K in keyof T]: Runtype<T[K]>;
}): Runtype<T>;
export declare function record<T, Typemap = {
[K in keyof T]: Runtype<T[K]> | OptionalRuntype<T[K]>;
}, OptionalKeys extends keyof Typemap = {
[K in keyof Typemap]: Typemap[K] extends OptionalRuntype<any> ? K : never;
}[keyof Typemap]>(typemap: Typemap): Runtype<Collapse<{
[K in Exclude<keyof Typemap, OptionalKeys>]: Unpack<Typemap[K]>;
} & {
[K in OptionalKeys]?: Unpack<Typemap[K]>;
}>>;
/**

@@ -26,4 +32,10 @@ * Like record but ignore unknown keys.

*/
export declare function sloppyRecord<T extends object>(typemap: {
[K in keyof T]: Runtype<T[K]>;
}): Runtype<T>;
export declare function sloppyRecord<T, Typemap = {
[K in keyof T]: Runtype<T[K]> | OptionalRuntype<T[K]>;
}, OptionalKeys extends keyof Typemap = {
[K in keyof Typemap]: Typemap[K] extends OptionalRuntype<any> ? K : never;
}[keyof Typemap]>(typemap: Typemap): Runtype<Collapse<{
[K in Exclude<keyof Typemap, OptionalKeys>]: Unpack<Typemap[K]>;
} & {
[K in OptionalKeys]?: Unpack<Typemap[K]>;
}>>;

@@ -51,2 +51,13 @@ /**

}
/**
* Special runtype for use in record definitions to mark optional keys.
*/
export interface OptionalRuntype<T> {
isOptionalRuntype: true;
(v: unknown): T;
}
export declare type Unpack<T> = T extends Runtype<infer U> ? U : T extends OptionalRuntype<infer V> ? V : never;
export declare type Collapse<T> = T extends infer U ? {
[K in keyof U]: U[K];
} : never;
export declare function internalRuntype<T>(fn: (v: unknown, failOrThrow?: typeof failSymbol) => T, isPure?: boolean): Runtype<T>;

@@ -53,0 +64,0 @@ /**

@@ -715,6 +715,6 @@ 'use strict';

/**
* A type or null.
* Shortcut for a type or null.
*/
function nullable(t) {
function nullOr(t) {
var isPure = isPureRuntype(t);

@@ -731,6 +731,6 @@ return internalRuntype(function (v, failOrThrow) {

/**
* Optional (?)
* Shortcut for a type or undefined.
*/
function optional(t) {
function undefinedOr(t) {
var isPure = isPureRuntype(t);

@@ -746,2 +746,24 @@ return internalRuntype(function (v, failOrThrow) {

/**
* Optional (?), only usable within `record`
*
* Marks the key its used on as optional, e.g.:
*
* record({foo: optional(string())})
*
* => {foo?: string}
*/
function optional(t) {
var isPure = isPureRuntype(t);
var rt = internalRuntype(function (v, failOrThrow) {
if (v === undefined) {
return undefined;
}
return t(v, failOrThrow);
}, isPure);
return rt;
}
var stringAsIntegerRuntype =

@@ -906,23 +928,46 @@ /*#__PURE__*/

function isPureTypemap(typemap) {
for (var k in typemap) {
if (!Object.prototype.hasOwnProperty.call(typemap, k)) {
continue;
}
if (!isPureRuntype(typemap[k])) {
return false;
}
}
return true;
}
function internalRecord(typemap, sloppy) {
var isPure = Object.values(typemap).every(function (t) {
return isPureRuntype(t);
});
var copyObject = sloppy || !isPure;
var isPure = isPureTypemap(typemap);
var copyObject = sloppy || !isPure; // cache typemap in arrays for a faster loop
var typemapKeys = [].concat(Object.keys(typemap));
var typemapValues = [].concat(Object.values(typemap));
var rt = internalRuntype(function (v, failOrThrow) {
var o = objectRuntype(v, failOrThrow);
// inlined object runtype for perf
if (typeof v !== 'object' || Array.isArray(v) || v === null) {
return createFail(failOrThrow, 'expected an object', v);
}
if (isFail(o)) {
return propagateFail(failOrThrow, o, v);
} // optimize allocations: only create a copy if any of the key runtypes
var o = v; // optimize allocations: only create a copy if any of the key runtypes
// return a different object - otherwise return value as is
var res = copyObject ? {} : o;
for (var k in typemap) {
// nested types should always fail with explicit `Fail` so we can add additional data
var value = typemap[k](o[k], failSymbol);
for (var i = 0; i < typemapKeys.length; i++) {
var k = typemapKeys[i];
var t = typemapValues[i]; // nested types should always fail with explicit `Fail` so we can add additional data
var value = t(o[k], failSymbol);
if (isFail(value)) {
if (!(k in o)) {
// rt failed because o[k] was undefined bc. the key was missing from o
// use a more specific error message in this case
return createFail(failOrThrow, "missing key in record: " + debugValue(k));
}
return propagateFail(failOrThrow, value, v, k);

@@ -937,6 +982,10 @@ }

if (!sloppy) {
var unknownKeys = Object.keys(o).filter(function (k) {
return !Object.prototype.hasOwnProperty.call(typemap, k);
});
var unknownKeys = [];
for (var _k in o) {
if (!Object.prototype.hasOwnProperty.call(typemap, _k)) {
unknownKeys.push(o);
}
}
if (unknownKeys.length) {

@@ -995,8 +1044,4 @@ return createFail(failOrThrow, "invalid keys in record " + debugValue(unknownKeys), v);

/**
* An index with string keys.
*/
function stringIndex(t) {
var isPure = isPureRuntype(t);
function dictionaryRuntype(keyRuntype, valueRuntype) {
var isPure = isPureRuntype(keyRuntype) && isPureRuntype(valueRuntype);
return internalRuntype(function (v, failOrThrow) {

@@ -1010,22 +1055,35 @@ var o = objectRuntype(v, failOrThrow);

if (Object.getOwnPropertySymbols(o).length) {
return createFail(failOrThrow, "invalid key in stringIndex: " + debugValue(Object.getOwnPropertySymbols(o)), v);
}
return createFail(failOrThrow, "invalid key in dictionary: " + debugValue(Object.getOwnPropertySymbols(o)), v);
} // optimize allocations: only create a copy if any of the key runtypes
// return a different object - otherwise return value as is
var res = isPure ? o : {};
for (var k in o) {
if (k === '__proto__') {
for (var key in o) {
if (!Object.prototype.hasOwnProperty.call(o, key)) {
continue;
}
if (key === '__proto__') {
// e.g. someone tried to sneak __proto__ into this object and that
// will cause havoc when assigning it to a new object (in case its impure)
return createFail(failOrThrow, "invalid key in stringIndex: " + debugValue(k), v);
return createFail(failOrThrow, "invalid key in dictionary: " + debugValue(key), v);
}
var value = t(o[k], failSymbol);
var keyOrFail = keyRuntype(key, failOrThrow);
if (isFail(value)) {
return propagateFail(failOrThrow, value, v, k);
if (isFail(keyOrFail)) {
return propagateFail(failOrThrow, keyOrFail, v);
}
var value = o[key];
var valueOrFail = valueRuntype(value, failOrThrow);
if (isFail(valueOrFail)) {
return propagateFail(failOrThrow, valueOrFail, v);
}
if (!isPure) {
res[k] = value;
res[keyOrFail] = valueOrFail;
}

@@ -1037,48 +1095,12 @@ }

}
/**
* An index with number keys.
* An object that matches a Typecript `Record<KeyType, ValueType>` type.
*
* You pass a runtype for the objects keys and one for its values.
* Keeps you save from unwanted propertiers and evil __proto__ injections.
*/
function numberIndex(t) {
var isPure = isPureRuntype(t);
return internalRuntype(function (v, failOrThrow) {
var o = objectRuntype(v, failOrThrow);
if (isFail(o)) {
return propagateFail(failOrThrow, o, v);
}
if (Object.getOwnPropertySymbols(o).length) {
return createFail(failOrThrow, "invalid key in stringIndex: " + debugValue(Object.getOwnPropertySymbols(o)), v);
}
var res = isPure ? o : {};
for (var k in o) {
if (k === '__proto__') {
// e.g. someone tried to sneak __proto__ into this object and that
// will cause havoc when assigning it to a new object (in case its impure)
return createFail(failOrThrow, "invalid key in stringIndex: " + debugValue(k), v);
}
var key = stringAsIntegerRuntype(k, failOrThrow);
if (isFail(key)) {
return propagateFail(failOrThrow, key, v);
}
var value = t(o[key], failSymbol);
if (isFail(value)) {
return propagateFail(failOrThrow, value, v, key);
}
if (!isPure) {
res[key] = value;
}
}
return res;
}, true);
function dictionary(keyRuntype, valueRuntype) {
return dictionaryRuntype(keyRuntype, valueRuntype);
}

@@ -1322,3 +1344,4 @@

delete newRecordFields[k];
});
}); // TODO: keep 'sloppyness'
return record(newRecordFields);

@@ -1348,4 +1371,5 @@ }

}
}
} // TODO: keep 'sloppyness'
return record(newRecordFields);

@@ -1373,3 +1397,4 @@ }

newRecordFields[k] = fields[k];
});
}); // TODO: keep 'sloppyness'
return record(newRecordFields);

@@ -1384,2 +1409,3 @@ }

exports.createError = createError;
exports.dictionary = dictionary;
exports.enum = enumRuntype;

@@ -1395,5 +1421,4 @@ exports.getFormattedError = getFormattedError;

exports.null = nullRuntype;
exports.nullable = nullable;
exports.nullOr = nullOr;
exports.number = number;
exports.numberIndex = numberIndex;
exports.object = object;

@@ -1409,6 +1434,6 @@ exports.omit = omit;

exports.stringAsInteger = stringAsInteger;
exports.stringIndex = stringIndex;
exports.stringLiteralUnion = stringLiteralUnion;
exports.tuple = tuple;
exports.undefined = undefinedRuntype;
exports.undefinedOr = undefinedOr;
exports.union = union;

@@ -1415,0 +1440,0 @@ exports.unknown = unknown;

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

"use strict";function r(r,e){var t;if(void 0===e&&(e=512),void 0===r)return"undefined";if("function"==typeof r)return r.toString();try{t=JSON.stringify(r)}catch(e){t=""+r}return t.length>e?t.slice(0,e-1)+"…":t}function e(r){return Array.isArray(r.path)}function t(r){if(!e(r))return"(error is not a RuntypeError!)";var t=[].concat(r.path).reverse().map((function(r){return"number"==typeof r?"["+r+"]":/^\w+$/.test(r)?"."+r:"['"+JSON.stringify(r)+"']"})).join("");return t.startsWith(".")?t.slice(1):t}function n(t,n){return void 0===n&&(n=512),e(t)?r(t.path.reduceRight((function(r,e){var t=r.value,n=r.isResolvable;return n?e in t?{value:t[e],isResolvable:n}:{value:t,isResolvable:!1}:{value:t,isResolvable:n}}),{value:t.value,isResolvable:!0}).value,n):"(error is not a RuntypeError!)"}function o(){return(o=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function i(r,e){r.prototype=Object.create(e.prototype),r.prototype.constructor=r,r.__proto__=e}function u(r){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(r){return r.__proto__||Object.getPrototypeOf(r)})(r)}function a(r,e){return(a=Object.setPrototypeOf||function(r,e){return r.__proto__=e,r})(r,e)}function f(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(r){return!1}}function c(r,e,t){return(c=f()?Reflect.construct:function(r,e,t){var n=[null];n.push.apply(n,e);var o=new(Function.bind.apply(r,n));return t&&a(o,t.prototype),o}).apply(null,arguments)}function s(r){var e="function"==typeof Map?new Map:void 0;return(s=function(r){if(null===r||-1===Function.toString.call(r).indexOf("[native code]"))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(r))return e.get(r);e.set(r,t)}function t(){return c(r,arguments,u(this).constructor)}return t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),a(t,r)})(r)}Object.defineProperty(exports,"__esModule",{value:!0});var l=function(r){function e(e,t,n){var o;return(o=r.call(this,e)||this).name="RuntypeError",o.reason=e,o.path=n,o.value=t,o}return i(e,r),e}(s(Error)),p=function(r){function e(){return r.apply(this,arguments)||this}return i(e,r),e}(s(Error)),v=Symbol("SimpleRuntypesFail");function d(r,e,t){if(void 0===r)throw new l(e,t,[]);var n;if(r===v)return(n={})[v]=!0,n.reason=e,n.path=[],n.value=void 0,n;throw new p("failOrThrow must be undefined or the failSymbol, not "+JSON.stringify(r))}function y(r,e,t,n){if(void 0!==n&&e.path.push(n),void 0===r)throw new l(e.reason,t,e.path);if(r===v)return e;throw new p("failOrThrow must be undefined or the failSymbol, not "+JSON.stringify(r))}var h=Symbol("isPure");function g(r,e){if(!0===e)return Object.assign(r,{isPure:h});if(void 0===e||!1===e)return r;throw new p('expected "isPure" or undefined as the second argument')}function x(r){return!!r.isPure}function b(r){return!("object"!=typeof r||!r)&&r[v]}var m=g((function(r,e){return!0===r||!1===r?r:d(e,"expected a boolean",r)}),!0),w=g((function(r,e){return"object"!=typeof r||Array.isArray(r)||null===r?d(e,"expected an object",r):r}),!0),O=g((function(r,e){return"string"==typeof r?r:d(e,"expected a string",r)}),!0),j=g((function(r,e){return"number"==typeof r&&Number.isSafeInteger(r)?r:d(e,"expected a safe integer",r)}),!0);function _(r){return g((function(e,t){if(void 0!==e)return r(e,t)}),x(r))}var S=g((function(r,e){if("string"==typeof r){var t=parseInt(r,10),n=j(t,v);if(b(n))return y(e,n,r);var o="-0"===r?"0":"+"===r[0]?r.slice(1):r;return n.toString()!==o?d(e,"expected string to contain only the safe integer, not additional characters, whitespace or leading zeros",r):n}return d(e,"expected a string that contains a safe integer",r)})),R=g((function(r,e){return Array.isArray(r)?r:d(e,"expected an Array",r)}),!0);function P(e,t){var n=Object.values(e).every((function(r){return x(r)})),o=t||!n,i=g((function(n,i){var u=w(n,i);if(b(u))return y(i,u,n);var a=o?{}:u;for(var f in e){var c=e[f](u[f],v);if(b(c))return y(i,c,n,f);o&&(a[f]=c)}if(!t){var s=Object.keys(u).filter((function(r){return!Object.prototype.hasOwnProperty.call(e,r)}));if(s.length)return d(i,"invalid keys in record "+r(s),n)}return a}),n),u={};for(var a in e)u[a]=e[a];return i.fields=u,i}function A(r){if(r.fields)return r.fields}function E(r){return P(r,!1)}function I(e,t){var n=new Map;t.forEach((function(t){var o=t.fields[e].literal;if(void 0===o)throw new p("broken record type definition, "+t+"["+e+"] is not a literal");if("string"!=typeof o&&"number"!=typeof o)throw new p("broken record type definition, "+t+"["+e+"] must be a string or number, not "+r(o));n.set(o,t)}));var o=g((function(t,o){var i=w(t,o);if(b(i))return y(o,i,t);var u=i[e],a=n.get(u);return void 0===a?d(o,"no Runtype found for discriminated union tag "+e+": "+r(u),t):a(t,o)}),t.every((function(r){return x(r)})));return o.unions=t,o}function k(r){for(var e=new Map,t=0;t<r.length;t++){var n=A(r[t]);if(!n)return;for(var o in n){var i,u=n[o].literal;void 0!==u&&(e.has(o)||e.set(o,new Set),null===(i=e.get(o))||void 0===i||i.add(u))}}var a=[];if(e.forEach((function(e,t){e.size===r.length&&a.push(t)})),a.length)return a[0]}function N(){for(var r=arguments.length,e=new Array(r),t=0;t<r;t++)e[t]=arguments[t];if(!e.length)throw new p("no runtypes given to union");var n=k(e);if(void 0!==n)return I(n,e);var o=e.every((function(r){return x(r)}));return g((function(r,t){for(var n,o=0;o<e.length;o++){var i=e[o](r,v);if(!b(i))return i;n=i}return y(t,n,r)}),o)}function F(r,e){var t=r.unions;if(!t||!Array.isArray(t)||!t.length)throw new p("unionIntersection2: first argument is not a union type");return N.apply(void 0,t.map((function(r){return M(r,e)})))}function M(r,e){if("fields"in r&&"fields"in e)return function(r,e){var t={},n=r.fields,i=e.fields;for(var u in o({},n,{},i))if(n[u]&&i[u])t[u]=J(n[u],i[u]);else if(n[u])t[u]=n[u];else{if(!i[u])throw new p("recordIntersection2: invalid else");t[u]=i[u]}return E(t)}(r,e);if("unions"in r&&"fields"in e)return F(r,e);if("unions"in e&&"fields"in r)return F(e,r);if("fields"in r||"fields"in e)throw new p("intersection2: cannot intersect a base type with a record");return g((function(t,n){var o=r(t,n),i=e(t,n);return b(i)?y(n,i,t):b(o)?y(n,o,t):i}),x(r)&&x(e))}function J(){if(2===arguments.length)return M(arguments.length<=0?void 0:arguments[0],arguments.length<=1?void 0:arguments[1]);if(3===arguments.length)return J(M(arguments.length<=0?void 0:arguments[0],arguments.length<=1?void 0:arguments[1]),arguments.length<=2?void 0:arguments[2]);throw new p("unsupported number of arguments "+arguments.length)}exports.RuntypeError=l,exports.RuntypeUsageError=p,exports.any=function(){return g((function(r){return r}),!0)},exports.array=function(r,e){var t=e||{},n=t.maxLength,o=t.minLength,i=x(r);return g((function(e,t){var u=R(e,t);if(b(u))return y(t,u,e);if(void 0!==n&&u.length>n)return d(t,"expected the array to contain at most "+n+" elements",e);if(void 0!==o&&u.length<o)return d(t,"expected the array to contain at least "+o+" elements",e);for(var a=i?u:new Array(u.length),f=0;f<u.length;f++){var c=r(u[f],v);if(b(c))return y(t,c,e,f);i||(a[f]=c)}return a}),i)},exports.boolean=function(){return m},exports.createError=function(r){return d(v,r)},exports.enum=function(e){return g((function(t,n){return"number"==typeof t&&void 0!==e[t]||-1!==Object.values(e).indexOf(t)?t:d(n,"expected a value that belongs to the enum "+r(e),t)}),!0)},exports.getFormattedError=function(r,e){void 0===e&&(e=512);var o=t(r),i=o?"<value>"+(o.startsWith("[")?"":".")+o:"<value>",u="name"in r?r.name+": ":"",a=n(r,e);return""+u+r.reason+" at `"+i+"` for `"+a+"`"},exports.getFormattedErrorPath=t,exports.getFormattedErrorValue=n,exports.guardedBy=function(r){return g((function(e,t){return r(e)?e:d(t,"expected typeguard to return true",e)}),!0)},exports.ignore=function(){return g((function(){}),!0)},exports.integer=function(r){if(!r)return j;var e=r.min,t=r.max;return g((function(r,n){var o=j(r,n);return b(o)?y(n,o,r):void 0!==e&&o<e?d(n,"expected the integer to be >= "+e,r):void 0!==t&&o>t?d(n,"expected the integer to be <= "+t,r):o}),!0)},exports.intersection=J,exports.literal=function(e){var t=g((function(t,n){return t===e?e:d(n,"expected a literal: "+r(e),t)}),!0);return t.literal=e,t},exports.null=function(){return g((function(r,e){return null!==r?d(e,"expected null",r):r}),!0)},exports.nullable=function(r){return g((function(e,t){return null===e?null:r(e,t)}),x(r))},exports.number=function(r){var e=r||{},t=e.allowNaN,n=e.allowInfinity,o=e.min,i=e.max;return g((function(r,e){return"number"!=typeof r?d(e,"expected a number",r):!t&&isNaN(r)?d(e,"expected a number that is not NaN",r):n||Infinity!==r&&-Infinity!==r?void 0!==o&&r<o?d(e,"expected number to be >= "+o,r):void 0!==i&&r>i?d(e,"expected number to be <= "+i,r):r:d(e,"expected a finite number",r)}),!0)},exports.numberIndex=function(e){var t=x(e);return g((function(n,o){var i=w(n,o);if(b(i))return y(o,i,n);if(Object.getOwnPropertySymbols(i).length)return d(o,"invalid key in stringIndex: "+r(Object.getOwnPropertySymbols(i)),n);var u=t?i:{};for(var a in i){if("__proto__"===a)return d(o,"invalid key in stringIndex: "+r(a),n);var f=S(a,o);if(b(f))return y(o,f,n);var c=e(i[f],v);if(b(c))return y(o,c,n,f);t||(u[f]=c)}return u}),!0)},exports.object=function(){return w},exports.omit=function(r){var e=r.fields;if(!e)throw new p("expected a record runtype");for(var t=o({},e),n=arguments.length,i=new Array(n>1?n-1:0),u=1;u<n;u++)i[u-1]=arguments[u];return i.forEach((function(r){delete t[r]})),E(t)},exports.optional=_,exports.partial=function(r){var e=r.fields;if(!e)throw new p("expected a record runtype");var t={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=_(e[n]));return E(t)},exports.pick=function(r){var e=r.fields;if(!e)throw new p("expected a record runtype");for(var t={},n=arguments.length,o=new Array(n>1?n-1:0),i=1;i<n;i++)o[i-1]=arguments[i];return o.forEach((function(r){t[r]=e[r]})),E(t)},exports.record=E,exports.runtype=function(r){return g((function(e,t){var n=r(e);return b(n)?y(t,n,e):n}))},exports.sloppyRecord=function(r){return P(r,!0)},exports.string=function(r){if(!r)return O;var e=r.maxLength,t=r.trim,n=r.match;return g((function(r,o){var i=O(r,o);return b(i)?y(o,i,r):void 0!==e&&i.length>e?d(o,"expected the string length to not exceed "+e,r):void 0===n||n.test(i)?t?i.trim():i:d(o,"expected the string to match "+n,r)}),!t)},exports.stringAsInteger=function(r){if(!r)return S;var e=r.min,t=r.max;return g((function(r,n){var o=S(r,n);return b(o)?y(n,o,r):void 0!==e&&o<e?d(n,"expected the integer to be >= "+e,r):void 0!==t&&o>t?d(n,"expected the integer to be <= "+t,r):o}))},exports.stringIndex=function(e){var t=x(e);return g((function(n,o){var i=w(n,o);if(b(i))return y(o,i,n);if(Object.getOwnPropertySymbols(i).length)return d(o,"invalid key in stringIndex: "+r(Object.getOwnPropertySymbols(i)),n);var u=t?i:{};for(var a in i){if("__proto__"===a)return d(o,"invalid key in stringIndex: "+r(a),n);var f=e(i[a],v);if(b(f))return y(o,f,n,a);t||(u[a]=f)}return u}),t)},exports.stringLiteralUnion=function(){for(var r=arguments.length,e=new Array(r),t=0;t<r;t++)e[t]=arguments[t];var n=new Set(e);return g((function(r,t){return"string"==typeof r&&n.has(r)?r:d(t,"expected one of "+e,r)}),!0)},exports.tuple=function(){for(var r=arguments.length,e=new Array(r),t=0;t<r;t++)e[t]=arguments[t];var n=e.every((function(r){return x(r)}));return g((function(r,t){var o=R(r,t);if(b(o))return y(t,o,r);if(o.length!==e.length)return d(t,"tuple array does not have the required length",r);for(var i=n?o:new Array(o.length),u=0;u<e.length;u++){var a=e[u](o[u],v);if(b(a))return y(t,a,r,u);n||(i[u]=a)}return i}),n)},exports.undefined=function(){return g((function(r,e){return void 0!==r?d(e,"expected undefined",r):r}),!0)},exports.union=N,exports.unknown=function(){return g((function(r){return r}),!0)},exports.use=function(r,e){var t=r(e,v);return b(t)?(t.value=e,{ok:!1,error:t}):{ok:!0,result:t}};
"use strict";function r(r,e){var t;if(void 0===e&&(e=512),void 0===r)return"undefined";if("function"==typeof r)return r.toString();try{t=JSON.stringify(r)}catch(e){t=""+r}return t.length>e?t.slice(0,e-1)+"…":t}function e(r){return Array.isArray(r.path)}function t(r){if(!e(r))return"(error is not a RuntypeError!)";var t=[].concat(r.path).reverse().map((function(r){return"number"==typeof r?"["+r+"]":/^\w+$/.test(r)?"."+r:"['"+JSON.stringify(r)+"']"})).join("");return t.startsWith(".")?t.slice(1):t}function n(t,n){return void 0===n&&(n=512),e(t)?r(t.path.reduceRight((function(r,e){var t=r.value,n=r.isResolvable;return n?e in t?{value:t[e],isResolvable:n}:{value:t,isResolvable:!1}:{value:t,isResolvable:n}}),{value:t.value,isResolvable:!0}).value,n):"(error is not a RuntypeError!)"}function o(){return(o=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function i(r,e){r.prototype=Object.create(e.prototype),r.prototype.constructor=r,r.__proto__=e}function u(r){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(r){return r.__proto__||Object.getPrototypeOf(r)})(r)}function a(r,e){return(a=Object.setPrototypeOf||function(r,e){return r.__proto__=e,r})(r,e)}function f(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(r){return!1}}function c(r,e,t){return(c=f()?Reflect.construct:function(r,e,t){var n=[null];n.push.apply(n,e);var o=new(Function.bind.apply(r,n));return t&&a(o,t.prototype),o}).apply(null,arguments)}function s(r){var e="function"==typeof Map?new Map:void 0;return(s=function(r){if(null===r||-1===Function.toString.call(r).indexOf("[native code]"))return r;if("function"!=typeof r)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(r))return e.get(r);e.set(r,t)}function t(){return c(r,arguments,u(this).constructor)}return t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),a(t,r)})(r)}Object.defineProperty(exports,"__esModule",{value:!0});var l=function(r){function e(e,t,n){var o;return(o=r.call(this,e)||this).name="RuntypeError",o.reason=e,o.path=n,o.value=t,o}return i(e,r),e}(s(Error)),p=function(r){function e(){return r.apply(this,arguments)||this}return i(e,r),e}(s(Error)),v=Symbol("SimpleRuntypesFail");function d(r,e,t){if(void 0===r)throw new l(e,t,[]);var n;if(r===v)return(n={})[v]=!0,n.reason=e,n.path=[],n.value=void 0,n;throw new p("failOrThrow must be undefined or the failSymbol, not "+JSON.stringify(r))}function y(r,e,t,n){if(void 0!==n&&e.path.push(n),void 0===r)throw new l(e.reason,t,e.path);if(r===v)return e;throw new p("failOrThrow must be undefined or the failSymbol, not "+JSON.stringify(r))}var h=Symbol("isPure");function g(r,e){if(!0===e)return Object.assign(r,{isPure:h});if(void 0===e||!1===e)return r;throw new p('expected "isPure" or undefined as the second argument')}function x(r){return!!r.isPure}function b(r){return!("object"!=typeof r||!r)&&r[v]}var w=g((function(r,e){return!0===r||!1===r?r:d(e,"expected a boolean",r)}),!0),m=g((function(r,e){return"object"!=typeof r||Array.isArray(r)||null===r?d(e,"expected an object",r):r}),!0),O=g((function(r,e){return"string"==typeof r?r:d(e,"expected a string",r)}),!0),j=g((function(r,e){return"number"==typeof r&&Number.isSafeInteger(r)?r:d(e,"expected a safe integer",r)}),!0);function A(r){return g((function(e,t){if(void 0!==e)return r(e,t)}),x(r))}var R=g((function(r,e){if("string"==typeof r){var t=parseInt(r,10),n=j(t,v);if(b(n))return y(e,n,r);var o="-0"===r?"0":"+"===r[0]?r.slice(1):r;return n.toString()!==o?d(e,"expected string to contain only the safe integer, not additional characters, whitespace or leading zeros",r):n}return d(e,"expected a string that contains a safe integer",r)})),S=g((function(r,e){return Array.isArray(r)?r:d(e,"expected an Array",r)}),!0);function P(e,t){var n=function(r){for(var e in r)if(Object.prototype.hasOwnProperty.call(r,e)&&!x(r[e]))return!1;return!0}(e),o=t||!n,i=[].concat(Object.keys(e)),u=[].concat(Object.values(e)),a=g((function(n,a){if("object"!=typeof n||Array.isArray(n)||null===n)return d(a,"expected an object",n);for(var f=n,c=o?{}:f,s=0;s<i.length;s++){var l=i[s],p=(0,u[s])(f[l],v);if(b(p))return l in f?y(a,p,n,l):d(a,"missing key in record: "+r(l));o&&(c[l]=p)}if(!t){var h=[];for(var g in f)Object.prototype.hasOwnProperty.call(e,g)||h.push(f);if(h.length)return d(a,"invalid keys in record "+r(h),n)}return c}),n),f={};for(var c in e)f[c]=e[c];return a.fields=f,a}function _(r){if(r.fields)return r.fields}function E(r){return P(r,!1)}function k(e,t){var n=new Map;t.forEach((function(t){var o=t.fields[e].literal;if(void 0===o)throw new p("broken record type definition, "+t+"["+e+"] is not a literal");if("string"!=typeof o&&"number"!=typeof o)throw new p("broken record type definition, "+t+"["+e+"] must be a string or number, not "+r(o));n.set(o,t)}));var o=g((function(t,o){var i=m(t,o);if(b(i))return y(o,i,t);var u=i[e],a=n.get(u);return void 0===a?d(o,"no Runtype found for discriminated union tag "+e+": "+r(u),t):a(t,o)}),t.every((function(r){return x(r)})));return o.unions=t,o}function N(r){for(var e=new Map,t=0;t<r.length;t++){var n=_(r[t]);if(!n)return;for(var o in n){var i,u=n[o].literal;void 0!==u&&(e.has(o)||e.set(o,new Set),null===(i=e.get(o))||void 0===i||i.add(u))}}var a=[];if(e.forEach((function(e,t){e.size===r.length&&a.push(t)})),a.length)return a[0]}function I(){for(var r=arguments.length,e=new Array(r),t=0;t<r;t++)e[t]=arguments[t];if(!e.length)throw new p("no runtypes given to union");var n=N(e);if(void 0!==n)return k(n,e);var o=e.every((function(r){return x(r)}));return g((function(r,t){for(var n,o=0;o<e.length;o++){var i=e[o](r,v);if(!b(i))return i;n=i}return y(t,n,r)}),o)}function F(r,e){var t=r.unions;if(!t||!Array.isArray(t)||!t.length)throw new p("unionIntersection2: first argument is not a union type");return I.apply(void 0,t.map((function(r){return M(r,e)})))}function M(r,e){if("fields"in r&&"fields"in e)return function(r,e){var t={},n=r.fields,i=e.fields;for(var u in o({},n,{},i))if(n[u]&&i[u])t[u]=J(n[u],i[u]);else if(n[u])t[u]=n[u];else{if(!i[u])throw new p("recordIntersection2: invalid else");t[u]=i[u]}return E(t)}(r,e);if("unions"in r&&"fields"in e)return F(r,e);if("unions"in e&&"fields"in r)return F(e,r);if("fields"in r||"fields"in e)throw new p("intersection2: cannot intersect a base type with a record");return g((function(t,n){var o=r(t,n),i=e(t,n);return b(i)?y(n,i,t):b(o)?y(n,o,t):i}),x(r)&&x(e))}function J(){if(2===arguments.length)return M(arguments.length<=0?void 0:arguments[0],arguments.length<=1?void 0:arguments[1]);if(3===arguments.length)return J(M(arguments.length<=0?void 0:arguments[0],arguments.length<=1?void 0:arguments[1]),arguments.length<=2?void 0:arguments[2]);throw new p("unsupported number of arguments "+arguments.length)}exports.RuntypeError=l,exports.RuntypeUsageError=p,exports.any=function(){return g((function(r){return r}),!0)},exports.array=function(r,e){var t=e||{},n=t.maxLength,o=t.minLength,i=x(r);return g((function(e,t){var u=S(e,t);if(b(u))return y(t,u,e);if(void 0!==n&&u.length>n)return d(t,"expected the array to contain at most "+n+" elements",e);if(void 0!==o&&u.length<o)return d(t,"expected the array to contain at least "+o+" elements",e);for(var a=i?u:new Array(u.length),f=0;f<u.length;f++){var c=r(u[f],v);if(b(c))return y(t,c,e,f);i||(a[f]=c)}return a}),i)},exports.boolean=function(){return w},exports.createError=function(r){return d(v,r)},exports.dictionary=function(e,t){return function(e,t){var n=x(e)&&x(t);return g((function(o,i){var u=m(o,i);if(b(u))return y(i,u,o);if(Object.getOwnPropertySymbols(u).length)return d(i,"invalid key in dictionary: "+r(Object.getOwnPropertySymbols(u)),o);var a=n?u:{};for(var f in u)if(Object.prototype.hasOwnProperty.call(u,f)){if("__proto__"===f)return d(i,"invalid key in dictionary: "+r(f),o);var c=e(f,i);if(b(c))return y(i,c,o);var s=t(u[f],i);if(b(s))return y(i,s,o);n||(a[c]=s)}return a}),n)}(e,t)},exports.enum=function(e){return g((function(t,n){return"number"==typeof t&&void 0!==e[t]||-1!==Object.values(e).indexOf(t)?t:d(n,"expected a value that belongs to the enum "+r(e),t)}),!0)},exports.getFormattedError=function(r,e){void 0===e&&(e=512);var o=t(r),i=o?"<value>"+(o.startsWith("[")?"":".")+o:"<value>",u="name"in r?r.name+": ":"",a=n(r,e);return""+u+r.reason+" at `"+i+"` for `"+a+"`"},exports.getFormattedErrorPath=t,exports.getFormattedErrorValue=n,exports.guardedBy=function(r){return g((function(e,t){return r(e)?e:d(t,"expected typeguard to return true",e)}),!0)},exports.ignore=function(){return g((function(){}),!0)},exports.integer=function(r){if(!r)return j;var e=r.min,t=r.max;return g((function(r,n){var o=j(r,n);return b(o)?y(n,o,r):void 0!==e&&o<e?d(n,"expected the integer to be >= "+e,r):void 0!==t&&o>t?d(n,"expected the integer to be <= "+t,r):o}),!0)},exports.intersection=J,exports.literal=function(e){var t=g((function(t,n){return t===e?e:d(n,"expected a literal: "+r(e),t)}),!0);return t.literal=e,t},exports.null=function(){return g((function(r,e){return null!==r?d(e,"expected null",r):r}),!0)},exports.nullOr=function(r){return g((function(e,t){return null===e?null:r(e,t)}),x(r))},exports.number=function(r){var e=r||{},t=e.allowNaN,n=e.allowInfinity,o=e.min,i=e.max;return g((function(r,e){return"number"!=typeof r?d(e,"expected a number",r):!t&&isNaN(r)?d(e,"expected a number that is not NaN",r):n||Infinity!==r&&-Infinity!==r?void 0!==o&&r<o?d(e,"expected number to be >= "+o,r):void 0!==i&&r>i?d(e,"expected number to be <= "+i,r):r:d(e,"expected a finite number",r)}),!0)},exports.object=function(){return m},exports.omit=function(r){var e=r.fields;if(!e)throw new p("expected a record runtype");for(var t=o({},e),n=arguments.length,i=new Array(n>1?n-1:0),u=1;u<n;u++)i[u-1]=arguments[u];return i.forEach((function(r){delete t[r]})),E(t)},exports.optional=A,exports.partial=function(r){var e=r.fields;if(!e)throw new p("expected a record runtype");var t={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=A(e[n]));return E(t)},exports.pick=function(r){var e=r.fields;if(!e)throw new p("expected a record runtype");for(var t={},n=arguments.length,o=new Array(n>1?n-1:0),i=1;i<n;i++)o[i-1]=arguments[i];return o.forEach((function(r){t[r]=e[r]})),E(t)},exports.record=E,exports.runtype=function(r){return g((function(e,t){var n=r(e);return b(n)?y(t,n,e):n}))},exports.sloppyRecord=function(r){return P(r,!0)},exports.string=function(r){if(!r)return O;var e=r.maxLength,t=r.trim,n=r.match;return g((function(r,o){var i=O(r,o);return b(i)?y(o,i,r):void 0!==e&&i.length>e?d(o,"expected the string length to not exceed "+e,r):void 0===n||n.test(i)?t?i.trim():i:d(o,"expected the string to match "+n,r)}),!t)},exports.stringAsInteger=function(r){if(!r)return R;var e=r.min,t=r.max;return g((function(r,n){var o=R(r,n);return b(o)?y(n,o,r):void 0!==e&&o<e?d(n,"expected the integer to be >= "+e,r):void 0!==t&&o>t?d(n,"expected the integer to be <= "+t,r):o}))},exports.stringLiteralUnion=function(){for(var r=arguments.length,e=new Array(r),t=0;t<r;t++)e[t]=arguments[t];var n=new Set(e);return g((function(r,t){return"string"==typeof r&&n.has(r)?r:d(t,"expected one of "+e,r)}),!0)},exports.tuple=function(){for(var r=arguments.length,e=new Array(r),t=0;t<r;t++)e[t]=arguments[t];var n=e.every((function(r){return x(r)}));return g((function(r,t){var o=S(r,t);if(b(o))return y(t,o,r);if(o.length!==e.length)return d(t,"tuple array does not have the required length",r);for(var i=n?o:new Array(o.length),u=0;u<e.length;u++){var a=e[u](o[u],v);if(b(a))return y(t,a,r,u);n||(i[u]=a)}return i}),n)},exports.undefined=function(){return g((function(r,e){return void 0!==r?d(e,"expected undefined",r):r}),!0)},exports.undefinedOr=function(r){return g((function(e,t){if(void 0!==e)return r(e,t)}),x(r))},exports.union=I,exports.unknown=function(){return g((function(r){return r}),!0)},exports.use=function(r,e){var t=r(e,v);return b(t)?(t.value=e,{ok:!1,error:t}):{ok:!0,result:t}};
//# sourceMappingURL=simple-runtypes.cjs.production.min.js.map

@@ -711,6 +711,6 @@ /**

/**
* A type or null.
* Shortcut for a type or null.
*/
function nullable(t) {
function nullOr(t) {
var isPure = isPureRuntype(t);

@@ -727,6 +727,6 @@ return internalRuntype(function (v, failOrThrow) {

/**
* Optional (?)
* Shortcut for a type or undefined.
*/
function optional(t) {
function undefinedOr(t) {
var isPure = isPureRuntype(t);

@@ -742,2 +742,24 @@ return internalRuntype(function (v, failOrThrow) {

/**
* Optional (?), only usable within `record`
*
* Marks the key its used on as optional, e.g.:
*
* record({foo: optional(string())})
*
* => {foo?: string}
*/
function optional(t) {
var isPure = isPureRuntype(t);
var rt = internalRuntype(function (v, failOrThrow) {
if (v === undefined) {
return undefined;
}
return t(v, failOrThrow);
}, isPure);
return rt;
}
var stringAsIntegerRuntype =

@@ -902,23 +924,46 @@ /*#__PURE__*/

function isPureTypemap(typemap) {
for (var k in typemap) {
if (!Object.prototype.hasOwnProperty.call(typemap, k)) {
continue;
}
if (!isPureRuntype(typemap[k])) {
return false;
}
}
return true;
}
function internalRecord(typemap, sloppy) {
var isPure = Object.values(typemap).every(function (t) {
return isPureRuntype(t);
});
var copyObject = sloppy || !isPure;
var isPure = isPureTypemap(typemap);
var copyObject = sloppy || !isPure; // cache typemap in arrays for a faster loop
var typemapKeys = [].concat(Object.keys(typemap));
var typemapValues = [].concat(Object.values(typemap));
var rt = internalRuntype(function (v, failOrThrow) {
var o = objectRuntype(v, failOrThrow);
// inlined object runtype for perf
if (typeof v !== 'object' || Array.isArray(v) || v === null) {
return createFail(failOrThrow, 'expected an object', v);
}
if (isFail(o)) {
return propagateFail(failOrThrow, o, v);
} // optimize allocations: only create a copy if any of the key runtypes
var o = v; // optimize allocations: only create a copy if any of the key runtypes
// return a different object - otherwise return value as is
var res = copyObject ? {} : o;
for (var k in typemap) {
// nested types should always fail with explicit `Fail` so we can add additional data
var value = typemap[k](o[k], failSymbol);
for (var i = 0; i < typemapKeys.length; i++) {
var k = typemapKeys[i];
var t = typemapValues[i]; // nested types should always fail with explicit `Fail` so we can add additional data
var value = t(o[k], failSymbol);
if (isFail(value)) {
if (!(k in o)) {
// rt failed because o[k] was undefined bc. the key was missing from o
// use a more specific error message in this case
return createFail(failOrThrow, "missing key in record: " + debugValue(k));
}
return propagateFail(failOrThrow, value, v, k);

@@ -933,6 +978,10 @@ }

if (!sloppy) {
var unknownKeys = Object.keys(o).filter(function (k) {
return !Object.prototype.hasOwnProperty.call(typemap, k);
});
var unknownKeys = [];
for (var _k in o) {
if (!Object.prototype.hasOwnProperty.call(typemap, _k)) {
unknownKeys.push(o);
}
}
if (unknownKeys.length) {

@@ -991,8 +1040,4 @@ return createFail(failOrThrow, "invalid keys in record " + debugValue(unknownKeys), v);

/**
* An index with string keys.
*/
function stringIndex(t) {
var isPure = isPureRuntype(t);
function dictionaryRuntype(keyRuntype, valueRuntype) {
var isPure = isPureRuntype(keyRuntype) && isPureRuntype(valueRuntype);
return internalRuntype(function (v, failOrThrow) {

@@ -1006,22 +1051,35 @@ var o = objectRuntype(v, failOrThrow);

if (Object.getOwnPropertySymbols(o).length) {
return createFail(failOrThrow, "invalid key in stringIndex: " + debugValue(Object.getOwnPropertySymbols(o)), v);
}
return createFail(failOrThrow, "invalid key in dictionary: " + debugValue(Object.getOwnPropertySymbols(o)), v);
} // optimize allocations: only create a copy if any of the key runtypes
// return a different object - otherwise return value as is
var res = isPure ? o : {};
for (var k in o) {
if (k === '__proto__') {
for (var key in o) {
if (!Object.prototype.hasOwnProperty.call(o, key)) {
continue;
}
if (key === '__proto__') {
// e.g. someone tried to sneak __proto__ into this object and that
// will cause havoc when assigning it to a new object (in case its impure)
return createFail(failOrThrow, "invalid key in stringIndex: " + debugValue(k), v);
return createFail(failOrThrow, "invalid key in dictionary: " + debugValue(key), v);
}
var value = t(o[k], failSymbol);
var keyOrFail = keyRuntype(key, failOrThrow);
if (isFail(value)) {
return propagateFail(failOrThrow, value, v, k);
if (isFail(keyOrFail)) {
return propagateFail(failOrThrow, keyOrFail, v);
}
var value = o[key];
var valueOrFail = valueRuntype(value, failOrThrow);
if (isFail(valueOrFail)) {
return propagateFail(failOrThrow, valueOrFail, v);
}
if (!isPure) {
res[k] = value;
res[keyOrFail] = valueOrFail;
}

@@ -1033,48 +1091,12 @@ }

}
/**
* An index with number keys.
* An object that matches a Typecript `Record<KeyType, ValueType>` type.
*
* You pass a runtype for the objects keys and one for its values.
* Keeps you save from unwanted propertiers and evil __proto__ injections.
*/
function numberIndex(t) {
var isPure = isPureRuntype(t);
return internalRuntype(function (v, failOrThrow) {
var o = objectRuntype(v, failOrThrow);
if (isFail(o)) {
return propagateFail(failOrThrow, o, v);
}
if (Object.getOwnPropertySymbols(o).length) {
return createFail(failOrThrow, "invalid key in stringIndex: " + debugValue(Object.getOwnPropertySymbols(o)), v);
}
var res = isPure ? o : {};
for (var k in o) {
if (k === '__proto__') {
// e.g. someone tried to sneak __proto__ into this object and that
// will cause havoc when assigning it to a new object (in case its impure)
return createFail(failOrThrow, "invalid key in stringIndex: " + debugValue(k), v);
}
var key = stringAsIntegerRuntype(k, failOrThrow);
if (isFail(key)) {
return propagateFail(failOrThrow, key, v);
}
var value = t(o[key], failSymbol);
if (isFail(value)) {
return propagateFail(failOrThrow, value, v, key);
}
if (!isPure) {
res[key] = value;
}
}
return res;
}, true);
function dictionary(keyRuntype, valueRuntype) {
return dictionaryRuntype(keyRuntype, valueRuntype);
}

@@ -1318,3 +1340,4 @@

delete newRecordFields[k];
});
}); // TODO: keep 'sloppyness'
return record(newRecordFields);

@@ -1344,4 +1367,5 @@ }

}
}
} // TODO: keep 'sloppyness'
return record(newRecordFields);

@@ -1369,7 +1393,8 @@ }

newRecordFields[k] = fields[k];
});
}); // TODO: keep 'sloppyness'
return record(newRecordFields);
}
export { RuntypeError, RuntypeUsageError, any, array, _boolean as boolean, createError, enumRuntype as enum, getFormattedError, getFormattedErrorPath, getFormattedErrorValue, guardedBy, ignore, integer, intersection, literal, nullRuntype as null, nullable, number, numberIndex, object, omit, optional, partial, pick, record, runtype, sloppyRecord, string, stringAsInteger, stringIndex, stringLiteralUnion, tuple, undefinedRuntype as undefined, union, unknown, use };
export { RuntypeError, RuntypeUsageError, any, array, _boolean as boolean, createError, dictionary, enumRuntype as enum, getFormattedError, getFormattedErrorPath, getFormattedErrorValue, guardedBy, ignore, integer, intersection, literal, nullRuntype as null, nullOr, number, object, omit, optional, partial, pick, record, runtype, sloppyRecord, string, stringAsInteger, stringLiteralUnion, tuple, undefinedRuntype as undefined, undefinedOr, union, unknown, use };
//# sourceMappingURL=simple-runtypes.esm.js.map
{
"name": "simple-runtypes",
"version": "6.3.0",
"version": "7.0.0",
"license": "MIT",

@@ -9,13 +9,15 @@ "author": "Erik Söhnel",

"module": "dist/simple-runtypes.esm.js",
"typings": "dist/index.d.ts",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"sideEffects": false,
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"build": "tsdx build --tsconfig tsconfig.build.json",
"test": "tsdx test",
"test:watch": "tsdx test --watchAll",
"test:types": "tsd",
"lint": "tsdx lint",
"prepare": "tsdx build",
"prepare": "tsdx build --tsconfig tsconfig.build.json",
"build-readme": "ts-node --compiler-options '{\"module\": \"CommonJS\"}' ./scripts/build-readme-references.ts && markdown-toc -i --maxdepth 4 README.md"

@@ -59,2 +61,5 @@ },

},
"tsd": {
"directory": "test-d"
},
"devDependencies": {

@@ -67,2 +72,3 @@ "@types/jest": "^25.2.1",

"ts-node": "^9.0.0",
"tsd": "^0.14.0",
"tsdx": "^0.13.1",

@@ -69,0 +75,0 @@ "tslib": "^1.11.1",

@@ -83,3 +83,3 @@ [![npm version](https://badge.fury.io/js/simple-runtypes.svg)](https://www.npmjs.com/package/simple-runtypes)

Not throwing errors is way more efficient and less obscure.
Throwing errors and catching them outside may be more convenient in some situations.
Throwing errors and catching them outside is more convenient.

@@ -90,12 +90,13 @@ ## Why?

1. Written in and for Typescript
2. Strict by default (e.g. safe against proto injection attacks)
3. Supports efficient discriminated unions
4. Frontend-friendly (no `eval`, small [footprint](https://bundlephobia.com/result?p=simple-runtypes), no dependencies)
6. Fast (of all non-eval based libs, only one is faster according to the [benchmark](https://github.com/moltar/typescript-runtime-type-benchmarks))
1. Strict: by default safe against proto injection attacks and unwanted properties
2. Fast: check the [benchmark](https://github.com/moltar/typescript-runtime-type-benchmarks)
3. Friendly: no use of `eval`, a small [footprint](https://bundlephobia.com/result?p=simple-runtypes) and no dependencies
4. Flexible: optionally modify the data while its being checked: trim strings, convert numbers, parse dates
## Benchmarks
[@moltar](https://github.com/moltar) has done a great job comparing existing runtime typechecking libraries in [moltar/typescript-runtime-type-benchmarks](https://github.com/moltar/typescript-runtime-type-benchmarks)
[@moltar](https://github.com/moltar) has done a great job comparing existing runtime typechecking libraries in [moltar/typescript-runtime-type-benchmarks](https://github.com/moltar/typescript-runtime-type-benchmarks).
[@pongo](https://github.com/pongo) has benchmarked [simple-runtypes](https://github.com/hoeck/simple-runtypes) against [io-ts](https://github.com/gcanti/io-ts) in [pongo/benchmark-simple-runtypes](https://github.com/pongo/benchmark-simple-runtypes).
## Documentation

@@ -126,3 +127,3 @@

When using [`record`](src/record.ts#L97), any properties which are not defined in the runtype will cause the runtype to fail:
When using [`record`](src/record.ts#L134), any properties which are not defined in the runtype will cause the runtype to fail:

@@ -145,3 +146,3 @@ ```typescript

Use [`sloppyRecord`](src/record.ts#L111) to only validate known properties and remove everything else:
Use [`sloppyRecord`](src/record.ts#L159) to only validate known properties and remove everything else:

@@ -155,7 +156,7 @@ ```typescript

Using any of [`record`](src/record.ts#L97) or [`sloppyRecord`](src/record.ts#L111) will keep you safe from any `__proto__` injection or overriding attempts.
Using any of [`record`](src/record.ts#L134) or [`sloppyRecord`](src/record.ts#L159) will keep you safe from any `__proto__` injection or overriding attempts.
#### Optional Properties
Use the [`optional`](src/optional.ts#L11) runtype to create [optional properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#optional-properties):
Use the [`optional`](src/optional.ts#L18) runtype to create [optional properties](https://www.typescriptlang.org/docs/handbook/interfaces.html#optional-properties):

@@ -171,3 +172,3 @@ ```typescript

Collection runtypes such as [`record`](src/record.ts#L97), [`array`](src/array.ts#L28), [`tuple`](src/tuple.ts#L42) take runtypes as their parameters:
Collection runtypes such as [`record`](src/record.ts#L134), [`array`](src/array.ts#L28), [`tuple`](src/tuple.ts#L42) take runtypes as their parameters:

@@ -267,11 +268,9 @@ ```typescript

- [`array`](src/array.ts#L28)
- [`record`](src/record.ts#L97)
- [`sloppyRecord`](src/record.ts#L111)
- [`numberIndex`](src/numberIndex.ts#L18)
- [`stringIndex`](src/stringIndex.ts#L17)
- [`record`](src/record.ts#L134)
- [`optional`](src/optional.ts#L18)
- [`sloppyRecord`](src/record.ts#L159)
- [`dictionary`](src/dictionary.ts#L87)
Combinators
- [`optional`](src/optional.ts#L11)
- [`nullable`](src/nullable.ts#L11)
- [`union`](src/union.ts#L143)

@@ -284,2 +283,7 @@ - [`intersection`](src/intersection.ts#L110)

Shortcuts
- [`nullOr`](src/nullOr.ts#L11)
- [`undefinedOr`](src/undefinedOr.ts#L11)
### Roadmap / Todos

@@ -290,3 +294,3 @@

on all types that [`literal`](src/literal.ts#L10) accepts
- rename [`sloppyRecord`](src/record.ts#L111) to `record.sloppy` because I need
- rename [`sloppyRecord`](src/record.ts#L159) to `record.sloppy` because I need
the "sloppy"-concept for other runtypes too: e.g. `nullable.sloppy` - a

@@ -297,3 +301,3 @@ `Runtype<T | null>` that also accepts `undefined` which is useful to slowly

- *preface*: what is a runtype and why is it useful
- *why*: explain or link to example that shows "strict by default" and "efficient discriminating unions"
- *why*: explain or link to example that shows "strict by default"
- show that simple-runtypes is feature complete because it can

@@ -300,0 +304,0 @@ 1. express all typescript types

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc