simple-runtypes
Advanced tools
Comparing version 6.1.0 to 6.1.1
@@ -0,1 +1,5 @@ | ||
### 6.1.1 | ||
- fix and improve error value and path formatting (shout outs to @pabra for the patch) | ||
### 6.1.0 | ||
@@ -2,0 +6,0 @@ |
@@ -5,7 +5,24 @@ import { Runtype } from './runtype'; | ||
} | undefined; | ||
/** | ||
* An object with defined keys and values. | ||
* | ||
* In contrast to typescript types, objects checked by this runtype will fail | ||
* if they have any additional keys (strict checking) not specified in | ||
* typemap. | ||
* | ||
* Keeps you save from unwanted propertiers and evil __proto__ injections. | ||
*/ | ||
export declare function record<T extends object>(typemap: { | ||
[K in keyof T]: Runtype<T[K]>; | ||
}): Runtype<T>; | ||
/** | ||
* Like record but ignore unknown keys. | ||
* | ||
* Returns a new object that only contains the keys specified in the typemap. | ||
* Additional keys are ignored. | ||
* | ||
* Keeps you save from unwanted propertiers and evil __proto__ injections. | ||
*/ | ||
export declare function sloppyRecord<T extends object>(typemap: { | ||
[K in keyof T]: Runtype<T[K]>; | ||
}): Runtype<T>; |
@@ -17,2 +17,8 @@ 'use strict'; | ||
return 'undefined'; | ||
} // `JSON.stringify(fn)` would return `undefined` thus `s.length` would become | ||
// `undefined.length` which would fail | ||
if (typeof v === 'function') { | ||
return v.toString(); | ||
} | ||
@@ -33,7 +39,15 @@ | ||
/** | ||
* Return boolean to indicate whether passed object seems to be an RuntypeError | ||
*/ | ||
function isRuntypeErrorPath(e) { | ||
return Array.isArray(e.path); | ||
} | ||
/** | ||
* Return the object path at which the error occured. | ||
*/ | ||
function getFormattedErrorPath(e) { | ||
if (!Array.isArray(e.path)) { | ||
if (!isRuntypeErrorPath(e)) { | ||
return '(error is not a RuntypeError!)'; | ||
@@ -45,5 +59,6 @@ } // path in Fail objects is with the root-element at the end bc. its easier | ||
var pathInRootElementFirstOrder = [].concat(e.path).reverse(); | ||
return pathInRootElementFirstOrder.map(function (k) { | ||
return typeof k === 'number' ? "[" + k + "]" : /^([A-z0-9_])+$/.test(k) ? "." + k : "['" + JSON.stringify(k) + "']"; | ||
}).join('').slice(1); | ||
var formattedErrorPath = pathInRootElementFirstOrder.map(function (k) { | ||
return typeof k === 'number' ? "[" + k + "]" : /^\w+$/.test(k) ? "." + k : "['" + JSON.stringify(k) + "']"; | ||
}).join(''); | ||
return formattedErrorPath.startsWith('.') ? formattedErrorPath.slice(1) : formattedErrorPath; | ||
} | ||
@@ -61,3 +76,38 @@ /** | ||
return debugValue(e.value, maxLength); | ||
if (!isRuntypeErrorPath(e)) { | ||
return '(error is not a RuntypeError!)'; | ||
} | ||
var _e$path$reduceRight = e.path.reduceRight(function (_ref, key) { | ||
var value = _ref.value, | ||
isResolvable = _ref.isResolvable; | ||
// we have not not been able to resolve the value previously - don't try any further | ||
if (!isResolvable) { | ||
return { | ||
value: value, | ||
isResolvable: isResolvable | ||
}; | ||
} // try to resolve key within objects or arrays | ||
if (key in value) { | ||
return { | ||
value: value[key], | ||
isResolvable: isResolvable | ||
}; | ||
} // otherwise return last value successfully resolved and mark as "not further resolvable" | ||
return { | ||
value: value, | ||
isResolvable: false | ||
}; | ||
}, { | ||
value: e.value, | ||
isResolvable: true | ||
}), | ||
resolvedValue = _e$path$reduceRight.value; | ||
return debugValue(resolvedValue, maxLength); | ||
} | ||
@@ -76,6 +126,6 @@ /** | ||
var rawPath = getFormattedErrorPath(e); | ||
var path = rawPath ? "<value>." + rawPath : '<value>'; | ||
var path = rawPath ? "<value>" + (rawPath.startsWith('[') ? '' : '.') + rawPath : '<value>'; | ||
var label = 'name' in e ? e.name + ": " : ''; | ||
var value = getFormattedErrorValue(e, maxLength); | ||
return "" + label + e.reason + " at `" + path + "` in `" + value + "`"; | ||
return "" + label + e.reason + " at `" + path + "` for `" + value + "`"; | ||
} | ||
@@ -852,6 +902,2 @@ | ||
/** | ||
* An object with defined keys and values. | ||
*/ | ||
function internalRecord(typemap, sloppy) { | ||
@@ -918,5 +964,24 @@ var isPure = Object.values(typemap).every(function (t) { | ||
} | ||
/** | ||
* An object with defined keys and values. | ||
* | ||
* In contrast to typescript types, objects checked by this runtype will fail | ||
* if they have any additional keys (strict checking) not specified in | ||
* typemap. | ||
* | ||
* Keeps you save from unwanted propertiers and evil __proto__ injections. | ||
*/ | ||
function record(typemap) { | ||
return internalRecord(typemap, false); | ||
} | ||
/** | ||
* Like record but ignore unknown keys. | ||
* | ||
* Returns a new object that only contains the keys specified in the typemap. | ||
* Additional keys are ignored. | ||
* | ||
* Keeps you save from unwanted propertiers and evil __proto__ injections. | ||
*/ | ||
function sloppyRecord(typemap) { | ||
@@ -923,0 +988,0 @@ return internalRecord(typemap, true); |
@@ -1,2 +0,2 @@ | ||
"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}}; | ||
"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),_=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 R(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 P(r){return R(r,!1)}function E(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 I(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 k(){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=I(e);if(void 0!==n)return E(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 N(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 k.apply(void 0,t.map((function(r){return F(r,e)})))}function F(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]=M(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 P(t)}(r,e);if("unions"in r&&"fields"in e)return N(r,e);if("unions"in e&&"fields"in r)return N(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 M(){if(2===arguments.length)return F(arguments.length<=0?void 0:arguments[0],arguments.length<=1?void 0:arguments[1]);if(3===arguments.length)return M(F(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 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=M,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=_(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]})),P(t)},exports.optional=function(r){return g((function(e,t){if(void 0!==e)return r(e,t)}),x(r))},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]})),P(t)},exports.record=P,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 R(r,!0)},exports.string=function(r){if(!r)return O;var e=r.maxLength,t=r.trim;return g((function(r,n){var o=O(r,n);return b(o)?y(n,o,r):void 0!==e&&o.length>e?d(n,"expected the string length to not exceed "+e,r):t?o.trim():o}),!t)},exports.stringAsInteger=function(r){if(!r)return _;var e=r.min,t=r.max;return g((function(r,n){var o=_(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=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.union=k,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 |
@@ -13,2 +13,8 @@ /** | ||
return 'undefined'; | ||
} // `JSON.stringify(fn)` would return `undefined` thus `s.length` would become | ||
// `undefined.length` which would fail | ||
if (typeof v === 'function') { | ||
return v.toString(); | ||
} | ||
@@ -29,7 +35,15 @@ | ||
/** | ||
* Return boolean to indicate whether passed object seems to be an RuntypeError | ||
*/ | ||
function isRuntypeErrorPath(e) { | ||
return Array.isArray(e.path); | ||
} | ||
/** | ||
* Return the object path at which the error occured. | ||
*/ | ||
function getFormattedErrorPath(e) { | ||
if (!Array.isArray(e.path)) { | ||
if (!isRuntypeErrorPath(e)) { | ||
return '(error is not a RuntypeError!)'; | ||
@@ -41,5 +55,6 @@ } // path in Fail objects is with the root-element at the end bc. its easier | ||
var pathInRootElementFirstOrder = [].concat(e.path).reverse(); | ||
return pathInRootElementFirstOrder.map(function (k) { | ||
return typeof k === 'number' ? "[" + k + "]" : /^([A-z0-9_])+$/.test(k) ? "." + k : "['" + JSON.stringify(k) + "']"; | ||
}).join('').slice(1); | ||
var formattedErrorPath = pathInRootElementFirstOrder.map(function (k) { | ||
return typeof k === 'number' ? "[" + k + "]" : /^\w+$/.test(k) ? "." + k : "['" + JSON.stringify(k) + "']"; | ||
}).join(''); | ||
return formattedErrorPath.startsWith('.') ? formattedErrorPath.slice(1) : formattedErrorPath; | ||
} | ||
@@ -57,3 +72,38 @@ /** | ||
return debugValue(e.value, maxLength); | ||
if (!isRuntypeErrorPath(e)) { | ||
return '(error is not a RuntypeError!)'; | ||
} | ||
var _e$path$reduceRight = e.path.reduceRight(function (_ref, key) { | ||
var value = _ref.value, | ||
isResolvable = _ref.isResolvable; | ||
// we have not not been able to resolve the value previously - don't try any further | ||
if (!isResolvable) { | ||
return { | ||
value: value, | ||
isResolvable: isResolvable | ||
}; | ||
} // try to resolve key within objects or arrays | ||
if (key in value) { | ||
return { | ||
value: value[key], | ||
isResolvable: isResolvable | ||
}; | ||
} // otherwise return last value successfully resolved and mark as "not further resolvable" | ||
return { | ||
value: value, | ||
isResolvable: false | ||
}; | ||
}, { | ||
value: e.value, | ||
isResolvable: true | ||
}), | ||
resolvedValue = _e$path$reduceRight.value; | ||
return debugValue(resolvedValue, maxLength); | ||
} | ||
@@ -72,6 +122,6 @@ /** | ||
var rawPath = getFormattedErrorPath(e); | ||
var path = rawPath ? "<value>." + rawPath : '<value>'; | ||
var path = rawPath ? "<value>" + (rawPath.startsWith('[') ? '' : '.') + rawPath : '<value>'; | ||
var label = 'name' in e ? e.name + ": " : ''; | ||
var value = getFormattedErrorValue(e, maxLength); | ||
return "" + label + e.reason + " at `" + path + "` in `" + value + "`"; | ||
return "" + label + e.reason + " at `" + path + "` for `" + value + "`"; | ||
} | ||
@@ -848,6 +898,2 @@ | ||
/** | ||
* An object with defined keys and values. | ||
*/ | ||
function internalRecord(typemap, sloppy) { | ||
@@ -914,5 +960,24 @@ var isPure = Object.values(typemap).every(function (t) { | ||
} | ||
/** | ||
* An object with defined keys and values. | ||
* | ||
* In contrast to typescript types, objects checked by this runtype will fail | ||
* if they have any additional keys (strict checking) not specified in | ||
* typemap. | ||
* | ||
* Keeps you save from unwanted propertiers and evil __proto__ injections. | ||
*/ | ||
function record(typemap) { | ||
return internalRecord(typemap, false); | ||
} | ||
/** | ||
* Like record but ignore unknown keys. | ||
* | ||
* Returns a new object that only contains the keys specified in the typemap. | ||
* Additional keys are ignored. | ||
* | ||
* Keeps you save from unwanted propertiers and evil __proto__ injections. | ||
*/ | ||
function sloppyRecord(typemap) { | ||
@@ -919,0 +984,0 @@ return internalRecord(typemap, true); |
{ | ||
"name": "simple-runtypes", | ||
"version": "6.1.0", | ||
"version": "6.1.1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Erik Söhnel", |
[![npm version](https://badge.fury.io/js/simple-runtypes.svg)](https://www.npmjs.com/package/simple-runtypes) | ||
[![unit-tests](https://github.com/hoeck/simple-runtypes/workflows/unit-tests/badge.svg?branch=master)](https://github.com/hoeck/simple-runtypes/actions?query=workflow%3Aunit-tests+branch%3Amaster) | ||
[![npm-publish](https://github.com/hoeck/simple-runtypes/workflows/npm-publish/badge.svg?branch=master)](https://github.com/hoeck/simple-runtypes/actions?query=workflow%3Anpm-publish+branch%3Amaster) | ||
[![npm-publish](https://github.com/hoeck/simple-runtypes/workflows/npm-publish/badge.svg)](https://github.com/hoeck/simple-runtypes/actions?query=workflow%3Anpm-publish) | ||
@@ -65,3 +65,3 @@ ## Preface | ||
userRuntype({id: 1, name: 'matt', isAdmin: true}) | ||
// throws a st.RuntypeError 'invalid field 'isAdmin' in data | ||
// throws an st.RuntypeError: "invalid field 'isAdmin' in data" | ||
``` | ||
@@ -79,3 +79,3 @@ | ||
st.getFormattedError(FAIL) | ||
// => 'invalid keys in record ["c"] at `<value>` in `{"a":1,"b":"as","c":false}`' | ||
// => 'invalid keys in record ["isAdmin"] at `<value>` in `{"id":1,"name": "matt", ... }`' | ||
``` | ||
@@ -227,2 +227,3 @@ | ||
- [`enum`](src/enum.ts#9) | ||
- [`literal`](src/literal.ts#L10) | ||
@@ -246,5 +247,4 @@ Meta runtypes | ||
Advanced Runtypes | ||
Combinators | ||
- [`literal`](src/literal.ts#L10) | ||
- [`optional`](src/optional.ts#L11) | ||
@@ -255,5 +255,7 @@ - [`nullable`](src/nullable.ts#L11) | ||
- [`omit`](src/omit.ts#L8) | ||
- TODO: `get` - similar to Type[key] | ||
### Roadmap / Todos | ||
- `size` - a meta-runtype that imposes a size limit on types, maybe via convert-to-json and .length on the value passed to it | ||
- rename `stringLiteralUnion` to `literals` or `literalUnion` and make it work | ||
@@ -260,0 +262,0 @@ on all types that `literal` accepts |
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
299302
2594
272