Skip to content

Commit

Permalink
fix: prevent ruleset to be mutated
Browse files Browse the repository at this point in the history
SL-786
- deep cloning the provided rulset
  • Loading branch information
Chris Miaskowski committed Dec 3, 2018
1 parent e25d46f commit a793b6c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/__tests__/spectral.test.ts
Expand Up @@ -14,6 +14,55 @@ describe('spectral', () => {
expect(results.length).toBeGreaterThan(0);
});

test('setting rules should not mutate the original ruleset', () => {
const givenCustomRuleSet = {
rules: {
oas2: {
rule1: {
type: RuleType.STYLE,
function: RuleFunction.TRUTHY,
path: '$',
enabled: true,
summary: '',
input: {
properties: 'something',
},
},
},
},
};
const expectedCustomRuleSet = {
rules: {
oas2: {
rule1: {
type: RuleType.STYLE,
function: RuleFunction.TRUTHY,
path: '$',
enabled: true,
summary: '',
input: {
properties: 'something',
},
},
},
},
};

const s = new Spectral({ rulesets: [givenCustomRuleSet] });

s.setRules([
{
rules: {
oas2: {
rule1: false,
},
},
},
]);

expect(expectedCustomRuleSet).toEqual(givenCustomRuleSet);
});

test('be able to toggle rules on apply', () => {
const spec = {
hello: 'world',
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Expand Up @@ -92,7 +92,7 @@ export class Spectral {
}

public setRules(rulesets: types.IRuleset[]) {
this._rulesets = rulesets;
this._rulesets = merge([], rulesets);
this._functions = this._rulesetsToFunctions(this._rulesets);
this._rules = this._rulesetsToRules(this._rulesets);
}
Expand Down Expand Up @@ -126,7 +126,7 @@ export class Spectral {
if (rule.path !== path) {
console.warn(
`Rule '${ruleName} was categorized under an incorrect path. Was under ${path}, but rule path is set to ${
rule.path
rule.path
}`
);
continue;
Expand Down

0 comments on commit a793b6c

Please sign in to comment.