Git Product home page Git Product logo

csv-file-validator's Introduction

CSV File Validator Twitter URL

MIT Licence codecov Build Status npm version

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)

returns the Promise

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
        }
    ]
}

Contributing

Any contributions you make are greatly appreciated.

Please read the Contributions Guidelines before submitting a PR.

License

MIT ยฉ Vasyl Stokolosa

csv-file-validator's People

Contributors

shystruk avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.