-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(json-schema-parser): add getJsonSchemaBaseUri
- Loading branch information
1 parent
2a2fafc
commit ebb0b66
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...parser/json-schema-parser/src/jsonSchema/202012/calculations/getJsonSchemaBaseUri.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
...ages/parser/json-schema-parser/src/jsonSchema/202012/calculations/getJsonSchemaBaseUri.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |