🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

csv-import-validation

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

csv-import-validation

Validation library for CSV files against defined schema

unpublished
latest
Source
npmnpm
Version
0.1.1
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

CSV Import Validation

Validation library for CSV files against defined schema. Easy to use and customize. Developed by Logic Spark team.

CSV import validation is a Typescript library that is utilize the function to read CSV data from ExcelJS.



:building_construction: Built With

Here is a list of frameworks/libraries used to develop CSV Import Validation

:rocket: Getting Started

CSV Import Validation library checks data validation for CSV files against defined schema and returns data and validation messages as an object. In this guide, you will learn what is required before using the library and how to install it quickly. Let's get started!

Prerequisite

Before installing the CSV Import Validation library, please install the most recent Node.js version

  • npm
  npm install npm@latest -g

Support Node.js 14.21.0 and above

Installation

Since the example is based on npm, you can use npm to install CSV Import Validation. You can also install via yarn or pnpm.

npm install

npm install csv-import-validation

Other options

Or yarn

yarn add csv-import-validation

Or pnpm

pnpm install csv-import-validation

(back to top)

:fire: Usage

There are 2 main features in the library. One reads CSV and returns an array while another reads CSV and returns as objects with validation messages. For each feature, it can be read via a file path or buffer. The functions are independent and do not need to be called in sequence.

To use the library, you will need to import to your targeted file

import { CsvUtilities } from "csv-import-validation";

Functions that read CSV and return an array

There are two functions, namely:

  • function readFileMapArray: read CSV by specifying a file path to the CSV file
  • function readBufferMapArray: read CSV from Buffer
const filePath = "files/your-csv-file-name.csv";
const reading = readCsvFileMapArray(filePath);
  const reading = readBufferMapArray(ฺbuffer);

Each function has the same output as below:

[
  [
    "Order ID",
    "Product Name",
    "Customer Name",
    "Quantity",
    "Price",
    "Discount",
    "Total",
    "Region",
    "Category",
    "Discount Rate",
  ],
  [
    "1",
    "Eldon Base for stackable storage shelf, platinum",
    "Muhammed MacIntyre",
    "3",
    "-213.25",
    "38.94",
    "35",
    "Nunavut",
    "Storage & Organization",
    "0.8",
  ],
];

Functions that read CSV and return objects and validations

There are two functions, namely:

  • readFileValidator: read CSV by specifying a file path to the CSV file and validate data
  • readBufferValidator: read CSV from Buffer and validate data. The validation can be applied to the header name and data type of each column.

If you import with validation, data will only be imported if there is no validation message.

const csvConfig: ValidationConfig = [
  { headerName: "name", keyName: "name", type: "string" },
  { headerName: "ip", keyName: "ip", type: "string" },
  { headerName: "buildingId", keyName: "buildingId", type: "number" },
  { headerName: "floorId", keyName: "floorId", type: "number" },
  { headerName: "zoneId", keyName: "zoneId", type: "string" },
  { headerName: "room", keyName: "room", type: "string" },
  { headerName: "type", keyName: "type", type: "string" },
];

const filePath = "files/your-csv-file-name.csv";
const data = await csvUtilities.readFileValidator(filePath, csvConfig);
const csvConfig: ValidationConfig = [
  { headerName: "name", keyName: "name", type: "string" },
  { headerName: "ip", keyName: "ip", type: "string" },
  { headerName: "buildingId", keyName: "buildingId", type: "number" },
  { headerName: "floorId", keyName: "floorId", type: "number" },
  { headerName: "zoneId", keyName: "zoneId", type: "string" },
  { headerName: "room", keyName: "room", type: "string" },
  { headerName: "type", keyName: "type", type: "string" },
];
const data = await csvUtilities.readBufferValidator(buffer, csvConfig);

Configuration Parameters

ParameterDescription
headerNameSpecify header name of each column to validate with a message
keyNameSpecify key to map data of the corresponding column
typeSpecify data type of each column (number, string)

Each function has the same output. If there is no validation message, the output will return objects of data imported.

{
  "data": [
    {
      "orderID": 1,
      "productName": "Eldon Base for stackable storage shelf, platinum",
      "customerName": "Muhammed MacIntyre",
      "quantity": 3,
      "price": -213.25,
      "discount": 38.94,
      "total": "35",
      "region": "Nunavut",
      "category": "Storage & Organization",
      "discountRate": 0.8
    },
    {
      "orderID": 2,
      "productName": "1.7 Cubic Foot Compact \"Cube\" Office Refrigerators",
      "customerName": "Barry French",
      "quantity": 293,
      "price": 457.81,
      "discount": 208.16,
      "total": "68.02",
      "region": "Nunavut",
      "category": "Appliances",
      "discountRate": 0.58
    }
  ]
}

If there is validation, the data will not be imported and the output will return locations that need to be fixed before importing again.

{
  "invalidData": [
    {
      "rowIndex": 1,
      "columnIndex": "A",
      "message": "Order ID's type is number."
    },
    {
      "rowIndex": 1,
      "columnIndex": "D",
      "message": "Quantity's type is number."
    },
    {
      "rowIndex": 7,
      "columnIndex": "A",
      "message": "Order ID's type is number."
    }
  ]
}

For more details, please see our demo folder. There are demos for both JavaScript and TypeScript. In the example-files folder, we provide csv files with and without header for your ease of use.

(back to top)

:books: License

Distributed under the MIT license. See LICENSE.txt for more information.

ExcelJS is licensed under the MIT license.

(back to top)

:pray: Acknowledgement

(back to top)

Keywords

typescript

FAQs

Package last updated on 31 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts