🐛 import invalid dates (#8712)

closes #8703, closes #8015

- add sanitize fn to importer
- check wether an imported date is a valid date
- if not, print a warning
This commit is contained in:
Katharina Irrgang 2017-07-20 12:24:23 +02:00 committed by Kevin Ansfield
parent 90fc7a6c06
commit 59d7302da5
4 changed files with 50 additions and 6 deletions

View File

@ -54,8 +54,32 @@ class Base {
});
}
/**
* Clean invalid values.
*/
sanitizeValues() {
let temporaryDate, self = this;
_.each(this.dataToImport, function (obj) {
_.each(_.pick(obj, ['updated_at', 'created_at', 'published_at']), function (value, key) {
temporaryDate = new Date(value);
if (isNaN(temporaryDate)) {
self.problems.push({
message: 'Date is in a wrong format and invalid. It was replaced with the current timestamp.',
help: self.modelName,
context: JSON.stringify(obj)
});
obj[key] = new Date().toISOString();
}
});
});
}
beforeImport() {
this.stripProperties(['id']);
this.sanitizeValues();
return Promise.resolve();
}

View File

@ -353,7 +353,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
* `sanitizeData` ensures that client data is in the correct format for further operations.
*
* Dates:
* - client dates are sent as ISO format (moment(..).format())
* - client dates are sent as ISO 8601 format (moment(..).format())
* - server dates are in JS Date format
* >> when bookshelf fetches data from the database, all dates are in JS Dates
* >> see `parse`

View File

@ -60,7 +60,7 @@ describe('Import', function () {
}).then(function (importResult) {
should.exist(importResult.data.posts);
importResult.data.posts.length.should.equal(1);
importResult.problems.length.should.eql(2);
importResult.problems.length.should.eql(3);
done();
}).catch(done);
@ -93,6 +93,26 @@ describe('Import', function () {
done();
}).catch(done);
});
it('cares about invalid dates', function (done) {
var exportData;
testUtils.fixtures.loadExportFixture('export-003',{lts:true}).then(function (exported) {
exportData = exported;
return dataImporter.doImport(exportData);
}).then(function (importResult) {
should.exist(importResult.data.posts);
importResult.data.posts.length.should.equal(1);
importResult.problems.length.should.eql(3);
moment(importResult.data.posts[0].created_at).isValid().should.eql(true);
moment(importResult.data.posts[0].updated_at).format().should.eql('2013-10-18T23:58:44Z');
moment(importResult.data.posts[0].published_at).format().should.eql('2013-12-29T11:58:30Z');
moment(importResult.data.tags[0].updated_at).format().should.eql('2016-07-17T12:02:54Z');
done();
}).catch(done);
});
});
describe('DataImporter', function () {

View File

@ -20,9 +20,9 @@
"meta_title": null,
"meta_description": null,
"author_id": 1,
"created_at": 1388318310782,
"created_at": "00-00-0000 00:00:00",
"created_by": 1,
"updated_at": 1388318310782,
"updated_at": "Fri, 18 Oct 2013 23:58:44 +0000",
"updated_by": 1,
"published_at": 1388318310783,
"published_by": 1
@ -47,7 +47,7 @@
"meta_title": null,
"meta_description": null,
"last_login": null,
"created_at": 1388319501897,
"created_at": "2017-07-17T12:02:54.000Z",
"created_by": 1,
"updated_at": null,
"updated_by": null
@ -305,7 +305,7 @@
"meta_description": null,
"created_at": 1388318310790,
"created_by": 1,
"updated_at": 1388318310790,
"updated_at": "2016-07-17T12:02:54.000Z",
"updated_by": 1
}
],