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.0.1 to 6.1.0

4

CHANGELOG.md

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

### 6.1.0
- support intersection of unions and records
### 6.0.1

@@ -2,0 +6,0 @@

227

dist/simple-runtypes.cjs.development.js

@@ -1007,101 +1007,2 @@ 'use strict';

function recordIntersection(recordA, recordB) {
var fields = {};
var a = recordA.fields;
var b = recordB.fields;
for (var k in _extends({}, a, {}, b)) {
if (a[k] && b[k]) {
fields[k] = intersection(a[k], b[k]);
} else if (a[k]) {
fields[k] = a[k];
} else if (b[k]) {
fields[k] = b[k];
} else {
throw new RuntypeUsageError('invalid else');
}
}
return record(fields);
}
function intersection2(a, b) {
if ('fields' in a && 'fields' in b) {
return recordIntersection(a, b);
} else {
var isPure = isPureRuntype(a) && isPureRuntype(b);
return internalRuntype(function (v, failOrThrow) {
var valFromA = a(v, failOrThrow);
var valFromB = b(v, failOrThrow);
if (isFail(valFromB)) {
return propagateFail(failOrThrow, valFromB, v);
}
if (isFail(valFromA)) {
return propagateFail(failOrThrow, valFromA, v);
}
return valFromB; // second runtype arg is preferred
}, isPure);
}
}
function intersection() {
if (arguments.length === 2) {
return intersection2(arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1]);
} else if (arguments.length === 3) {
return intersection(intersection2(arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1]), arguments.length <= 2 ? undefined : arguments[2]);
} else {
throw new RuntypeUsageError("unsupported number of arguments " + arguments.length);
}
}
/**
* Build a new record runtype that omits some keys from the original.
*/
// TODO: should work with unions too!!!!!
function omit(original) {
var fields = original.fields;
if (!fields) {
throw new RuntypeUsageError("expected a record runtype");
}
var newRecordFields = _extends({}, fields);
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
keys[_key - 1] = arguments[_key];
}
keys.forEach(function (k) {
delete newRecordFields[k];
});
return record(newRecordFields);
}
/**
* Build a new record runtype that contains some keys from the original
*/
function pick(original) {
var fields = original.fields;
if (!fields) {
throw new RuntypeUsageError("expected a record runtype");
}
var newRecordFields = {};
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
keys[_key - 1] = arguments[_key];
}
keys.forEach(function (k) {
newRecordFields[k] = fields[k];
});
return record(newRecordFields);
}
// Runtypes must be created with `record(...)` which contains type metadata to

@@ -1135,3 +1036,3 @@ // identify the literals in each record.

});
return internalRuntype(function (v, failOrThrow) {
var resultingRuntype = internalRuntype(function (v, failOrThrow) {
var o = objectRuntype(v, failOrThrow);

@@ -1152,2 +1053,4 @@

}, isPure);
resultingRuntype.unions = runtypes;
return resultingRuntype;
} // given a list of runtypes, return the name of the key that acts as the

@@ -1217,3 +1120,3 @@ // unique discriminating value across all runtypes

} // optimize: when the union is a discriminating union, find the
// discriminating key and use it to efficiently validate the union
// discriminating key and use it to look up the runtype for the keys value

@@ -1249,2 +1152,124 @@

