Comparing version 1.8.3 to 1.8.4
148
errors.js
var inherits = require('inherits') | ||
var native = require('./native') | ||
function getFunctionName (fn) { | ||
return fn.name || fn.toString().match(/function (.*?)\s*\(/)[1] | ||
} | ||
function TfTypeError (type, value, valueTypeName) { | ||
this.__error = Error.call(this) | ||
this.__type = type | ||
this.__value = value | ||
this.__valueTypeName = valueTypeName | ||
function getValueTypeName (value) { | ||
if (native.Null(value)) return '' | ||
return getFunctionName(value.constructor) | ||
} | ||
function getValue (value) { | ||
if (native.Function(value)) return '' | ||
if (native.String(value)) return JSON.stringify(value) | ||
if (value && native.Object(value)) return '' | ||
return value | ||
} | ||
function tfJSON (type) { | ||
if (native.Function(type)) return type.toJSON ? type.toJSON() : getFunctionName(type) | ||
if (native.Array(type)) return 'Array' | ||
if (type && native.Object(type)) return 'Object' | ||
return type !== undefined ? type : '' | ||
} | ||
function stfJSON (type) { | ||
type = tfJSON(type) | ||
return native.Object(type) ? JSON.stringify(type) : type | ||
} | ||
function TfTypeError (type, value) { | ||
this.tfError = Error.call(this) | ||
this.tfType = type | ||
this.tfValue = value | ||
var message | ||
@@ -46,4 +15,6 @@ Object.defineProperty(this, 'message', { | ||
if (message) return message | ||
message = tfErrorString(type, value) | ||
valueTypeName = valueTypeName || getValueTypeName(value) | ||
message = tfErrorString(type, value, valueTypeName) | ||
return message | ||
@@ -54,12 +25,10 @@ } | ||
inherits(TfTypeError, Error) | ||
Object.defineProperty(TfTypeError, 'stack', { get: function () { return this.tfError.stack } }) | ||
function TfPropertyTypeError (type, property, label, value, error, valueTypeName) { | ||
this.__error = error || Error.call(this) | ||
this.__label = label | ||
this.__property = property | ||
this.__type = type | ||
this.__value = value | ||
this.__valueTypeName = valueTypeName | ||
function TfPropertyTypeError (type, property, side, value, error) { | ||
this.tfError = error || Error.call(this) | ||
this.tfProperty = property | ||
this.tfSide = side | ||
this.tfType = type | ||
this.tfValue = value | ||
var message | ||
@@ -71,3 +40,4 @@ Object.defineProperty(this, 'message', { | ||
if (type) { | ||
message = tfPropertyErrorString(type, side, property, value) | ||
valueTypeName = valueTypeName || getValueTypeName(value) | ||
message = tfPropertyErrorString(type, label, property, value, valueTypeName) | ||
} else { | ||
@@ -82,44 +52,84 @@ message = 'Unexpected property "' + property + '"' | ||
inherits(TfPropertyTypeError, Error) | ||
Object.defineProperty(TfPropertyTypeError, 'stack', { | ||
get: function () { return this.tfError.stack } | ||
// inherit from Error, assign stack | ||
[TfTypeError, TfPropertyTypeError].forEach(function (tfErrorType) { | ||
inherits(tfErrorType, Error) | ||
Object.defineProperty(tfErrorType, 'stack', { | ||
get: function () { return this.__error.stack } | ||
}) | ||
}) | ||
TfPropertyTypeError.prototype.asChildOf = function (property) { | ||
return new TfPropertyTypeError(this.tfType, property + '.' + this.tfProperty, this.tfSide, this.tfValue, this.tfError) | ||
function tfCustomError (expected, actual) { | ||
return new TfTypeError(expected, '', actual) | ||
} | ||
function tfErrorString (type, value) { | ||
var valueTypeName = getValueTypeName(value) | ||
var valueValue = getValue(value) | ||
function tfSubError (e, property, label) { | ||
// sub child? | ||
if (e instanceof TfPropertyTypeError) { | ||
property = property + '.' + e.__property | ||
label = e.__label | ||
return 'Expected ' + stfJSON(type) + ', got' + | ||
(valueTypeName !== '' ? ' ' + valueTypeName : '') + | ||
(valueValue !== '' ? ' ' + valueValue : '') | ||
return new TfPropertyTypeError( | ||
e.__type, property, label, e.__value, e.__error, e.__valueTypeName | ||
) | ||
} | ||
// child? | ||
if (e instanceof TfTypeError) { | ||
return new TfPropertyTypeError( | ||
e.__type, property, label, e.__value, e.__error, e.__valueTypeName | ||
) | ||
} | ||
return e | ||
} | ||
function tfPropertyErrorString (type, side, name, value) { | ||
var description = '" of type ' | ||
if (side === 'key') description = '" with key type ' | ||
function getFunctionName (fn) { | ||
return fn.name || fn.toString().match(/function (.*?)\s*\(/)[1] | ||
} | ||
return tfErrorString('property "' + stfJSON(name) + description + stfJSON(type), value) | ||
function getValueTypeName (value) { | ||
if (native.Null(value)) return '' | ||
return getFunctionName(value.constructor) | ||
} | ||
function tfSubError (e, propertyName, sideLabel) { | ||
if (e instanceof TfPropertyTypeError) return e.asChildOf(propertyName) | ||
if (e instanceof TfTypeError) { | ||
return new TfPropertyTypeError(e.tfType, propertyName, sideLabel, e.tfValue, e.tfError) | ||
} | ||
function getValue (value) { | ||
if (native.Function(value)) return '' | ||
if (native.String(value)) return JSON.stringify(value) | ||
if (value && native.Object(value)) return '' | ||
return e | ||
return value | ||
} | ||
function tfJSON (type) { | ||
if (native.Function(type)) return type.toJSON ? type.toJSON() : getFunctionName(type) | ||
if (native.Array(type)) return 'Array' | ||
if (type && native.Object(type)) return 'Object' | ||
return type !== undefined ? type : '' | ||
} | ||
function tfErrorString (type, value, valueTypeName) { | ||
var valueJson = getValue(value) | ||
return 'Expected ' + tfJSON(type) + ', got' + | ||
(valueTypeName !== '' ? ' ' + valueTypeName : '') + | ||
(valueJson !== '' ? ' ' + valueJson : '') | ||
} | ||
function tfPropertyErrorString (type, label, name, value, valueTypeName) { | ||
var description = '" of type ' | ||
if (label === 'key') description = '" with key type ' | ||
return tfErrorString('property "' + tfJSON(name) + description + tfJSON(type), value, valueTypeName) | ||
} | ||
module.exports = { | ||
TfTypeError: TfTypeError, | ||
TfPropertyTypeError: TfPropertyTypeError, | ||
tfCustomError: tfCustomError, | ||
tfSubError: tfSubError, | ||
tfJSON: tfJSON, | ||
stfJSON: stfJSON, | ||
getFunctionName: getFunctionName, | ||
getValueTypeName: getValueTypeName | ||
} |
@@ -12,3 +12,3 @@ var errors = require('./errors') | ||
if (value.length !== length) { | ||
throw new errors.TfTypeError('Buffer(Length: ' + length + ')', 'Buffer(Length: ' + value.length + ')') | ||
throw errors.tfCustomError('Buffer(Length: ' + length + ')', 'Buffer(Length: ' + value.length + ')') | ||
} | ||
@@ -15,0 +15,0 @@ |
13
index.js
@@ -6,3 +6,2 @@ var errors = require('./errors') | ||
var tfJSON = errors.tfJSON | ||
var stfJSON = errors.stfJSON | ||
var TfTypeError = errors.TfTypeError | ||
@@ -28,3 +27,3 @@ var TfPropertyTypeError = errors.TfPropertyTypeError | ||
} | ||
_arrayOf.toJSON = function () { return [tfJSON(type)] } | ||
_arrayOf.toJSON = function () { return '[' + tfJSON(type) + ']' } | ||
@@ -40,3 +39,3 @@ return _arrayOf | ||
} | ||
_maybe.toJSON = function () { return '?' + stfJSON(type) } | ||
_maybe.toJSON = function () { return '?' + tfJSON(type) } | ||
@@ -76,6 +75,6 @@ return _maybe | ||
_map.toJSON = function () { | ||
return '{' + stfJSON(propertyKeyType) + ': ' + stfJSON(propertyType) + '}' | ||
return '{' + tfJSON(propertyKeyType) + ': ' + tfJSON(propertyType) + '}' | ||
} | ||
} else { | ||
_map.toJSON = function () { return '{' + stfJSON(propertyType) + '}' } | ||
_map.toJSON = function () { return '{' + tfJSON(propertyType) + '}' } | ||
} | ||
@@ -133,3 +132,3 @@ | ||
} | ||
_oneOf.toJSON = function () { return types.map(stfJSON).join('|') } | ||
_oneOf.toJSON = function () { return types.map(tfJSON).join('|') } | ||
@@ -160,3 +159,3 @@ return _oneOf | ||
} | ||
_tuple.toJSON = function () { return '(' + types.map(stfJSON).join(', ') + ')' } | ||
_tuple.toJSON = function () { return '(' + types.map(tfJSON).join(', ') + ')' } | ||
@@ -163,0 +162,0 @@ return _tuple |
{ | ||
"name": "typeforce", | ||
"version": "1.8.3", | ||
"version": "1.8.4", | ||
"description": "Another biased type checking solution for Javascript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -22,2 +22,3 @@ var typeforce = require('../') | ||
'{ a: undefined }': { a: undefined }, | ||
'@{ a: undefined }': typeforce.object({ a: undefined }), // DEPRECATED | ||
'Unmatchable': Unmatchable, | ||
@@ -24,0 +25,0 @@ '?Unmatchable': typeforce.maybe(Unmatchable), |
Sorry, the diff of this file is too big to display
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
424083
19519