Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize documentUri #1185

Merged
merged 2 commits into from May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/document.ts
Expand Up @@ -17,10 +17,13 @@ export interface IDocument<D = unknown> {
data: D;
}

const normalizeSource = (source: Optional<string>): string | null => {
export function normalizeSource(source: undefined): null;
export function normalizeSource(source: string): string;
export function normalizeSource(source: Optional<string>): string | null;
export function 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> {
protected readonly parserResult: R;
Expand Down
4 changes: 2 additions & 2 deletions src/spectral.ts
Expand Up @@ -5,7 +5,7 @@ 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 @@ -80,7 +80,7 @@ export class Spectral {
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;
(document as Omit<Document, 'source'> & { source: string }).source = normalizeSource(opts.resolve.documentUri);
}

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