Skip to content

Commit

Permalink
Add option for error limit (#6)
Browse files Browse the repository at this point in the history
* Add option for error limit

Default error limit is 1000. Having an error limit helps avoid cases
where the user's system runs out of memory.

* Update README.md

Adding additional prereq documentation.

* Update README.md

Including 'limitation' language to account for error and memory limitations.

---------

Co-authored-by: scott haselton <shaselton.usds@gmail.com>
  • Loading branch information
mint-thompson and shaselton-usds authored Mar 26, 2024
1 parent db07c40 commit 158eec3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Hospital Price Transparency Validator CLI
# Hospital Price Transparency CLI Validator

CLI for validating CMS Hospital Price Transparency machine-readable files

## Getting Started

### Prerequisites
These were the minimum versions used to develop the CLI tool. It is recommended to keep both Node and NPM up-to-date with the latest releases.

* Node (version 16.x)
* NPM (version 8.5.x)

### Installation

Install the CLI globally with
Expand All @@ -16,23 +22,43 @@ 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
```

### Examples

Basic usage:

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

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

```sh
cms-hpt-validator ./sample.csv
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 v2.0.0 -e 0
```

## Limitations
There may be a situation in which the CLI tool will run out of memory due to the amount of errors that are found in the file being validated. If you run into this NODE error, update the amount of errors to a smaller value that will be allowed to be collected with the `-e, --error-limit` flag.


## Contributing

Thank you for considering contributing to an Open Source project of the US
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 158eec3

Please sign in to comment.