CSVFileValidator. Validation of CSV file against user defined schema (returns back object w…

综合技术 2018-03-07

CSV File Validator https://twitter.com/intent/tweet?hashtags=javascript&original_referer=https%3A%2F%2Fpublish.twitter.com%2F&ref_src=twsrc%5Etfw&text=Validation%20of%20CSV%20file%20against%20user%20defined%20schema%20(returns%20back%20object%20with%20data%20and%20invalid%20messages)&tw_p=tweetbutton&url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Fcsv-file-validator&via=shystrukk

Validation of CSV file against user defined schema (returns back object with data and invalid messages)

Getting csv-file-validator

npm

npm install --save csv-file-validator

yarn

yarn add csv-file-validator --save

Example

import CSVFileValidator from 'csv-file-validator'

CSVFileValidator(file, config)
    .then(csvData => {
        csvData.data // Array of objects from file
        csvData.inValidMessages // Array of error messages
    })
    .catch(err => {})

Please see Demo for more details /demo/index.html

API

CSVFileValidator(file, config)

file

Type: File

.csv file

config

Type: Object

Config object should contain headers array, array of row header (title) objects

const config = {
    headers: []
}

name

Type: String

name of the row header (title)

inputName

Type: String

key name which will be return with value in a column

required

Type: Boolean

If required is true than a column value will be checked if it is not empty

requiredError

Type: Function|String

If value is empty requiredError function will be called with arguments headerName, rowNumber, columnNumber

unique

Type: Boolean

If it is true all header (title) column values will be checked for uniqueness

uniqueError

Type: Function|String

If one of the header value is not unique uniqueError function will be called with argument headerName

validate

Type: Function

Validate column value. As an argument column value will be passed For e.g.

function(email) {
    return isEmailValid(email)
}

validateError

Type: Function|String

If validate returns false validateError function will be called with arguments headerName, rowNumber, columnNumber

isArray

Type: Boolean

If column containes list of values separated by comma in return object it will be as an array

Config example

const config = {
    headers: [
        { 
            name: 'First Name',
            inputName: 'firstName',
            required: true,
            requiredError: function (headerName, rowNumber, columnNumber) {
                return `${headerName} is required in the ${rowNumber} row / ${columnNumber} column`
            }
        },
        { 
            name: 'Last Name', 
            inputName: 'lastName', 
            required: false
        },
        { 
            name: 'Email', 
            inputName: 'email', 
            unique: true, 
            uniqueError: function (headerName) {
                return `${headerName} is not unique`
            }, 
            validate: function(email) {
                return isEmailValid(email)
            }, 
            validateError: function (headerName, rowNumber, columnNumber) {
                return `${headerName} is not valid in the ${rowNumber} row / ${columnNumber} column`
            }
        },
        { 
            name: 'Roles',
            inputName: 'roles',
            isArray: true
        }
    ]
}

您可能感兴趣的

Datasette Facets: Faceted Browse and a JSON API fo... Datasette Facets Datasette 0.22 is out with the most significant new feature I’ve added since the initial release: faceted browse . ...
Mark Needham: Go: Multi-threaded writing to a CSV ... As part of a Go script I’ve been working on I wanted to write to a CSV file from multiple Go routines, but realised that the built in CSV Writer isn’t...
利用java反射读写csv中的数据 前一段有个需求需要将从数据库读取到的信息保存到csv文件中,在实现该需求的时候发现资料比较少,经过收集反射和csv相关资料,最终得到了如下程序。 1.在使用java反射读取csv文件数据时,先通过csvreader jar包得到相关数据信息,然后格局反射去读指定对象的成员变量的信息,并转换数...
PHP创建的csv文件在windows上乱码的问题 前言 做PHP开发的时候,由于业务需要,可能要经常导出csv文件。使用PHP将数据导出到csv文件不难,使用fputcsv函数就可以了,但导出来的csv文件在windows系统上使用office excel打开时,中文部分会乱码(使用金山的WPS打开不会),这是由于windows系统默认...
CSV to php associative array My CSV file has this structure title;firstName;lastName;email; Sir;Bob;Public;; The first row shows the column names. Content starts with the seco...
0
Github

责编内容来自:Github (本文源链)
阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。