Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-invalid event not working with from stream #198

Closed
keatz55 opened this issue Sep 13, 2017 · 1 comment
Closed

data-invalid event not working with from stream #198

keatz55 opened this issue Sep 13, 2017 · 1 comment

Comments

@keatz55
Copy link

keatz55 commented Sep 13, 2017

I have the following snippet almost taken verbatim from the parse tests w/ in lib:
https://github.com/C2FO/fast-csv/blob/5cf10be9bc9dbc87abfce04ccf2a03dd5d1f8a9e/test/parsing.test.js

csv
    .fromStream(request(job.data.file), { headers: true })
    .validate(function (data, next) {  
      next(new Error("Validation ERROR!!!!"));
    })
    .on("data", function (data, index) {
      console.log('DATA: ', data, index);
    })
    .on("data-invalid", function (data, index) {
      console.log('DATA_INVALID: ', data, index);
    })
    .on("error", function (err) {
      console.log('ERROR: ', err);
    })
    .on("end", function (count) {
      console.log('END: ', count);
    });

Whenever I run this code, "data-invalid" is completely bypassed and only "error" is hit.

I also looked at the code and saw that a next(null, false) can trigger the "data-invalid" emit event. However, every index is listed as 0.

@doug-martin
Copy link
Contributor

In your example that is expected behavior, the next callback should mainly be used for async validation and an error should only be passed to the callback if an error occurs that should stop the stream processing.

Here is a slightly modified example that shows each data and data-invalid event being hit, with a final error on the last row.

const CSV_CONTENT = [
    'id,name,location',
    '1,Joe,Boston',
    '2,Emily,San Francisco',
    ',Harold,Dallas',
    ',Kate,Seattle',
    '5,William,New York',
].join('\n');

let count = 0;
csv
    .fromString(CSV_CONTENT, { headers: true })
    .validate((data, next) => {
        count += 1;
        if (count === 5) {
            return next(new Error('Validation ERROR!!!!'));
        }
        return next(null, count % 2 === 0);
    })
    .on('data', data => console.log('DATA: ', data))
    .on('data-invalid', (data, index) => console.log('DATA_INVALID: ', data, index))
    .on('error', err => console.log('ERROR: ', err))
    .on('end', rowCount => console.log('END: ', rowCount));

Output:

DATA_INVALID:  { id: '1', name: 'Joe', location: 'Boston' } 1
DATA:  { id: '2', name: 'Emily', location: 'San Francisco' }
DATA_INVALID:  { id: '', name: 'Harold', location: 'Dallas' } 2
DATA:  { id: '', name: 'Kate', location: 'Seattle' }
ERROR:  Error: Validation ERROR!!!!

In that example there is an error where the row count is not properly incremented and is covered under #130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants