Skip to content

Commit

Permalink
add changed and added or changed to spectral ruleset (#2328)
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim authored Sep 21, 2023
1 parent 645cafb commit f77533d
Showing 1 changed file with 77 additions and 9 deletions.
86 changes: 77 additions & 9 deletions projects/standard-rulesets/src/spectral/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,47 @@ const configSchema = {
description: 'URI of spectral ruleset file (file or URL)',
},
},
changed: {
type: 'array',
items: {
type: 'string',
description: 'URI of spectral ruleset file (file or URL)',
},
},
addedOrChanged: {
type: 'array',
items: {
type: 'string',
description: 'URI of spectral ruleset file (file or URL)',
},
},
},
};
const validateConfigSchema = ajv.compile(configSchema);

export class SpectralRulesets extends ExternalRuleBase {
constructor(
private options: {
always: string[];
added: string[];
matches?: (context: RuleContext) => boolean;
}
) {
private options: {
always: string[];
added: string[];
changed: string[];
addedOrChanged: string[];
matches?: (context: RuleContext) => boolean;
};
constructor(options: {
always?: string[];
added?: string[];
changed?: string[];
addedOrChanged?: string[];
matches?: (context: RuleContext) => boolean;
}) {
super();
this.options = {
always: options.always ?? [],
added: options.added ?? [],
changed: options.changed ?? [],
addedOrChanged: options.addedOrChanged ?? [],
matches: options.matches,
};
}

async runRules(inputs: {
Expand Down Expand Up @@ -78,6 +106,39 @@ export class SpectralRulesets extends ExternalRuleBase {
matches: this.options.matches,
});
});
const changed = this.options.changed.map((ruleInput) => {
return new SpectralRule({
name:
'Spectral Rules applied to changes to the specification: ' +
ruleInput,
flatSpecFile: absolutePathTmpSpec,
applies: 'changed',
rulesetPointer: ruleInput,
matches: this.options.matches,
});
});
const addedOrChanged = this.options.addedOrChanged.flatMap((ruleInput) => {
return [
new SpectralRule({
name:
'Spectral Rules applied to additions to the specification: ' +
ruleInput,
flatSpecFile: absolutePathTmpSpec,
applies: 'added',
rulesetPointer: ruleInput,
matches: this.options.matches,
}),
new SpectralRule({
name:
'Spectral Rules applied to changes to the specification: ' +
ruleInput,
flatSpecFile: absolutePathTmpSpec,
applies: 'changed',
rulesetPointer: ruleInput,
matches: this.options.matches,
}),
];
});
const always = this.options.always.map((ruleInput) => {
return new SpectralRule({
name: 'Spectral Rules applied to entire specification: ' + ruleInput,
Expand All @@ -88,7 +149,7 @@ export class SpectralRulesets extends ExternalRuleBase {
});
});

const allRulesets = [...always, ...added];
const allRulesets = [...always, ...added, ...changed, ...addedOrChanged];

const allResults = await Promise.all(
allRulesets.map((ruleset) => ruleset.runRules(inputs))
Expand Down Expand Up @@ -160,11 +221,18 @@ export class SpectralRulesets extends ExternalRuleBase {
})
.join('\n- ');
}
const configValidated = config as { added?: string[]; always?: string[] };
const configValidated = config as {
added?: string[];
always?: string[];
changed?: string[];
addedOrChanged?: string[];
};

return new SpectralRulesets({
added: configValidated.added ?? [],
always: configValidated.always ?? [],
changed: configValidated.changed ?? [],
addedOrChanged: configValidated.addedOrChanged ?? [],
});
}
}

0 comments on commit f77533d

Please sign in to comment.