Skip to content

Commit

Permalink
refactor: runWithResolved
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Aug 19, 2019
1 parent 79908f2 commit 6e1dbfd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
42 changes: 22 additions & 20 deletions src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,26 +628,6 @@ responses:: !!foo
expect(result).toEqual([]);
});

test('should include both resolved and validation results if includeResolved is true', async () => {
spectral.addRules({
'no-info': {
// some dumb rule to have some error
message: 'should be OK',
given: '$.info',
then: {
function: 'falsy',
},
},
});
spectral.addFunctions(oas3Functions());

const { result } = await new Resolver().resolve(parse(petstoreMergeKeys));
const { resolved, results } = await spectral.run(petstoreMergeKeys, { includeResolved: true });

expect(resolved).toEqual(result);
expect(results).toEqual([expect.objectContaining({ code: 'no-info' })]);
});

describe('reports duplicated properties for', () => {
test('JSON format', async () => {
const result = await spectral.run({
Expand Down Expand Up @@ -923,4 +903,26 @@ responses:: !!foo
});
});
});

describe('runWithResolved', () => {
test('should include both resolved and validation results', async () => {
spectral.addRules({
'no-info': {
// some dumb rule to have some error
message: 'should be OK',
given: '$.info',
then: {
function: 'falsy',
},
},
});
spectral.addFunctions(oas3Functions());

const { result } = await new Resolver().resolve(parse(petstoreMergeKeys));
const { resolved, results } = await spectral.runWithResolved(petstoreMergeKeys);

expect(resolved).toEqual(result);
expect(results).toEqual([expect.objectContaining({ code: 'no-info' })]);
});
});
});
26 changes: 9 additions & 17 deletions src/spectral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,10 @@ export class Spectral {
this.formats = {};
}

public async run(
target: IParsedResult | object | string,
opts?: Omit<IRunOpts, 'includeResolved'> & { includeResolved?: false },
): Promise<IRuleResult[]>;
public async run(
target: IParsedResult | object | string,
opts: Omit<IRunOpts, 'includeResolved'> & { includeResolved: true },
): Promise<ISpectralFullResult>;
public async run(
public async runWithResolved(
target: IParsedResult | object | string,
opts: IRunOpts = {},
): Promise<IRuleResult[] | ISpectralFullResult> {
): Promise<ISpectralFullResult> {
let results: IRuleResult[] = [];

let parsedResult: IParsedResult | IParsedResult<YamlParserResult<unknown>>;
Expand Down Expand Up @@ -138,14 +130,14 @@ export class Spectral {
...runRules(resolved, this.rules, this.functions),
];

if (opts.includeResolved) {
return {
resolved: resolved.result,
results: validationResults,
};
}
return {
resolved: resolved.result,
results: validationResults,
};
}

return validationResults;
public async run(target: IParsedResult | object | string, opts: IRunOpts = {}): Promise<IRuleResult[]> {
return (await this.runWithResolved(target, opts)).results;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/types/spectral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export interface IRunOpts {
resolve?: {
documentUri?: string;
};
includeResolved?: boolean;
}

export interface IRuleResult extends IDiagnostic {
Expand Down

0 comments on commit 6e1dbfd

Please sign in to comment.