From eabf7e154c09ec897b47d4ee51c9dad642835250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20J=C3=A4rvel=C3=B6v?= Date: Sun, 20 Nov 2016 11:33:59 +0100 Subject: [PATCH] Fix bug with forbidUndefinedVariables (#62) The forbidUndefinedVariables check wrongly flagged keys that existed in other scopes as undefined, this fix checks for the key in the other scopes of the validationModel before flagging it as undefined. --- lib/validation.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/validation.js b/lib/validation.js index e1e1b96..175fb49 100644 --- a/lib/validation.js +++ b/lib/validation.js @@ -176,11 +176,23 @@ module.exports.process = function (validationModel, req, options) { keys.splice(index, 1); } }); - if (options.forbidUndefinedVariables === true) { - keys.forEach(function(key) { - addError(_createError(realScope, key, { name: "undefinedVariable" })); - }); - } + if (options.forbidUndefinedVariables === true) { + keys.forEach(function(key) { + var invalid = true; + + // check if key exist in some other scope in the validationModel + _.each(validationModel, function (validationModelScope) { + if (key in validationModelScope) { + invalid = false; + } + }); + + // flag key as undefined if it didn't show up in another scope + if (invalid) { + addError(_createError(realScope, key, { name: "undefinedVariable" })); + } + }); + } } }); return errors;