Skip to content

Commit

Permalink
refactor(json-schema-parser): add getJsonSchemaBaseUri
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Sep 6, 2023
1 parent 2a2fafc commit ebb0b66
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals';

jest.mock('@cuaklabs/uri');

import { JsonSchemaObject } from '@cuaklabs/json-schema-types/2020-12';
import { getBaseUri, GetBaseUriOptions } from '@cuaklabs/uri';

import { JsonRootSchema202012Fixtures } from '../fixtures/JsonRootSchema202012Fixtures';
import { getJsonSchemaBaseUri } from './getJsonSchemaBaseUri';

describe(getJsonSchemaBaseUri.name, () => {
describe('having a JsonSchema with $id', () => {
let jsonSchemaFixture: JsonSchemaObject;

beforeAll(() => {
jsonSchemaFixture = JsonRootSchema202012Fixtures.withId;
});

describe('when called', () => {
let uriFixture: string;

let result: unknown;

beforeAll(() => {
uriFixture = 'uri:fixture';

(getBaseUri as jest.Mock<typeof getBaseUri>).mockReturnValueOnce(
uriFixture,
);

result = getJsonSchemaBaseUri(jsonSchemaFixture);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call getBaseUri()', () => {
const expected: GetBaseUriOptions = {
documentBaseUri: jsonSchemaFixture.$id,
};

expect(getBaseUri).toHaveBeenCalledTimes(1);
expect(getBaseUri).toHaveBeenCalledWith(expected);
});

it('should return an Uri()', () => {
expect(result).toBe(uriFixture);
});
});
});

describe('having a JsonSchema with no id', () => {
let jsonSchemaFixture: JsonSchemaObject;

beforeAll(() => {
jsonSchemaFixture = JsonRootSchema202012Fixtures.withNoId;
});

describe('when called', () => {
let uriFixture: string;

let result: unknown;

beforeAll(() => {
uriFixture = 'uri:fixture';

(getBaseUri as jest.Mock<typeof getBaseUri>).mockReturnValueOnce(
uriFixture,
);

result = getJsonSchemaBaseUri(jsonSchemaFixture);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call getBaseUri()', () => {
const expected: GetBaseUriOptions = {
documentBaseUri: undefined,
};

expect(getBaseUri).toHaveBeenCalledTimes(1);
expect(getBaseUri).toHaveBeenCalledWith(expected);
});

it('should return an Uri()', () => {
expect(result).toBe(uriFixture);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import { getBaseUri } from '@cuaklabs/uri';

export interface GetJsonSchemaBaseUriOptions {
encapsulatingDocumentBaseUri?: string | undefined;
retrievalUri?: string | undefined;
}

export function getJsonSchemaBaseUri(
schema: JsonSchema,
options?: GetJsonSchemaBaseUriOptions,
): string {
const documentBaseUri: string | undefined =
typeof schema === 'boolean' ? undefined : schema.$id;

return getBaseUri({
...(options ?? {}),
documentBaseUri,
});
}

0 comments on commit ebb0b66

Please sign in to comment.