diff --git a/lib/csv.js b/lib/csv.js index 3ec762e..61bee12 100644 --- a/lib/csv.js +++ b/lib/csv.js @@ -97,6 +97,8 @@ export async function previewCsvFromStream(inputStream, options = {}) { ...parserOptions, preview: options.previewCount || 10, complete({meta, data, errors}) { + errors = filterErrors(errors) + const response = { format: 'csv', formatOptions: { @@ -156,6 +158,8 @@ export function validateCsvFromStream(inputStream, options = {}) { chunkSize: 1024 * 1024, chunk({data, errors}, parser) { + errors = filterErrors(errors) + if (errors.length > 0) { emitter.emit('error', new Error('Error in CSV file: ' + errors[0].code)) emitter.removeAllListeners() @@ -210,3 +214,9 @@ export function createCsvReadStream(options = {}) { Papa.parse(Papa.NODE_STREAM_INPUT, parserOptions) ) } + +/* Helpers */ + +function filterErrors(errors) { + return errors.filter(error => error.code !== 'UndetectableDelimiter') +} diff --git a/test/csv.js b/test/csv.js index 67fd3a1..09e768f 100644 --- a/test/csv.js +++ b/test/csv.js @@ -59,15 +59,15 @@ test('parsing invalid CSV', async t => { }) }) -test('validate CSV file / single column / no delimiter defined', t => { +test('validate CSV file / single column', t => { const path = new URL('fixtures/single-column.csv', import.meta.url) const inputStream = createReadStream(path) return new Promise(resolve => { const emitter = validateCsvFromStream(inputStream) - emitter.once('error', error => { - t.is(error.message, 'Error in CSV file: UndetectableDelimiter') + emitter.once('complete', result => { + t.true(result.isValid) resolve() }) })