UNPKG

csv-validator

Version:
87 lines (65 loc) 2.23 kB
// 'use strict'; // /** // * TODO: // * csv = csv-validator // * csv(file, [validator]) // * // */ // /** // * // * enieemit ng iniemit ung on json // */ // const path = require('path'); // const csv = require('csvtojson'); // function a (file, h = {}, cb = () => {}) { // let arr = [], // errMsg = []; // return new Promise((resolve, reject) => { // csv() // .fromFile(file) // .on('json', (obj, idx) => { // const valid = validate(obj, h); // if (valid instanceof Error) { // errMsg.push(`Row ${idx+1}: ${valid.message}`); // } // arr.push(obj); // }) // .on('done', err => { // if (err || errMsg.length) { // cb(err || errMsg); // return reject(err || errMsg); // } // cb(null, arr); // resolve(arr); // }); // }); // } // function validate (obj, h = {}) { // for (let h_prop in h) { // if (h.hasOwnProperty(h_prop)) { // let obj_prop = h_prop; // if (h_prop[0] === '_') { // obj_prop = h_prop.slice(1); // } // if (h_prop[0] !== '_' && obj[obj_prop] === '') { // return new Error(`${h_prop} is required`); // } // if (h[h_prop] instanceof RegExp && !h[h_prop].test(obj[obj_prop])) { // return new Error(`${h_prop} must be in ${h[h_prop]}`); // } // if (typeof h[h_prop] === 'number' && !!isNaN(obj[obj_prop])) { // return new Error(`${h_prop} must be a type number`); // } // } // } // } const a = require(__dirname + '/lib'); const headers = { name: '', phone: 1, email: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, country: '' }; a(__dirname + '/test.csv', headers) .then(console.log) .catch(console.log);