项目作者: travishorn

项目描述 :
Check CSV files against a set of validation rules.
高级语言: JavaScript
项目地址: git://github.com/travishorn/csval.git
创建时间: 2019-09-12T20:49:28Z
项目社区:https://github.com/travishorn/csval

开源协议:MIT License

下载


csval

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

  1. npm install --global csval

Usage

Run csval and give a CSV file as the first argument

  1. 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

  1. 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.

  1. {
  2. "type": "object",
  3. "properties": {
  4. "salary": {
  5. "type": "number"
  6. }
  7. }
  8. }

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.

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

This CSV file would fail.

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

Here’s another example rules file.

  1. {
  2. "properties": {
  3. "age": {
  4. "type": "number",
  5. "minimum": 0
  6. }
  7. }
  8. }

This CSV file would pass.

  1. name,age
  2. John,30
  3. Jane,50

But this one would fail.

  1. name,age
  2. John,30
  3. Jane,-10

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

  1. {
  2. "properties": {
  3. "age": {
  4. "type": "number"
  5. }
  6. },
  7. "required": ["age"]
  8. }

This CSV file would pass.

  1. name,age,salary
  2. John,30,100000
  3. Jane,50,150000

This one would fail.

  1. name,salary
  2. John,100000
  3. Jane,150000

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

Programmatic API

Install the library

  1. npm install csval

Use it in your project like so

  1. import { parseCsv, validate } from "csval";
  2. const main = async () => {
  3. const csvString= "name,age\nJohn,30";
  4. const rules = {
  5. properties: {
  6. name: {
  7. type: "string"
  8. }
  9. }
  10. };
  11. const parsed = await parseCsv(csvString);
  12. const valid = await validate(parsed, rules);
  13. // validate will either throw an error or valid will be true
  14. };
  15. main();

You can also read CSV data and rules from files.

  1. import { readCsv, readRules, parseCsv, validate } from "csval";
  2. const csvString = await readCsv("path/to/file.csv");
  3. const rules = await readRules("path/to/rules.json");
  4. const parsed = await parseCsv(csvString);
  5. const valid = await validate(parsed, rules);
  6. // validate will either throw an error or valid will be true

Develop

Clone the repository

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

Change into the directory

  1. cd csval

Install dependencies

  1. npm install

Tests

Run tests via Mocha

  1. npm run test

Lint

Lint all JavaScript files via ESLint and Prettier

  1. npm run lint

Automatically fix linting problems if possible

  1. 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.