Add validation of onError callback object for rules.

This commit is contained in:
David Anson 2018-02-27 21:14:02 -08:00
parent 802c81f929
commit 7a752784f1
7 changed files with 107 additions and 12 deletions

View file

@ -324,8 +324,36 @@ function lintContent(
var ruleNameFriendly = rule.names[0];
var ruleName = ruleNameFriendly.toUpperCase();
params.config = effectiveConfig[ruleName];
function throwError(property) {
throw new Error(
"Property '" + property + "' of onError parameter is incorrect.");
}
var errors = [];
function onError(errorInfo) {
if (!errorInfo ||
!errorInfo.lineNumber ||
!shared.isNumber(errorInfo.lineNumber)) {
throwError("lineNumber");
}
if ((errorInfo.detail !== null) &&
(errorInfo.detail !== undefined) &&
(!shared.isString(errorInfo.detail) ||
shared.isEmptyString(errorInfo.detail))) {
throwError("detail");
}
if ((errorInfo.context !== null) &&
(errorInfo.context !== undefined) &&
(!shared.isString(errorInfo.context) ||
shared.isEmptyString(errorInfo.context))) {
throwError("context");
}
if (errorInfo.range &&
(!Array.isArray(errorInfo.range) ||
(errorInfo.range.length !== 2) ||
!shared.isNumber(errorInfo.range[0]) ||
!shared.isNumber(errorInfo.range[1]))) {
throwError("range");
}
errors.push({
"lineNumber": errorInfo.lineNumber + frontMatterLines.length,
"detail": errorInfo.detail || null,