再帰関数を呼び出していますが、再帰呼び出しから受け取ったエラーを連結して呼び出し元に戻したいと思います。以下は私が使用するコードです。ただし、_errors変数はインスタンス間で共有されているようです。この_errors変数をインスタンスに固有にするにはどうすればよいですか。
var check = require('./validator.js').check;
var QV = function() {
this._errors = {};
}
QV.prototype.a = function (str) { check(str).len(1,4).notNull().isInt() };
QV.prototype.b = function (str) { check(str).len(1,4).notNull().isInt() };
QV.prototype.c = function (str) { check(str).len(1,4).notNull().isInt() };
QV.prototype.validator = function (opt) {
qv = new QV();
for(var i in opt) {
try {
if (opt[i].toString() === '[object Object]')
{
var errors = qv.validator(opt[i]);
console.log(qv._errors); //Here the qv._errors is overwritten with the 'sub' errors. I lose the error 'a' here.
qv._errors[i] = errors;
}
else
{
qv[i](opt[i]);
}
} catch (e) {
qv._errors[i] = e;
}
}
return qv._errors;
}
module.exports = QV;
そして、私はこのコードを使用して検証を行います
var test = require('./test_validator.js');
var q = new test();
msg = q.validator({
'a' : "asdf",
'sub' : {
'b' : "asdf",
'c' : "bsdf"
}
});
console.log(msg);