From 158eec3094985af002425807132fc94882d88e23 Mon Sep 17 00:00:00 2001 From: Mint Thompson Date: Tue, 26 Mar 2024 14:23:17 -0400 Subject: [PATCH] Add option for error limit (#6) * 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 --- README.md | 42 ++++++++++++++++++++++++++++++++++-------- src/commands.ts | 25 ++++++++++++++++++------- src/index.ts | 16 +++++++++++++++- 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a624a7c..a10ef72 100644 --- a/README.md +++ b/README.md @@ -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 @@ -16,23 +22,43 @@ npm install -g hpt-validator-cli ```sh cms-hpt-validator --help -Usage: cms-hpt-validator [options] +Usage: index [options] Arguments: - filepath filepath to validate - version (choices: "v1.1") + filepath filepath to validate + version (choices: "v2.0", "v2.0.0") Options: - -f, --format file format of file (choices: "csv", "json") - -h, --help display help for command + -f, --format file format of file (choices: "csv", "json") + -e, --error-limit 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 diff --git a/src/commands.ts b/src/commands.ts index 47da9af..c1e7479 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -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" @@ -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) { @@ -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 @@ -56,18 +64,21 @@ export async function validate( async function validateFile( filename: string, version: string, - format: FileFormat + format: FileFormat, + validatorOptions: CsvValidationOptions | JsonValidatorOptions ): Promise { 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 @@ -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 diff --git a/src/index.ts b/src/index.ts index 1c18a21..5fe3517 100755 --- a/src/index.ts +++ b/src/index.ts @@ -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) => { @@ -17,7 +17,21 @@ async function main() { "json", ]) ) + .option( + "-e, --error-limit ", + "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 +}