Skip to content

Commit

Permalink
Rewrite using ESM and Ajv
Browse files Browse the repository at this point in the history
  • Loading branch information
travishorn committed Feb 7, 2023
1 parent fa04912 commit 633400e
Show file tree
Hide file tree
Showing 20 changed files with 2,914 additions and 4,270 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
extends: ["eslint:recommended", "plugin:prettier/recommended"],
parserOptions: { ecmaVersion: "latest", sourceType: "module" },
env: { node: true },
};
13 changes: 0 additions & 13 deletions .eslintrc.js

This file was deleted.

2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
coverage/
node_modules/
.idea
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- 10
- 18

script:
- npm run lint
Expand Down
53 changes: 24 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ Check CSV files against a set of validation rules.
- Values from a fixed set of options
- Regex pattern matching
- Much more. Check the [JSON Schema
reference](https://json-schema.org/understanding-json-schema/reference/index.html)
for more information
reference](https://ajv.js.org/json-schema.html) for more information

## CLI Installation

Expand Down Expand Up @@ -48,8 +47,7 @@ Otherwise, it will display a success message.

### Rules file

Rules files should follow the [JSON
Schema](https://json-schema.org/understanding-json-schema/reference/index.html)
Rules files should follow the [JSON Schema](https://ajv.js.org/json-schema.html)
format. It describes what you should expect in each row. Here's an example.

```json
Expand All @@ -75,6 +73,10 @@ John,100000
Jane,150000
```

```
The CSV file meets all validation checks.
```

This CSV file would fail.

```
Expand All @@ -83,6 +85,10 @@ John,100000
Jane,idk
```

```
Row 3: 'salary' must be number
```

Here's another example rules file.

```json
Expand Down Expand Up @@ -142,8 +148,7 @@ Jane,150000
```

There are many other possible rules. See the [JSON
Schema](https://json-schema.org/understanding-json-schema/reference/index.html)
for more information.
Schema](https://ajv.js.org/json-schema.html) for more information.

## Programmatic API

Expand All @@ -156,7 +161,7 @@ npm install csval
Use it in your project like so

```javascript
const csval = require("csval");
import { parseCsv, validate } from "csval";

const main = async () => {
const csvString= "name,age\nJohn,30";
Expand All @@ -169,10 +174,10 @@ const main = async () => {
}
};

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

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

main();
Expand All @@ -181,18 +186,14 @@ main();
You can also read CSV data and rules from files.

```javascript
const csval = require("csval");

const main = async () => {
const csvString= await readCsv("path/to/file.csv");
const rules = await readRules("path/to/rules.json");
const parsed = await csval.parseCsv(csvString);
const valid = await csval.validate(parsed, rules);
import { readCsv, readRules, parseCsv, validate } from "csval";

// csval.validate will either throw an error or valid will be true
};
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);

main();
// validate will either throw an error or valid will be true
```

## Develop
Expand All @@ -217,21 +218,15 @@ npm install

## Tests

Run tests via Jest
Run tests via Mocha

```
npm run test
```

View test coverage

```
npm run test:coverage
```

## Lint

Lint all JavaScript files
Lint all JavaScript files via ESLint and Prettier

```
npm run lint
Expand All @@ -247,7 +242,7 @@ npm run lint:fix

The MIT License

Copyright 2019 Travis Horn
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
Expand Down
Loading

0 comments on commit 633400e

Please sign in to comment.