From 7711b729d59e9f7de24da08ad39612d8f613a17f Mon Sep 17 00:00:00 2001 From: Markku Virtanen Date: Wed, 31 May 2017 14:46:08 +0300 Subject: [PATCH 1/2] populate with raw document instead of validation value --- lib/mongoHandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoHandler.js b/lib/mongoHandler.js index bbc58f4..6e1f05b 100644 --- a/lib/mongoHandler.js +++ b/lib/mongoHandler.js @@ -217,7 +217,7 @@ MongoStore.prototype.populate = function(callback) { if (validationResult.error) { return cb(validationResult.error); } - self.create({ params: {} }, validationResult.value, cb); + self.create({ params: {} }, document, cb); }, function(error) { if (error) console.error("error creating example document:", error); return callback(); From c81e4290065d6bcf557445961c6d53dac856506e Mon Sep 17 00:00:00 2001 From: Markku Virtanen Date: Thu, 1 Jun 2017 11:01:23 +0300 Subject: [PATCH 2/2] Remove strip from validation attributes to populate correctly --- lib/mongoHandler.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/mongoHandler.js b/lib/mongoHandler.js index 6e1f05b..465dfe9 100644 --- a/lib/mongoHandler.js +++ b/lib/mongoHandler.js @@ -212,12 +212,19 @@ MongoStore.prototype.populate = function(callback) { self._db.dropDatabase(function(err) { if (err) return console.error("error dropping database", err.message); + + var attrsWithoutStrip = Object.assign({}, self.resourceConfig.attributes); + + // Remove strip flag from attribute validation to populate properly + Object.keys(attrsWithoutStrip).forEach(function(attrkey) { + delete attrsWithoutStrip[attrkey]._flags.strip; + }); async.each(self.resourceConfig.examples, function(document, cb) { - var validationResult = Joi.validate(document, self.resourceConfig.attributes); + var validationResult = Joi.validate(document, attrsWithoutStrip); if (validationResult.error) { return cb(validationResult.error); } - self.create({ params: {} }, document, cb); + self.create({ params: {} }, validationResult.value, cb); }, function(error) { if (error) console.error("error creating example document:", error); return callback();