Comparing version 4.0.2 to 4.0.3
@@ -80,8 +80,6 @@ "use strict"; | ||
var parameterIndex = validParameterIndices[typeIndex]; | ||
try { | ||
type.check(args[parameterIndex]); | ||
var validated = type.validate(args[parameterIndex]); | ||
if (!validated.success) { | ||
throw new errors_1.ValidationError(methodId + ", argument #" + parameterIndex + ": " + validated.message); | ||
} | ||
catch (err) { | ||
throw new errors_1.ValidationError(methodId + ", argument #" + parameterIndex + ": " + err.message); | ||
} | ||
}); | ||
@@ -88,0 +86,0 @@ return method.apply(this, args); |
113
lib/show.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var show = function (needsParens, circular) { return function (refl) { | ||
var parenthesize = function (s) { return (needsParens ? "(" + s + ")" : s); }; | ||
if (circular) { | ||
if (circular.indexOf(refl) !== -1) { | ||
var show = function (needsParens, circular) { | ||
if (circular === void 0) { circular = new Set(); } | ||
return function (refl) { | ||
var parenthesize = function (s) { return (needsParens ? "(" + s + ")" : s); }; | ||
if (circular.has(refl)) { | ||
return parenthesize("CIRCULAR " + refl.tag); | ||
} | ||
circular.push(refl); | ||
} | ||
switch (refl.tag) { | ||
// Primitive types | ||
case 'unknown': | ||
case 'never': | ||
case 'void': | ||
case 'boolean': | ||
case 'number': | ||
case 'string': | ||
case 'symbol': | ||
case 'function': | ||
return refl.tag; | ||
// Complex types | ||
case 'literal': { | ||
var value = refl.value; | ||
return typeof value === 'string' ? "\"" + value + "\"" : String(value); | ||
circular.add(refl); | ||
try { | ||
switch (refl.tag) { | ||
// Primitive types | ||
case 'unknown': | ||
case 'never': | ||
case 'void': | ||
case 'boolean': | ||
case 'number': | ||
case 'string': | ||
case 'symbol': | ||
case 'function': | ||
return refl.tag; | ||
// Complex types | ||
case 'literal': { | ||
var value = refl.value; | ||
return typeof value === 'string' ? "\"" + value + "\"" : String(value); | ||
} | ||
case 'array': | ||
return "" + readonlyTag(refl) + show(true, circular)(refl.element) + "[]"; | ||
case 'dictionary': | ||
return "{ [_: " + refl.key + "]: " + show(false, circular)(refl.value) + " }"; | ||
case 'record': { | ||
var keys = Object.keys(refl.fields); | ||
return keys.length | ||
? "{ " + keys | ||
.map(function (k) { return "" + readonlyTag(refl) + k + ": " + show(false, circular)(refl.fields[k]) + ";"; }) | ||
.join(' ') + " }" | ||
: '{}'; | ||
} | ||
case 'partial': { | ||
var keys = Object.keys(refl.fields); | ||
return keys.length | ||
? "{ " + keys.map(function (k) { return k + "?: " + show(false, circular)(refl.fields[k]) + ";"; }).join(' ') + " }" | ||
: '{}'; | ||
} | ||
case 'tuple': | ||
return "[" + refl.components.map(show(false, circular)).join(', ') + "]"; | ||
case 'union': | ||
return parenthesize("" + refl.alternatives.map(show(true, circular)).join(' | ')); | ||
case 'intersect': | ||
return parenthesize("" + refl.intersectees.map(show(true, circular)).join(' & ')); | ||
case 'constraint': | ||
return refl.name || show(needsParens, circular)(refl.underlying); | ||
case 'instanceof': | ||
var name_1 = refl.ctor.name; | ||
return "InstanceOf<" + name_1 + ">"; | ||
case 'brand': | ||
return show(needsParens, circular)(refl.entity); | ||
} | ||
} | ||
case 'array': | ||
return "" + readonlyTag(refl) + show(true, circular || [])(refl.element) + "[]"; | ||
case 'dictionary': | ||
return "{ [_: " + refl.key + "]: " + show(false, circular || [])(refl.value) + " }"; | ||
case 'record': { | ||
var keys = Object.keys(refl.fields); | ||
return keys.length | ||
? "{ " + keys | ||
.map(function (k) { return "" + readonlyTag(refl) + k + ": " + show(false, circular || [])(refl.fields[k]) + ";"; }) | ||
.join(' ') + " }" | ||
: '{}'; | ||
finally { | ||
circular.delete(refl); | ||
} | ||
case 'partial': { | ||
var keys = Object.keys(refl.fields); | ||
return keys.length | ||
? "{ " + keys.map(function (k) { return k + "?: " + show(false, circular || [])(refl.fields[k]) + ";"; }).join(' ') + " }" | ||
: '{}'; | ||
} | ||
case 'tuple': | ||
return "[" + refl.components.map(show(false, circular || [])).join(', ') + "]"; | ||
case 'union': | ||
return parenthesize("" + refl.alternatives.map(show(true, circular || [])).join(' | ')); | ||
case 'intersect': | ||
return parenthesize("" + refl.intersectees.map(show(true, circular || [])).join(' & ')); | ||
case 'constraint': | ||
return refl.name || show(needsParens, circular || [])(refl.underlying); | ||
case 'instanceof': | ||
var name_1 = refl.ctor.name; | ||
return "InstanceOf<" + name_1 + ">"; | ||
case 'brand': | ||
return show(needsParens, circular || [])(refl.entity); | ||
} | ||
}; }; | ||
throw Error('impossible'); | ||
}; | ||
}; | ||
exports.default = show(false); | ||
@@ -61,0 +68,0 @@ function readonlyTag(_a) { |
{ | ||
"name": "runtypes", | ||
"version": "4.0.2", | ||
"version": "4.0.3", | ||
"description": "Runtime validation for static types", | ||
@@ -16,8 +16,8 @@ "main": "./lib/index.js", | ||
"devDependencies": { | ||
"@types/jest": "24.0.13", | ||
"coveralls": "^3.0.3", | ||
"jest": "24.8.0", | ||
"prettier": "^1.17.1", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "3.5.1" | ||
"@types/jest": "24.0.18", | ||
"coveralls": "^3.0.6", | ||
"jest": "24.9.0", | ||
"prettier": "^1.18.2", | ||
"ts-jest": "^24.1.0", | ||
"typescript": "3.6.3" | ||
}, | ||
@@ -24,0 +24,0 @@ "keywords:": [ |
@@ -81,6 +81,7 @@ import { Runtype } from './runtype'; | ||
const parameterIndex = validParameterIndices[typeIndex]; | ||
try { | ||
type.check(args[parameterIndex]); | ||
} catch (err) { | ||
throw new ValidationError(`${methodId}, argument #${parameterIndex}: ${err.message}`); | ||
const validated = type.validate(args[parameterIndex]); | ||
if (!validated.success) { | ||
throw new ValidationError( | ||
`${methodId}, argument #${parameterIndex}: ${validated.message}`, | ||
); | ||
} | ||
@@ -87,0 +88,0 @@ }); |
@@ -226,3 +226,4 @@ import { | ||
fail('contract was violated but no exception was thrown'); | ||
} catch (e) { | ||
} catch (exception) { | ||
expect(exception).toBeInstanceOf(ValidationError); | ||
/* success */ | ||
@@ -240,3 +241,4 @@ } | ||
fail('contract was violated but no exception was thrown'); | ||
} catch (e) { | ||
} catch (exception) { | ||
expect(exception).toBeInstanceOf(ValidationError); | ||
/* success */ | ||
@@ -254,3 +256,4 @@ } | ||
fail('contract was violated but no exception was thrown'); | ||
} catch (e) { | ||
} catch (exception) { | ||
expect(exception).toBeInstanceOf(ValidationError); | ||
/* success */ | ||
@@ -257,0 +260,0 @@ } |
@@ -53,2 +53,3 @@ import { | ||
[Record({ x: String, y: Array(Boolean) }), '{ x: string; y: boolean[]; }'], | ||
[Record({ x: Number }).And(Partial({ y: Number })), '{ x: number; } & { y?: number; }'], | ||
[ | ||
@@ -55,0 +56,0 @@ Record({ x: String, y: Array(Boolean) }).asReadonly(), |
109
src/show.ts
import { Reflect } from './index'; | ||
const show = (needsParens: boolean, circular?: Reflect[]) => (refl: Reflect): string => { | ||
const show = (needsParens: boolean, circular = new Set<Reflect>()) => (refl: Reflect): string => { | ||
const parenthesize = (s: string) => (needsParens ? `(${s})` : s); | ||
if (circular) { | ||
if (circular.indexOf(refl) !== -1) { | ||
return parenthesize(`CIRCULAR ${refl.tag}`); | ||
} | ||
circular.push(refl); | ||
if (circular.has(refl)) { | ||
return parenthesize(`CIRCULAR ${refl.tag}`); | ||
} | ||
switch (refl.tag) { | ||
// Primitive types | ||
case 'unknown': | ||
case 'never': | ||
case 'void': | ||
case 'boolean': | ||
case 'number': | ||
case 'string': | ||
case 'symbol': | ||
case 'function': | ||
return refl.tag; | ||
circular.add(refl); | ||
// Complex types | ||
case 'literal': { | ||
const { value } = refl; | ||
return typeof value === 'string' ? `"${value}"` : String(value); | ||
try { | ||
switch (refl.tag) { | ||
// Primitive types | ||
case 'unknown': | ||
case 'never': | ||
case 'void': | ||
case 'boolean': | ||
case 'number': | ||
case 'string': | ||
case 'symbol': | ||
case 'function': | ||
return refl.tag; | ||
// Complex types | ||
case 'literal': { | ||
const { value } = refl; | ||
return typeof value === 'string' ? `"${value}"` : String(value); | ||
} | ||
case 'array': | ||
return `${readonlyTag(refl)}${show(true, circular)(refl.element)}[]`; | ||
case 'dictionary': | ||
return `{ [_: ${refl.key}]: ${show(false, circular)(refl.value)} }`; | ||
case 'record': { | ||
const keys = Object.keys(refl.fields); | ||
return keys.length | ||
? `{ ${keys | ||
.map(k => `${readonlyTag(refl)}${k}: ${show(false, circular)(refl.fields[k])};`) | ||
.join(' ')} }` | ||
: '{}'; | ||
} | ||
case 'partial': { | ||
const keys = Object.keys(refl.fields); | ||
return keys.length | ||
? `{ ${keys.map(k => `${k}?: ${show(false, circular)(refl.fields[k])};`).join(' ')} }` | ||
: '{}'; | ||
} | ||
case 'tuple': | ||
return `[${refl.components.map(show(false, circular)).join(', ')}]`; | ||
case 'union': | ||
return parenthesize(`${refl.alternatives.map(show(true, circular)).join(' | ')}`); | ||
case 'intersect': | ||
return parenthesize(`${refl.intersectees.map(show(true, circular)).join(' & ')}`); | ||
case 'constraint': | ||
return refl.name || show(needsParens, circular)(refl.underlying); | ||
case 'instanceof': | ||
const name = (refl.ctor as any).name; | ||
return `InstanceOf<${name}>`; | ||
case 'brand': | ||
return show(needsParens, circular)(refl.entity); | ||
} | ||
case 'array': | ||
return `${readonlyTag(refl)}${show(true, circular || [])(refl.element)}[]`; | ||
case 'dictionary': | ||
return `{ [_: ${refl.key}]: ${show(false, circular || [])(refl.value)} }`; | ||
case 'record': { | ||
const keys = Object.keys(refl.fields); | ||
return keys.length | ||
? `{ ${keys | ||
.map(k => `${readonlyTag(refl)}${k}: ${show(false, circular || [])(refl.fields[k])};`) | ||
.join(' ')} }` | ||
: '{}'; | ||
} | ||
case 'partial': { | ||
const keys = Object.keys(refl.fields); | ||
return keys.length | ||
? `{ ${keys.map(k => `${k}?: ${show(false, circular || [])(refl.fields[k])};`).join(' ')} }` | ||
: '{}'; | ||
} | ||
case 'tuple': | ||
return `[${refl.components.map(show(false, circular || [])).join(', ')}]`; | ||
case 'union': | ||
return parenthesize(`${refl.alternatives.map(show(true, circular || [])).join(' | ')}`); | ||
case 'intersect': | ||
return parenthesize(`${refl.intersectees.map(show(true, circular || [])).join(' & ')}`); | ||
case 'constraint': | ||
return refl.name || show(needsParens, circular || [])(refl.underlying); | ||
case 'instanceof': | ||
const name = (refl.ctor as any).name; | ||
return `InstanceOf<${name}>`; | ||
case 'brand': | ||
return show(needsParens, circular || [])(refl.entity); | ||
} finally { | ||
circular.delete(refl); | ||
} | ||
throw Error('impossible'); | ||
}; | ||
@@ -64,0 +67,0 @@ |
@@ -102,3 +102,3 @@ import { Runtype, Static, create } from '../runtype'; | ||
Static<H>, | ||
Static<I> | ||
Static<I>, | ||
] | ||
@@ -133,3 +133,3 @@ > { | ||
Static<I>, | ||
Static<J> | ||
Static<J>, | ||
] | ||
@@ -241,5 +241,3 @@ > { | ||
success: false, | ||
message: `Expected an array of length ${components.length}, but was ${ | ||
validated.value.length | ||
}`, | ||
message: `Expected an array of length ${components.length}, but was ${validated.value.length}`, | ||
}; | ||
@@ -246,0 +244,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
211403
6174