0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

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.
This commit is contained in:
kirrg001 2017-12-14 17:04:06 +01:00
parent 634fdbfa96
commit 740b247a80
2 changed files with 10 additions and 7 deletions

View file

@ -445,11 +445,11 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
* - but Bookshelf is not in our control for this case * - but Bookshelf is not in our control for this case
* *
* @IMPORTANT * @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`. * proper strings, see `format`.
*/ */
sanitizeData: function sanitizeData(data) { sanitizeData: function sanitizeData(data) {
var tableName = _.result(this.prototype, 'tableName'), dateMoment; var tableName = _.result(this.prototype, 'tableName'), date;
_.each(data, function (value, key) { _.each(data, function (value, key) {
if (value !== null if (value !== null
@ -457,16 +457,17 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
&& schema.tables[tableName][key].type === 'dateTime' && schema.tables[tableName][key].type === 'dateTime'
&& typeof value === 'string' && typeof value === 'string'
) { ) {
dateMoment = moment(value); date = new Date(value);
// CASE: client sends `0000-00-00 00:00:00` // CASE: client sends `0000-00-00 00:00:00`
if (!dateMoment.isValid()) { if (isNaN(date)) {
throw new common.errors.ValidationError({ 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();
} }
}); });

View file

@ -589,13 +589,15 @@ describe('Post Model', function () {
post = results.toJSON(); post = results.toJSON();
post.status.should.equal('draft'); post.status.should.equal('draft');
// @TODO: add unit test for valid and invalid formats
return PostModel.edit({ return PostModel.edit({
status: 'scheduled', status: 'scheduled',
published_at: '328432423' published_at: '0000-00-00 00:00:00'
}, _.extend({}, context, {id: post.id})); }, _.extend({}, context, {id: post.id}));
}).catch(function (err) { }).catch(function (err) {
should.exist(err); should.exist(err);
(err instanceof common.errors.ValidationError).should.eql(true); (err instanceof common.errors.ValidationError).should.eql(true);
err.code.should.eql('DATE_INVALID');
done(); done();
}); });
}); });