npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

csval

v2.0.0

Published

Check CSV files against a set of validation rules.

Downloads

545

Readme

csval

Build Status

Check CSV files against a set of validation rules.

Validation Checks

  • CSV file is actually valid itself and can be parsed
  • Presence of required fields
  • Mismatching types. For example, number vs string
  • Minimum and maxiumum lengths
  • Number ranges
  • Values from a fixed set of options
  • Regex pattern matching
  • Much more. Check the JSON Schema reference for more information

CLI Installation

npm install --global csval

Usage

Run csval and give a CSV file as the first argument

csval mydata.csv

Since no rules were specified above, the file is only checked to make sure it can be parsed correctly. As long as it's a valid CSV file, it will pass validation. The CLI will show errors if they exist. Otherwise, it will display a success message.

Pass in a rules file to validate against the rules

csval mydata.csv myrules.json

Again, the CLI will show parsing errors if they exist. When a rules file is specified as it is above, the CLI will also display any validation errors. Otherwise, it will display a success message.

Rules file

Rules files should follow the JSON Schema format. It describes what you should expect in each row. Here's an example.

{
  "type": "object",
  "properties": {
    "salary": {
      "type": "number"
    }
  }
}

Note: The "type": "object" line above is implied and can be left out if desired.

The rules above say that the "salary" field on each row must be a number. This CSV file would pass.

name,salary
John,100000
Jane,150000
The CSV file meets all validation checks.

This CSV file would fail.

name,salary
John,100000
Jane,idk
Row 3: 'salary' must be number

Here's another example rules file.

{
  "properties": {
    "age": {
      "type": "number",
      "minimum": 0
    }
  }
}

This CSV file would pass.

name,age
John,30
Jane,50

But this one would fail.

name,age
John,30
Jane,-10

You can require certain fields, as well. Consider this rules file.

{
  "properties": {
    "age": {
      "type": "number"
    }
  },
  "required": ["age"]
}

This CSV file would pass.

name,age,salary
John,30,100000
Jane,50,150000

This one would fail.

name,salary
John,100000
Jane,150000

There are many other possible rules. See the JSON Schema for more information.

Programmatic API

Install the library

npm install csval

Use it in your project like so

import { parseCsv, validate } from "csval";

const main = async () => {
  const csvString= "name,age\nJohn,30";

  const rules = {
    properties: {
      name: {
        type: "string"
      }
    }
  };

  const parsed = await parseCsv(csvString);
  const valid = await validate(parsed, rules);

  // validate will either throw an error or valid will be true
};

main();

You can also read CSV data and rules from files.

import { readCsv, readRules, parseCsv, validate } from "csval";

const csvString = await readCsv("path/to/file.csv");
const rules = await readRules("path/to/rules.json");
const parsed = await parseCsv(csvString);
const valid = await validate(parsed, rules);

// validate will either throw an error or valid will be true

Develop

Clone the repository

git clone https://github.com/travishorn/csval.git

Change into the directory

cd csval

Install dependencies

npm install

Tests

Run tests via Mocha

npm run test

Lint

Lint all JavaScript files via ESLint and Prettier

npm run lint

Automatically fix linting problems if possible

npm run lint:fix

License

The MIT License

Copyright 2023 Travis Horn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.