Skip to content
Closed
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
2 changes: 2 additions & 0 deletions docs/guides/2-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Other options include:
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
--show-documentation-url show documentation url in output result [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml)
[string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]
```
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ const lintCommand: CommandModule = {
description: 'path/URL to a ruleset file',
type: 'string',
},
parser: {
alias: 'p',
description: 'which parser should be used to read the file (defaults to Yaml)',
type: 'string',
choices: ['Yaml', 'Json'],
default: 'Yaml',
},
'fail-severity': {
alias: 'F',
description: 'results of this level or above will trigger a failure exit code',
Expand Down Expand Up @@ -178,6 +185,7 @@ const lintCommand: CommandModule = {
format,
output,
encoding,
parser,
ignoreUnknownFormat,
failOnUnmatchedGlobs,
showDocumentationUrl,
Expand All @@ -193,6 +201,7 @@ const lintCommand: CommandModule = {
format,
output,
encoding,
parser,
ignoreUnknownFormat,
failOnUnmatchedGlobs,
showDocumentationUrl,
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/services/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Dictionary } from '@stoplight/types';
import type * as Parsers from '@stoplight/spectral-parsers';
import { HumanReadableDiagnosticSeverity } from '@stoplight/spectral-core';

export type FailSeverity = HumanReadableDiagnosticSeverity;
Expand All @@ -24,6 +25,7 @@ export interface ILintConfig {
output?: Dictionary<string>;
resolver?: string;
ruleset?: string;
parser?: keyof Pick<typeof Parsers, 'Json' | 'Yaml'>;
stdinFilepath?: string;
ignoreUnknownFormat: boolean;
failOnUnmatchedGlobs: boolean;
Expand Down
15 changes: 13 additions & 2 deletions packages/cli/src/services/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function lint(documents: Array<number | string>, flags: ILintConfig
console.info(`Linting ${targetUri}`);
}

const document = await createDocument(targetUri, { encoding: flags.encoding }, flags.stdinFilepath ?? '<STDIN>');
const document = await createDocument(targetUri, { encoding: flags.encoding }, flags.stdinFilepath ?? '<STDIN>', flags.parser);

results.push(
...(await spectral.run(document, {
Expand All @@ -63,7 +63,18 @@ const createDocument = async (
identifier: string | number,
opts: IFileReadOptions,
source: string,
): Promise<Document<unknown, Parsers.YamlParserResult<unknown>>> => {
parser: ILintConfig['parser'] = 'Yaml'
) => {
if (parser === "Json") {
// Due to type issues, the parser cannot be dynamically resolved using something like Parsers[parser]
// This causes new Document() to fail for Json types.
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), Parsers.Yaml, identifier);
}
Expand Down
1 change: 1 addition & 0 deletions test-harness/scenarios/formats/too-few-outputs.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Options:
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
--show-documentation-url show documentation url in output result [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml) [string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]

Expand Down
1 change: 1 addition & 0 deletions test-harness/scenarios/formats/too-many-outputs.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Options:
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
--show-documentation-url show documentation url in output result [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml) [string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Options:
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
--show-documentation-url show documentation url in output result [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml) [string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]

Expand Down
1 change: 1 addition & 0 deletions test-harness/scenarios/help-no-document.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Options:
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
--show-documentation-url show documentation url in output result [boolean] [default: false]
-p, --parser which parser should be used to read the file (defaults to Yaml) [string] [choices: "Yaml", "Json"] [default: "Yaml"]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]

Expand Down
Loading