From 4e0386ccc35fd8fe2a1d38cb48ba3381d0dc32d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Thu, 28 May 2020 15:33:57 +0200 Subject: [PATCH] fix: alternative way --- src/document.ts | 4 ++-- src/spectral.ts | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/document.ts b/src/document.ts index e1dd8c402b..a550b69e62 100644 --- a/src/document.ts +++ b/src/document.ts @@ -17,9 +17,9 @@ export interface IDocument { data: D; } -const normalizeSource = (source: Optional): string | null => { +export const normalizeSource = (source: Optional): 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> implements IDocument { diff --git a/src/spectral.ts b/src/spectral.ts index b5e8b868b6..9d1ab2e1d5 100644 --- a/src/spectral.ts +++ b/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'; @@ -62,10 +62,7 @@ export class Spectral { Object.assign(STATIC_ASSETS, assets); } - protected parseDocument( - target: IParsedResult | IDocument | object | string, - documentUri: Optional, - ): IDocument { + protected parseDocument(target: IParsedResult | IDocument | object | string): IDocument { return target instanceof Document ? target : isParsedResult(target) @@ -73,7 +70,6 @@ export class Spectral { : new Document>( typeof target === 'string' ? target : safeStringify(target, undefined, 2), Parsers.Yaml, - documentUri, ); } @@ -81,10 +77,13 @@ export class Spectral { target: IParsedResult | IDocument | object | string, opts: IRunOpts = {}, ): Promise { - 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 & { source: string }).source = opts.resolve?.documentUri; + const normalizedSource = normalizeSource(opts.resolve.documentUri); + if (normalizedSource !== null) { + (document as Omit & { source: string }).source = normalizedSource; + } } const inventory = new DocumentInventory(document, this._resolver);