Comparing version 0.3.1 to 0.5.0
@@ -30,2 +30,5 @@ /** | ||
// Strip out "E_COERCION" errors- they are ok if we're just coercing. | ||
_.remove(errors, {code: 'E_COERCION'}); | ||
if (errors.length) { | ||
@@ -32,0 +35,0 @@ |
@@ -10,2 +10,3 @@ /** | ||
/** | ||
@@ -45,8 +46,12 @@ * Basic type definitions. | ||
if(_.isBoolean(v)) return v; | ||
if(v === 'true') return true; | ||
if(v === 'false') return false; | ||
if(v === 1) return true; | ||
if(v === 0) return false; | ||
if(v === '1') return true; | ||
if(v === '0') return false; | ||
if (_.isNumber(v)){ | ||
if(v === 1) return true; | ||
if(v === 0) return false; | ||
} | ||
if (_.isString(v)) { | ||
if(v === 'true') return true; | ||
if(v === 'false') return false; | ||
if(v === '1') return true; | ||
if(v === '0') return false; | ||
} | ||
@@ -196,7 +201,14 @@ throw new Error('E_runtimeInputTypeCoercionError'); | ||
// Check for Infinity | ||
// Check for Infinity and NaN | ||
if(v === Infinity || v === -Infinity) { | ||
throw new Error('E_runtimeInputTypeCoercionError'); | ||
} | ||
if (_.isNaN(v)) { | ||
throw new Error('E_runtimeInputTypeCoercionError'); | ||
} | ||
if (_.isNull(v)){ | ||
throw new Error('E_runtimeInputTypeCoercionError'); | ||
} | ||
if(type.number.is(v)) return v; | ||
@@ -206,16 +218,36 @@ if(type.bool.is(v)) return v ? 1 : 0; | ||
// Check for Infinity | ||
if(v === 'Infinity' || v === '-Infinity') { | ||
throw new Error('E_runtimeInputTypeCoercionError'); | ||
} | ||
// Is this a string that appears to be a number? | ||
var isStringThatAppearsToBeNumber = (function _getIsStringThatAppearsToBeNumber(value){ | ||
if (!_.isString(value)) return false; | ||
var num = v * 1; | ||
if(!_.isNumber(num)) { | ||
// (this is an exception... apparently doing `+''` in javascript results in `0`) | ||
if (value === '') return false; | ||
// General case: | ||
if ( _.isNaN(+value) ) return false; | ||
if ( +value=== Infinity ) return false; | ||
if ( +value=== -Infinity ) return false; | ||
return true; | ||
})(v); | ||
if (!isStringThatAppearsToBeNumber) { | ||
throw new Error('E_runtimeInputTypeCoercionError'); | ||
} | ||
return (num === 0 && !v.match(/^0+$/)) ? 0 : num; | ||
return +v; | ||
// // Check for Infinity | ||
// if(v === 'Infinity' || v === '-Infinity') { | ||
// throw new Error('E_runtimeInputTypeCoercionError'); | ||
// } | ||
// var num = v * 1; | ||
// if(!_.isNumber(num)) { | ||
// throw new Error('E_runtimeInputTypeCoercionError'); | ||
// } | ||
// return (num === 0 && !v.match(/^0+$/)) ? 0 : num; | ||
} | ||
throw new Error('E_runtimeInputTypeCoercionError'); | ||
}, | ||
@@ -239,1 +271,4 @@ base: 0, | ||
module.exports = type; | ||
@@ -18,6 +18,16 @@ /** | ||
// * (allow anything) | ||
// "*" (allow anything) | ||
if (expected === '*'){ | ||
return _.isUndefined(actual) ? '' : actual; | ||
} | ||
// ["*"] (allow any array) | ||
if (_.isArray(expected) && expected[0] === '*') { | ||
return _.isUndefined(actual) ? [] : actual; | ||
} | ||
// {} (allow any dictionary) | ||
if (_.isPlainObject(expected) && _.keys(expected).length === 0) { | ||
return _.isUndefined(actual) ? {} : actual; | ||
} | ||
// Arrays | ||
@@ -63,2 +73,19 @@ if (_.isArray(expected)) { | ||
// Push an E_INVALID_TYPE error | ||
errors.push((function (){ | ||
var err = new Error(util.format( | ||
'An invalid value was specified: \n' + util.inspect(actual, false, null) + '\n\n' + | ||
'This doesn\'t match the specified type: \n' + util.inspect(expected, false, null) | ||
)); | ||
err.code = 'E_INVALID_TYPE'; | ||
// if not expecting a dictionary or an array, and actual value is not | ||
// a dictionary or array, then this is just a "minor" thing. | ||
// No big deal, you know. | ||
if (!isExpectingDictionary && !isExpectingArray && !_.isObject(actual)) { | ||
err.minor = true; | ||
} | ||
return err; | ||
})()); | ||
// Invalid expected type. Try to coerce: | ||
@@ -76,5 +103,5 @@ try { | ||
'An invalid value was specified: \n' + util.inspect(actual, false, null) + '\n\n' + | ||
'This doesn\'t match the specified type: \n' + util.inspect(expected, false, null) | ||
'This cannot be coerced into the specified type: \n' + util.inspect(expected, false, null) | ||
)); | ||
err.code = 'E_INVALID_TYPE'; | ||
err.code = 'E_COERCION'; | ||
return err; | ||
@@ -93,2 +120,3 @@ })()); | ||
return _.reduce(expected, function (memo, expectedVal, expectedKey) { | ||
console.log('recursively diving into dictionary at key %s...', expectedKey); | ||
memo[expectedKey] = _validateRecursive(expected[expectedKey], coercedValue[expectedKey], errors); | ||
@@ -95,0 +123,0 @@ return memo; |
@@ -32,2 +32,3 @@ /** | ||
err.code = errors[0].code; | ||
err.minor = errors[0].minor; | ||
err.errors = errors; | ||
@@ -34,0 +35,0 @@ return err; |
{ | ||
"name": "rttc", | ||
"version": "0.3.1", | ||
"version": "0.5.0", | ||
"description": "Runtime type-checking for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
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
32356
13
797