Skip to content

Commit

Permalink
fix: alternative way
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed May 28, 2020
1 parent 3bc5d7c commit d22fae3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/document.ts
Expand Up @@ -17,9 +17,9 @@ export interface IDocument<D = unknown> {
data: D;
}

const normalizeSource = (source: Optional<string>): string | null => {
export const normalizeSource = (source: Optional<string>): string | null => {
if (source === void 0) return null;
return source && !startsWithProtocol(source) ? normalize(source) : source;
return source.length > 0 && !startsWithProtocol(source) ? normalize(source) : source;
};

export class Document<D = unknown, R extends IParserResult = IParserResult<D>> implements IDocument<D> {
Expand Down
17 changes: 8 additions & 9 deletions src/spectral.ts
@@ -1,11 +1,11 @@
import { safeStringify } from '@stoplight/json';
import { Resolver } from '@stoplight/json-ref-resolver';
import { DiagnosticSeverity, Dictionary, Optional } from '@stoplight/types';
import { DiagnosticSeverity, Dictionary } from '@stoplight/types';
import { YamlParserResult } from '@stoplight/yaml';
import { memoize, merge } from 'lodash';

import { STATIC_ASSETS } from './assets';
import { Document, IDocument, IParsedResult, isParsedResult, ParsedDocument } from './document';
import { Document, IDocument, IParsedResult, isParsedResult, ParsedDocument, normalizeSource } from './document';
import { DocumentInventory } from './documentInventory';
import { CoreFunctions, functions as coreFunctions } from './functions';
import * as Parsers from './parsers';
Expand Down Expand Up @@ -62,29 +62,28 @@ export class Spectral {
Object.assign(STATIC_ASSETS, assets);
}

protected parseDocument(
target: IParsedResult | IDocument | object | string,
documentUri: Optional<string>,
): IDocument {
protected parseDocument(target: IParsedResult | IDocument | object | string): IDocument {
return target instanceof Document
? target
: isParsedResult(target)
? new ParsedDocument(target)
: new Document<unknown, YamlParserResult<unknown>>(
typeof target === 'string' ? target : safeStringify(target, undefined, 2),
Parsers.Yaml,
documentUri,
);
}

public async runWithResolved(
target: IParsedResult | IDocument | object | string,
opts: IRunOpts = {},
): Promise<ISpectralFullResult> {
const document = this.parseDocument(target, opts.resolve?.documentUri);
const document = this.parseDocument(target);

if (document.source === null && opts.resolve?.documentUri !== void 0) {
(document as Omit<Document, 'source'> & { source: string }).source = opts.resolve?.documentUri;
const normalizedSource = normalizeSource(opts.resolve?.documentUri);
if (normalizedSource !== null) {
(document as Omit<Document, 'source'> & { source: string }).source = normalizedSource;
}
}

const inventory = new DocumentInventory(document, this._resolver);
Expand Down

0 comments on commit d22fae3

Please sign in to comment.