0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-18 02:21:47 -05:00

Added empty string '' to null transform when parsing CSVs

no issue

- When items are parsed from CSV empty values were interpreted as empty strings - ''. Empty strings are always transformed into 'null' values in Ghost's model layer and are much more problematic to validate comparing to plain `null`. Specifically validation was imossible for 'format: date-time' with JSON schema validation through ajv when the value of date property was an empty string
- This behavior resemples one present in Ghost's model layer  - 95880dddeb
- When testing performance overhead for this change did not spot any statistically significant change in performance (tested set was 50K rows)
This commit is contained in:
Nazar Gargol 2020-08-17 17:57:49 +12:00
parent 117309b4e8
commit b8c1aeee35
3 changed files with 25 additions and 1 deletions

View file

@ -64,7 +64,14 @@ const readCSV = ({path, columnsToExtract, mapping}) => {
reject(err);
})
.pipe(papaparse.parse(papaparse.NODE_STREAM_INPUT, {
header: true
header: true,
transform: function (value) {
if (value === '') {
return null;
}
return value;
}
}))
.on('data', function (row) {
rows.push(row);

View file

@ -0,0 +1,3 @@
email,name
"jbloggs@example.com","Bob"
test@example.com,
1 email name
2 jbloggs@example.com Bob
3 test@example.com

View file

@ -112,4 +112,18 @@ describe('parse', function () {
result[1].nombre.should.eql('test');
result[1].id.should.eql('2');
});
it('read csv: transforms empty values to nulls', async function () {
const result = await readCSV({
path: csvPath + 'multiple-records-with-empty-values.csv'
});
should.exist(result);
result.length.should.eql(2);
result[0].email.should.eql('jbloggs@example.com');
result[0].name.should.eql('Bob');
result[1].email.should.eql('test@example.com');
should.equal(result[1].name, null);
});
});