function recordIntersection2(recordA, recordB) {
var fields = {};
var a = recordA.fields;
var b = recordB.fields;
for (var k in _extends({}, a, {}, b)) {
if (a[k] && b[k]) {
fields[k] = intersection(a[k], b[k]);
} else if (a[k]) {
fields[k] = a[k];
} else if (b[k]) {
fields[k] = b[k];
} else {
throw new RuntypeUsageError('recordIntersection2: invalid else');
}
} // results in a new record type
return record(fields);
} // An intersection of a union with another type
function unionIntersection2(u, b) {
var unionRuntypes = u.unions;
if (!unionRuntypes || !Array.isArray(unionRuntypes) || !unionRuntypes.length) {
throw new RuntypeUsageError('unionIntersection2: first argument is not a union type');
} // results in a new union (because the intersection distributes over the union)
return union.apply(void 0, unionRuntypes.map(function (a) {
return intersection2(a, b);
}));
}
function intersection2(a, b) {
if ('fields' in a && 'fields' in b) {
return recordIntersection2(a, b);
} else if ('unions' in a && 'fields' in b) {
return unionIntersection2(a, b);
} else if ('unions' in b && 'fields' in a) {
return unionIntersection2(b, a);
} else if ('fields' in a || 'fields' in b) {
// Does such an intersection (e.g. string | {a: number} even make sense?
// And how would you implement it?
throw new RuntypeUsageError('intersection2: cannot intersect a base type with a record');
} else {
var isPure = isPureRuntype(a) && isPureRuntype(b);
return internalRuntype(function (v, failOrThrow) {
var valFromA = a(v, failOrThrow);
var valFromB = b(v, failOrThrow);
if (isFail(valFromB)) {
return propagateFail(failOrThrow, valFromB, v);
}
if (isFail(valFromA)) {
return propagateFail(failOrThrow, valFromA, v);
}
return valFromB; // second runtype arg is preferred
}, isPure);
}
}
function intersection() {
if (arguments.length === 2) {
return intersection2(arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1]);
} else if (arguments.length === 3) {
return intersection(intersection2(arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1]), arguments.length <= 2 ? undefined : arguments[2]);
} else {
throw new RuntypeUsageError("unsupported number of arguments " + arguments.length);
}
}
/**
* Build a new record runtype that omits some keys from the original.
*/
// TODO: should work with unions too!!!!!
function omit(original) {
var fields = original.fields;
if (!fields) {
throw new RuntypeUsageError("expected a record runtype");
}
var newRecordFields = _extends({}, fields);
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
keys[_key - 1] = arguments[_key];
}
keys.forEach(function (k) {
delete newRecordFields[k];
});
return record(newRecordFields);
}
/**
* Build a new record runtype that contains some keys from the original
*/
function pick(original) {
var fields = original.fields;
if (!fields) {
throw new RuntypeUsageError("expected a record runtype");
}
var newRecordFields = {};
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
keys[_key - 1] = arguments[_key];
}
keys.forEach(function (k) {
newRecordFields[k] = fields[k];
});
return record(newRecordFields);
}
exports.RuntypeError = RuntypeError;

@@ -1251,0 +1276,0 @@ exports.RuntypeUsageError = RuntypeUsageError;

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

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

@@ -1003,101 +1003,2 @@ /**

function recordIntersection(recordA, recordB) {
var fields = {};
var a = recordA.fields;
var b = recordB.fields;
for (var k in _extends({}, a, {}, b)) {
if (a[k] && b[k]) {
fields[k] = intersection(a[k], b[k]);
} else if (a[k]) {
fields[k] = a[k];
} else if (b[k]) {
fields[k] = b[k];
} else {
throw new RuntypeUsageError('invalid else');
}
}
return record(fields);
}
function intersection2(a, b) {
if ('fields' in a && 'fields' in b) {
return recordIntersection(a, b);
} else {
var isPure = isPureRuntype(a) && isPureRuntype(b);
return internalRuntype(function (v, failOrThrow) {
var valFromA = a(v, failOrThrow);
var valFromB = b(v, failOrThrow);
if (isFail(valFromB)) {
return propagateFail(failOrThrow, valFromB, v);
}
if (isFail(valFromA)) {
return propagateFail(failOrThrow, valFromA, v);
}
return valFromB; // second runtype arg is preferred
}, isPure);
}
}
function intersection() {
if (arguments.length === 2) {
return intersection2(arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1]);
} else if (arguments.length === 3) {
return intersection(intersection2(arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1]), arguments.length <= 2 ? undefined : arguments[2]);
} else {
throw new RuntypeUsageError("unsupported number of arguments " + arguments.length);
}
}
/**
* Build a new record runtype that omits some keys from the original.
*/
// TODO: should work with unions too!!!!!
function omit(original) {
var fields = original.fields;
if (!fields) {
throw new RuntypeUsageError("expected a record runtype");
}
var newRecordFields = _extends({}, fields);
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
keys[_key - 1] = arguments[_key];
}
keys.forEach(function (k) {
delete newRecordFields[k];
});
return record(newRecordFields);
}
/**
* Build a new record runtype that contains some keys from the original
*/
function pick(original) {
var fields = original.fields;
if (!fields) {
throw new RuntypeUsageError("expected a record runtype");
}
var newRecordFields = {};
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
keys[_key - 1] = arguments[_key];
}
keys.forEach(function (k) {
newRecordFields[k] = fields[k];
});
return record(newRecordFields);
}
// Runtypes must be created with `record(...)` which contains type metadata to

