MongoDB に書き込む前にすべてのデータをサニタイズする事前ハンドラーを作成しようとしています: http://mongoosejs.com/docs/middleware.html
各プロパティをサニタイズできるようにするために、次のことを試しました。
blogSchema.pre('save', function (next) {
var obj = this;
console.log(obj)//-> https://gist.github.com/daslicht/70e0501acd6c345df8c2
// I've tried the following to get the single items :
Object.keys(obj).forEach(function (key) {
console.log('Keys: ',obj[key]);
});
//and:
for(var key in obj) {
console.log(obj[key])
}
//and:
_.each( self , function(value, key, list){
console.log('VALUE:',key);
})
next();
})
上記のアプローチのいずれも、次のような結果になります。
それは次の出力です:
for(var key in obj) {
console.log(obj[key])
}
https://gist.github.com/daslicht/cb855f53d86062570a96
サニタイズできるように、各プロパティを取得する方法を知っている人はいますか?
〜マーク
[編集] 考えられる回避策の 1 つを次に示します。いずれにせよ、これはより DRY になるため、Scheme レベルで直接使用する方がクリーンです。
var post = {
createdAt : req.body.date,
createdBy : req.user.username,
headline : req.body.headline,
content : req.body.content
}
_.each( post , function(value, key, list){
post[key] = sanitize(value).xss(); //its the sanetize function of node validator
})
var item = new Blog(post);