From 740b247a80e04a9f287dda39553e862521c630c0 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Thu, 14 Dec 2017 17:04:06 +0100 Subject: [PATCH] Avoid moment deprecation warning when validating incoming dates no issue > Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. --- core/server/models/base/index.js | 13 +++++++------ core/test/integration/model/model_posts_spec.js | 4 +++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/core/server/models/base/index.js b/core/server/models/base/index.js index 590ca926a1..4c34db16c6 100644 --- a/core/server/models/base/index.js +++ b/core/server/models/base/index.js @@ -445,11 +445,11 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({ * - but Bookshelf is not in our control for this case * * @IMPORTANT - * Before the new client data get's inserted again, the dates get's retransformed into + * Before the new client data get's inserted again, the dates get's re-transformed into * proper strings, see `format`. */ sanitizeData: function sanitizeData(data) { - var tableName = _.result(this.prototype, 'tableName'), dateMoment; + var tableName = _.result(this.prototype, 'tableName'), date; _.each(data, function (value, key) { if (value !== null @@ -457,16 +457,17 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({ && schema.tables[tableName][key].type === 'dateTime' && typeof value === 'string' ) { - dateMoment = moment(value); + date = new Date(value); // CASE: client sends `0000-00-00 00:00:00` - if (!dateMoment.isValid()) { + if (isNaN(date)) { throw new common.errors.ValidationError({ - message: common.i18n.t('errors.models.base.invalidDate', {key: key}) + message: common.i18n.t('errors.models.base.invalidDate', {key: key}), + code: 'DATE_INVALID' }); } - data[key] = dateMoment.toDate(); + data[key] = moment(value).toDate(); } }); diff --git a/core/test/integration/model/model_posts_spec.js b/core/test/integration/model/model_posts_spec.js index d55e021454..fd8480b00b 100644 --- a/core/test/integration/model/model_posts_spec.js +++ b/core/test/integration/model/model_posts_spec.js @@ -589,13 +589,15 @@ describe('Post Model', function () { post = results.toJSON(); post.status.should.equal('draft'); + // @TODO: add unit test for valid and invalid formats return PostModel.edit({ status: 'scheduled', - published_at: '328432423' + published_at: '0000-00-00 00:00:00' }, _.extend({}, context, {id: post.id})); }).catch(function (err) { should.exist(err); (err instanceof common.errors.ValidationError).should.eql(true); + err.code.should.eql('DATE_INVALID'); done(); }); });