Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for error limit #6

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading