Skip to content

Commit db60c11

Browse files
author
Mike Kistler
committed
feat(spectral): allow spectral ruleset file to be specified with command line option
1 parent cafad9e commit db60c11

File tree

6 files changed

+13
-4
lines changed

6 files changed

+13
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Once pulled, the container can be run directly, but mount a volume containing th
8686
- -v (--version) : Print the current semantic version of the validator
8787
- -h (--help) : This option prints the usage menu.
8888
- -c (--config) <path/to/your/config> : Path to a validator configuration file. If provided, this is used instead of .validaterc.
89+
- -r (--ruleset) <path/to/your/ruleset> : Path to Spectral ruleset file, used instead of .spectral.yaml if provided.
8990
- --debug : Enable debugging output.
9091

9192
_These options only apply to running the validator on a file, not to any commands._

src/cli-validator/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ program
4040
.option(
4141
'-c, --config <file>', 'path to config file, used instead of .validaterc if provided'
4242
)
43+
.option(
44+
'-r, --ruleset <file>', 'path to Spectral ruleset file, used instead of .spectral.yaml if provided'
45+
)
4346
.option(
4447
'-e, --errors_only',
4548
'only print the errors, ignore the warnings'

src/cli-validator/runValidator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const processInput = async function(program) {
4242
const debug = !!program.debug;
4343

4444
const configFileOverride = program.config;
45+
const rulesetFileOverride = program.ruleset;
4546

4647
const limitsFileOverride = program.limits;
4748

@@ -166,7 +167,7 @@ const processInput = async function(program) {
166167
// or the default ruleset
167168
const spectral = new Spectral();
168169
try {
169-
await spectralValidator.setup(spectral, configObject);
170+
await spectralValidator.setup(spectral, rulesetFileOverride, configObject);
170171
} catch (err) {
171172
return Promise.reject(err);
172173
}

src/cli-validator/utils/processConfiguration.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,16 @@ const validateConfigOption = function(userOption, defaultOption) {
373373
return result;
374374
};
375375

376-
const getSpectralRuleset = async function(defaultRuleset) {
376+
const getSpectralRuleset = async function(rulesetFileOverride, defaultRuleset) {
377377
// List of ruleset files to search for
378378
const ruleSetFilesToFind = [
379379
'.spectral.yaml',
380380
'.spectral.yml',
381381
'.spectral.json'
382382
];
383+
if (rulesetFileOverride) {
384+
ruleSetFilesToFind.splice(0, 0, rulesetFileOverride);
385+
}
383386
let ruleSetFile;
384387

385388
// search up the file system for the first ruleset file found

src/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = async function(input, defaultMode = false) {
1616

1717
try {
1818
configObject = await config.get(defaultMode, chalk);
19-
await spectralValidator.setup(spectral, configObject);
19+
await spectralValidator.setup(spectral, null, configObject);
2020
} catch (err) {
2121
return Promise.reject(err);
2222
}

src/spectral/utils/spectral-validator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const parseResults = function(results, debug) {
5454
};
5555

5656
// setup: registers the oas2/oas3 formats, and attempts to load the ruleset file
57-
const setup = async function(spectral, configObject) {
57+
const setup = async function(spectral, rulesetFileOverride, configObject) {
5858
if (!spectral) {
5959
const message =
6060
'Error (spectral-validator): An instance of spectral has not been initialized.';
@@ -73,6 +73,7 @@ const setup = async function(spectral, configObject) {
7373

7474
// load the spectral ruleset, either a user's or the default ruleset
7575
const spectralRulesetURI = await config.getSpectralRuleset(
76+
rulesetFileOverride,
7677
defaultSpectralRulesetURI
7778
);
7879

0 commit comments

Comments
 (0)