From 28e1baca0f5b95bc20e25b6ef3486a7ed91bc28c Mon Sep 17 00:00:00 2001 From: Mint Thompson Date: Fri, 22 Mar 2024 13:45:07 -0400 Subject: [PATCH] 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. --- README.md | 30 +++++++++++++++++++++++------- src/commands.ts | 25 ++++++++++++++++++------- src/index.ts | 16 +++++++++++++++- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a624a7c..f158012 100644 --- a/README.md +++ b/README.md @@ -16,21 +16,37 @@ 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 ``` -### 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 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 +}