Comparing version 3.2.8 to 3.2.9
@@ -15,2 +15,9 @@ # Changelog | ||
# 3.2.9 | ||
- **New Feature** | ||
- fromJSON: track error path, fix #235 (@gcanti) | ||
- **Internal** | ||
- change shallow copy in order to improve perfs (@gcanti) | ||
# 3.2.8 | ||
@@ -17,0 +24,0 @@ |
@@ -10,3 +10,3 @@ var assert = require('./assert'); | ||
function fromJSON(value, type) { | ||
function fromJSON(value, type, path) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -16,6 +16,7 @@ assert(isFunction(type), function () { | ||
}); | ||
path = path || [getTypeName(type)]; | ||
} | ||
if (isFunction(type.fromJSON)) { | ||
return create(type, type.fromJSON(value)); | ||
return create(type, type.fromJSON(value), path); | ||
} | ||
@@ -34,6 +35,6 @@ | ||
case 'maybe' : | ||
return isNil(value) ? null : fromJSON(value, type.meta.type); | ||
return isNil(value) ? null : fromJSON(value, type.meta.type, path); | ||
case 'subtype' : // the kind of a refinement is 'subtype' (for legacy reasons) | ||
ret = fromJSON(value, type.meta.type); | ||
ret = fromJSON(value, type.meta.type, path); | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -56,3 +57,3 @@ assert(type.meta.predicate(ret), function () { | ||
if (props.hasOwnProperty(k)) { | ||
ret[k] = fromJSON(value[k], props[k]); | ||
ret[k] = fromJSON(value[k], props[k], ( process.env.NODE_ENV !== 'production' ? path.concat(k + ': ' + getTypeName(props[k])) : null )); | ||
} | ||
@@ -72,3 +73,3 @@ } | ||
if (interProps.hasOwnProperty(k)) { | ||
ret[k] = fromJSON(value[k], interProps[k]); | ||
ret[k] = fromJSON(value[k], interProps[k], ( process.env.NODE_ENV !== 'production' ? path.concat(k + ': ' + getTypeName(interProps[k])) : null )); | ||
} | ||
@@ -85,4 +86,5 @@ } | ||
var elementType = type.meta.type; | ||
return value.map(function (element) { | ||
return fromJSON(element, elementType); | ||
var elementTypeName = getTypeName(elementType); | ||
return value.map(function (element, i) { | ||
return fromJSON(element, elementType, ( process.env.NODE_ENV !== 'production' ? path.concat(i + ': ' + elementTypeName) : null )); | ||
}); | ||
@@ -97,3 +99,3 @@ | ||
} | ||
return fromJSON(value, actualType); | ||
return fromJSON(value, actualType, path); | ||
@@ -113,3 +115,3 @@ case 'tuple' : | ||
return value.map(function (element, i) { | ||
return fromJSON(element, types[i]); | ||
return fromJSON(element, types[i], ( process.env.NODE_ENV !== 'production' ? path.concat(i + ': ' + getTypeName(types[i])) : null )); | ||
}); | ||
@@ -125,6 +127,8 @@ | ||
var codomain = type.meta.codomain; | ||
var domainName = getTypeName(domain); | ||
var codomainName = getTypeName(codomain); | ||
ret = {}; | ||
for (k in value) { | ||
if (value.hasOwnProperty(k)) { | ||
ret[domain(k)] = fromJSON(value[k], codomain); | ||
ret[domain(k, ( process.env.NODE_ENV !== 'production' ? path.concat(domainName) : null ))] = fromJSON(value[k], codomain, ( process.env.NODE_ENV !== 'production' ? path.concat(k + ': ' + codomainName) : null )); | ||
} | ||
@@ -135,3 +139,3 @@ } | ||
default : // enums, irreducible, intersection | ||
return type(value); | ||
return type(value, path); | ||
} | ||
@@ -138,0 +142,0 @@ } |
var isType = require('./isType'); | ||
var getFunctionName = require('./getFunctionName'); | ||
module.exports = function getTypeName(constructor) { | ||
if (isType(constructor)) { | ||
return constructor.displayName; | ||
module.exports = function getTypeName(ctor) { | ||
if (isType(ctor)) { | ||
return ctor.displayName; | ||
} | ||
return getFunctionName(constructor); | ||
return getFunctionName(ctor); | ||
}; |
@@ -6,14 +6,17 @@ var assert = require('./assert'); | ||
var isNumber = require('./isNumber'); | ||
var mixin = require('./mixin'); | ||
function getShallowCopy(x) { | ||
if (isObject(x)) { | ||
if (x instanceof Date || x instanceof RegExp) { | ||
return x; | ||
} | ||
var ret = {}; | ||
for (var k in x) { | ||
ret[k] = x[k]; | ||
} | ||
return ret; | ||
} | ||
if (isArray(x)) { | ||
return x.concat(); | ||
} | ||
if (x instanceof Date || x instanceof RegExp) { | ||
return x; | ||
} | ||
if (isObject(x)) { | ||
return mixin({}, x); | ||
} | ||
return x; | ||
@@ -20,0 +23,0 @@ } |
{ | ||
"name": "tcomb", | ||
"version": "3.2.8", | ||
"version": "3.2.9", | ||
"description": "Type checking and DDD for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
88973
1926
76