@@ -1131,3 +1032,3 @@ // identify the literals in each record.

});
return internalRuntype(function (v, failOrThrow) {
var resultingRuntype = internalRuntype(function (v, failOrThrow) {
var o = objectRuntype(v, failOrThrow);

@@ -1148,2 +1049,4 @@

}, isPure);
resultingRuntype.unions = runtypes;
return resultingRuntype;
} // given a list of runtypes, return the name of the key that acts as the

@@ -1213,3 +1116,3 @@ // unique discriminating value across all runtypes

} // optimize: when the union is a discriminating union, find the
// discriminating key and use it to efficiently validate the union
// discriminating key and use it to look up the runtype for the keys value

@@ -1245,3 +1148,125 @@

function recordIntersection2(recordA, recordB) {
var fields = {};
var a = recordA.fields;
var b = recordB.fields;
for (var k in _extends({}, a, {}, b)) {
if (a[k] && b[k]) {
fields[k] = intersection(a[k], b[k]);
} else if (a[k]) {
fields[k] = a[k];
} else if (b[k]) {
fields[k] = b[k];
} else {
throw new RuntypeUsageError('recordIntersection2: invalid else');
}
} // results in a new record type
return record(fields);
} // An intersection of a union with another type
function unionIntersection2(u, b) {
var unionRuntypes = u.unions;
if (!unionRuntypes || !Array.isArray(unionRuntypes) || !unionRuntypes.length) {
throw new RuntypeUsageError('unionIntersection2: first argument is not a union type');
} // results in a new union (because the intersection distributes over the union)
return union.apply(void 0, unionRuntypes.map(function (a) {
return intersection2(a, b);
}));
}
function intersection2(a, b) {
if ('fields' in a && 'fields' in b) {
return recordIntersection2(a, b);
} else if ('unions' in a && 'fields' in b) {
return unionIntersection2(a, b);
} else if ('unions' in b && 'fields' in a) {
return unionIntersection2(b, a);
} else if ('fields' in a || 'fields' in b) {
// Does such an intersection (e.g. string | {a: number} even make sense?
// And how would you implement it?
throw new RuntypeUsageError('intersection2: cannot intersect a base type with a record');
} else {
var isPure = isPureRuntype(a) && isPureRuntype(b);
return internalRuntype(function (v, failOrThrow) {
var valFromA = a(v, failOrThrow);
var valFromB = b(v, failOrThrow);
if (isFail(valFromB)) {
return propagateFail(failOrThrow, valFromB, v);
}
if (isFail(valFromA)) {
return propagateFail(failOrThrow, valFromA, v);
}
return valFromB; // second runtype arg is preferred
}, isPure);
}
}
function intersection() {
if (arguments.length === 2) {
return intersection2(arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1]);
} else if (arguments.length === 3) {
return intersection(intersection2(arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1]), arguments.length <= 2 ? undefined : arguments[2]);
} else {
throw new RuntypeUsageError("unsupported number of arguments " + arguments.length);
}
}
/**
* Build a new record runtype that omits some keys from the original.
*/
// TODO: should work with unions too!!!!!
function omit(original) {
var fields = original.fields;
if (!fields) {
throw new RuntypeUsageError("expected a record runtype");
}
var newRecordFields = _extends({}, fields);
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
keys[_key - 1] = arguments[_key];
}
keys.forEach(function (k) {
delete newRecordFields[k];
});
return record(newRecordFields);
}
/**
* Build a new record runtype that contains some keys from the original
*/
function pick(original) {
var fields = original.fields;
if (!fields) {
throw new RuntypeUsageError("expected a record runtype");
}
var newRecordFields = {};
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
keys[_key - 1] = arguments[_key];
}
keys.forEach(function (k) {
newRecordFields[k] = fields[k];
});
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, pick, record, runtype, sloppyRecord, string, stringAsInteger, stringIndex, stringLiteralUnion, tuple, undefinedRuntype as undefined, union, unknown, use };
//# sourceMappingURL=simple-runtypes.esm.js.map
{
"name": "simple-runtypes",
"version": "6.0.1",
"version": "6.1.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "author": "Erik Söhnel",

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

- rename `stringLiteralUnion` to `literals` or `literalUnion` and make it work
on all types that `literal` accepts
- rename [`sloppyRecord`](src/record.ts#L97) to `record.sloppy` because I need

@@ -256,0 +258,0 @@ the "sloppy"-concept for other runtypes too: e.g. `nullable.sloppy` - a

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