Skip to content

Commit

Permalink
feat: expose resolved document (#433)
Browse files Browse the repository at this point in the history
* feat: expose resolved document

* refactor: runWithResolved
  • Loading branch information
P0lip authored and Phil Sturgeon committed Aug 22, 2019
1 parent 6e0004a commit b0a4342
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getLocationForJsonPath, parseWithPointers } from '@stoplight/json';
import { Resolver } from '@stoplight/json-ref-resolver';
import { DiagnosticSeverity } from '@stoplight/types';
import { parse } from '@stoplight/yaml';
import { readRulesFromRulesets } from '../rulesets';
import { isOpenApiv2, isOpenApiv3 } from '../rulesets/lookups';
import { mergeRulesets } from '../rulesets/merger';
Expand Down Expand Up @@ -901,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' })]);
});
});
});
2 changes: 1 addition & 1 deletion src/resolved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IParsedResult } from './types';

export class Resolved implements IResolveResult {
public refMap: Dictionary<string>;
public result: unknown;
public result: IResolveResult;
public errors: IResolveError[];
public runner: IResolveRunner;
public format?: string | null;
Expand Down
17 changes: 15 additions & 2 deletions src/spectral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
IParsedResult,
IRuleResult,
IRunOpts,
ISpectralFullResult,
PartialRuleCollection,
RegisteredFormats,
RuleCollection,
Expand All @@ -49,7 +50,10 @@ export class Spectral {
this.formats = {};
}

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

let parsedResult: IParsedResult | IParsedResult<YamlParserResult<unknown>>;
Expand Down Expand Up @@ -119,12 +123,21 @@ export class Spectral {
resolved.format = foundFormat === void 0 ? null : foundFormat;
}

return [
const validationResults = [
...refDiagnostics,
...results,
...formatResolverErrors(resolved),
...runRules(resolved, this.rules, this.functions),
];

return {
resolved: resolved.result,
results: validationResults,
};
}

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

/**
Expand Down
6 changes: 6 additions & 0 deletions src/types/spectral.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Resolver } from '@stoplight/json-ref-resolver';
import { IResolveResult } from '@stoplight/json-ref-resolver/types';
import {
DiagnosticSeverity,
Dictionary,
Expand Down Expand Up @@ -42,6 +43,11 @@ export interface IRuleResult extends IDiagnostic {
path: JsonPath;
}

export interface ISpectralFullResult {
resolved: IResolveResult;
results: IRuleResult[];
}

export interface IGivenNode {
path: JsonPath;
value: any;
Expand Down

0 comments on commit b0a4342

Please sign in to comment.