From 99be247e1374248f685bae353af3cf37830e5aa0 Mon Sep 17 00:00:00 2001 From: Thomas Haugland Rudfoss Date: Fri, 15 Nov 2024 09:42:07 +0000 Subject: [PATCH] chore(cli): fix type issues --- docs/guides/2-cli.md | 2 +- packages/cli/src/commands/lint.ts | 2 +- packages/cli/src/services/config.ts | 2 +- packages/cli/src/services/linter/linter.ts | 20 +++++++++++++++----- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/docs/guides/2-cli.md b/docs/guides/2-cli.md index c434e4bc5..14ae0531f 100644 --- a/docs/guides/2-cli.md +++ b/docs/guides/2-cli.md @@ -47,7 +47,7 @@ Other options include: --fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false] -v, --verbose increase verbosity [boolean] -q, --quiet no logging - output only [boolean] - -p, --parser specify the parser used to read the file ["JsonParserResult", "parseJson", "Json", "parseYaml", "Yaml"] + -p, --parser specify the parser used to read the file ["Json", "Yaml"] ``` The Spectral CLI supports loading documents as YAML or JSON, and validation of OpenAPI v2/v3 documents via the built-in ruleset. The default parser reads YAML files, when loading JSON documents you should specify `--parser Json`. diff --git a/packages/cli/src/commands/lint.ts b/packages/cli/src/commands/lint.ts index e2dcd243f..8feba8883 100644 --- a/packages/cli/src/commands/lint.ts +++ b/packages/cli/src/commands/lint.ts @@ -164,7 +164,7 @@ const lintCommand: CommandModule = { parser: { alias: 'p', description: 'specify the parser used to read the file', - choices: ["JsonParserResult", "parseJson", "Json", "parseYaml", "Yaml"], + choices: ["Json", "Yaml"], default: 'Yaml', type: 'string', } diff --git a/packages/cli/src/services/config.ts b/packages/cli/src/services/config.ts index 435622c29..eba168543 100644 --- a/packages/cli/src/services/config.ts +++ b/packages/cli/src/services/config.ts @@ -30,5 +30,5 @@ export interface ILintConfig { failOnUnmatchedGlobs: boolean; verbose?: boolean; quiet?: boolean; - parser?: keyof typeof Parsers + parser?: keyof Pick } diff --git a/packages/cli/src/services/linter/linter.ts b/packages/cli/src/services/linter/linter.ts index ca1b9e2da..d9339bcba 100644 --- a/packages/cli/src/services/linter/linter.ts +++ b/packages/cli/src/services/linter/linter.ts @@ -63,13 +63,23 @@ const createDocument = async ( identifier: string | number, opts: IFileReadOptions, source: string, - parser: keyof typeof Parsers = "Yaml" -): Promise>> => { - const parserImpl = Parsers[parser] + parser: ILintConfig["parser"] = "Yaml" +) => { + // I wanted to dynamically assign the parser using something like this: + // const parserImplementation = Parsers[parser] + // but I got a type-error on new Document() that I was unable to resolve dynamically (not sure why) + + if (parser === "Json") { + if (typeof identifier === 'string') { + return new Document(await readParsable(identifier, opts), Parsers.Json, identifier); + } + + return new Document(await readFileDescriptor(identifier, opts), Parsers.Json, source); + } if (typeof identifier === 'string') { - return new Document(await readParsable(identifier, opts), parserImpl, identifier); + return new Document(await readParsable(identifier, opts), Parsers.Yaml, identifier); } - return new Document(await readFileDescriptor(identifier, opts), parserImpl, source); + return new Document(await readFileDescriptor(identifier, opts), Parsers.Yaml, source); };