Skip to content

Commit

Permalink
chore(cli): fix type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rudfoss-rr committed Nov 15, 2024
1 parent c825d5b commit 99be247
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/guides/2-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export interface ILintConfig {
failOnUnmatchedGlobs: boolean;
verbose?: boolean;
quiet?: boolean;
parser?: keyof typeof Parsers
parser?: keyof Pick<typeof Parsers, "Yaml" | "Json">
}
20 changes: 15 additions & 5 deletions packages/cli/src/services/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,23 @@ const createDocument = async (
identifier: string | number,
opts: IFileReadOptions,
source: string,
parser: keyof typeof Parsers = "Yaml"
): Promise<Document<unknown, Parsers.YamlParserResult<unknown>>> => {
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);
};

0 comments on commit 99be247

Please sign in to comment.