Skip to content

Commit

Permalink
Add option for error limit
Browse files Browse the repository at this point in the history
Default error limit is 1000. Having an error limit helps avoid cases
where the user's system runs out of memory.
  • Loading branch information
mint-thompson committed Mar 25, 2024
1 parent db07c40 commit 28e1bac
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,37 @@ npm install -g hpt-validator-cli

```sh
cms-hpt-validator --help
Usage: cms-hpt-validator [options] <filepath> <version>
Usage: index [options] <filepath> <version>

Arguments:
filepath filepath to validate
version (choices: "v1.1")
filepath filepath to validate
version (choices: "v2.0", "v2.0.0")

Options:
-f, --format <string> file format of file (choices: "csv", "json")
-h, --help display help for command
-f, --format <string> file format of file (choices: "csv", "json")
-e, --error-limit <value> maximum number for errors and warnings (default:
1000)
-h, --help display help for command
```

### Example
### Examples

Basic usage:

```sh
cms-hpt-validator ./sample.csv v2.0.0
```

Overriding the default error limit to show 50 errors and warnings:

```sh
cms-hpt-validator ./sample.csv v2.0.0 -e 50
```

Overriding the default error limit to show all errors and warnings:

```sh
cms-hpt-validator ./sample.csv
cms-hpt-validator ./sample.csv v2.0.0 -e 0
```

## Contributing
Expand Down
25 changes: 18 additions & 7 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import fs from "fs"
import path from "path"
import chalk from "chalk"
import { validateCsv, validateJson } from "hpt-validator"
import {
CsvValidationOptions,
JsonValidatorOptions,
validateCsv,
validateJson,
} from "hpt-validator"
import { ValidationResult } from "hpt-validator/src/types"
import { InvalidArgumentError } from "commander"

Expand All @@ -10,7 +15,7 @@ type FileFormat = "csv" | "json"
export async function validate(
filepath: string,
version: string,
options: { [key: string]: string }
options: { [key: string]: string | number }
) {
const format = getFileFormat(filepath, options)
if (!format) {
Expand All @@ -22,7 +27,10 @@ export async function validate(
const validationResult = await validateFile(
filepath,
version,
format as FileFormat
format as FileFormat,
{
maxErrors: options.errorLimit as number,
}
)
if (!validationResult) return

Expand Down Expand Up @@ -56,18 +64,21 @@ export async function validate(
async function validateFile(
filename: string,
version: string,
format: FileFormat
format: FileFormat,
validatorOptions: CsvValidationOptions | JsonValidatorOptions
): Promise<ValidationResult | null> {
const schemaVersion = version as "v1.1" | "v2.0"
if (format === "csv") {
return await validateCsv(
fs.createReadStream(filename, "utf-8"),
schemaVersion
schemaVersion,
validatorOptions as CsvValidationOptions
)
} else if (format === "json") {
return await validateJson(
fs.createReadStream(filename, "utf-8"),
schemaVersion
schemaVersion,
validatorOptions as JsonValidatorOptions
)
} else {
return null
Expand All @@ -76,7 +87,7 @@ async function validateFile(

function getFileFormat(
filepath: string,
fileFormat: { [key: string]: string }
fileFormat: { [key: string]: string | number }
): FileFormat | null {
if (fileFormat.format) return fileFormat.format as FileFormat

Expand Down
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { program, Argument, Option } from "commander"
import { program, Argument, Option, InvalidArgumentError } from "commander"
import { validate } from "./commands.js"

main().catch((error) => {
Expand All @@ -17,7 +17,21 @@ async function main() {
"json",
])
)
.option(
"-e, --error-limit <value>",
"maximum number for errors and warnings",
ensureInt,
1000
)
.action(validate)

program.parseAsync(process.argv)
}

function ensureInt(value: string) {
const parsedValue = parseInt(value, 10)
if (isNaN(parsedValue)) {
throw new InvalidArgumentError("Must be a number.")
}
return parsedValue
}

0 comments on commit 28e1bac

Please sign in to comment.