Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly initialize and return Error objects #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/match/errorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ module.exports = function errorFactory(value, ruleName, keyName, customMessage)


// Construct error object
return [{
property: keyName,
data: value,
message: errMsg,
rule: ruleName,
actualType: typeof value,
expectedType: ruleName
}];
var err = new Error(errMsg);
err.property = keyName;
err.data = value;
err.rule = ruleName;
err.actualType = typeof value;
err.expectedType = ruleName;
return [err];
};
9 changes: 4 additions & 5 deletions lib/match/matchRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ module.exports = function matchRule (data, ruleName, args) {

// If outcome is false, an error occurred
if (!outcome) {
return [{
rule: ruleName,
data: data,
message: util.format('"%s" validation rule failed for input: %s', ruleName, util.inspect(data))
}];
var err = new Error(util.format('"%s" validation rule failed for input: %s', ruleName, util.inspect(data)));
err.data = data;
err.rule = ruleName;
return [err];
}
else {
return [];
Expand Down
22 changes: 10 additions & 12 deletions lib/match/matchType.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function deepMatchType(data, ruleset, depth, keyName, customMessage) {
depth = depth || 0;
if (depth > MAX_DEPTH) {
return [
new Error({ message: 'Exceeded MAX_DEPTH when validating object. Maybe it\'s recursively referencing itself?'})
new Error('Exceeded MAX_DEPTH when validating object. Maybe it\'s recursively referencing itself?')
];
}

Expand All @@ -63,7 +63,7 @@ function deepMatchType(data, ruleset, depth, keyName, customMessage) {
if (ruleset.length !== 0) {
if (ruleset.length > 1) {
return [
new Error({ message: '[] (or schema) rules must contain exactly one item.'})
new Error('[] (or schema) rules must contain exactly one item.')
];
}

Expand Down Expand Up @@ -103,13 +103,12 @@ function deepMatchType(data, ruleset, depth, keyName, customMessage) {
// Don't allow a `$message` without a `$validate`
if (_customMessage) {
if (!subValidation) {
return [{
code: 'E_USAGE',
status: 500,
$message: _customMessage,
property: keyName,
message: 'Custom messages ($message) require a subvalidation - please specify a `$validate` option on `'+keyName+'`'
}];
var err = new Error('Custom messages ($message) require a subvalidation - please specify a `$validate` option on `'+keyName+'`');
err.status = 500;
err.code = 'E_USAGE';
err.$message = _customMessage;
err.property = keyName;
return [err];
}
else {
// Use the specified message as the `customMessage`
Expand All @@ -121,7 +120,7 @@ function deepMatchType(data, ruleset, depth, keyName, customMessage) {
if (subValidation) {
if (!subValidation.type) {
return [
new Error({message: 'Sub-validation rules (i.e. using $validate) other than `type` are not currently supported'})
new Error('Sub-validation rules (i.e. using $validate) other than `type` are not currently supported')
];
}

Expand Down Expand Up @@ -204,7 +203,7 @@ function matchType(datum, ruleName, keyName, customMessage) {
// Determine outcome
if (!rule) {
return [
new Error({message:'Unknown rule: ' + ruleName})
new Error('Unknown rule: ' + ruleName)
];
}
else outcome = rule.call(self, datum);
Expand All @@ -222,4 +221,3 @@ function matchType(datum, ruleName, keyName, customMessage) {
}

}

6 changes: 6 additions & 0 deletions test/util/testType.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports = function testType (rule, example, nonexample) {
if (!_.isArray(nonexampleOutcome)) {
return gotErrors('Invalid input (' + nonexample + ') allowed through as a ' + rule + '.');
}
for (var i = 0; i < nonexampleOutcome.length; i++) {
var err = nonexampleOutcome[i];
if (!err instanceof Error) {
return gotErrors('Input ' + err + " should have been instanceof Error, wasn't");
}
}

function gotErrors (err) {
console.error('*****************');
Expand Down