-
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.
- Loading branch information
1 parent
5765ccb
commit 467fc85
Showing
8 changed files
with
231 additions
and
1 deletion.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
packages/parser/json-schema-parser/src/jsonSchema/202012/calculations/parse.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,115 @@ | ||
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'; | ||
|
||
jest.mock('./dereferenceJsonSchema'); | ||
|
||
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12'; | ||
|
||
import { JsonRootSchemaFixtures } from '../fixtures/JsonRootSchemaFixtures'; | ||
import { DereferenceFunction } from '../models/DereferenceFunction'; | ||
import { JsonSchemaParseResult } from '../models/JsonSchemaParseResult'; | ||
import { UriOptions } from '../models/UriOptions'; | ||
import { dereferenceJsonSchema } from './dereferenceJsonSchema'; | ||
import { parse } from './parse'; | ||
|
||
describe(parse.name, () => { | ||
let derefMock: jest.Mock<DereferenceFunction>; | ||
|
||
describe('having an array of schemas and uri options', () => { | ||
let jsonSchemaFixture: JsonSchema; | ||
let uriOptionsFixture: UriOptions; | ||
|
||
beforeAll(() => { | ||
jsonSchemaFixture = JsonRootSchemaFixtures.any; | ||
uriOptionsFixture = { | ||
encapsulatingDocumentBaseUri: 'sample://uri', | ||
}; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
( | ||
dereferenceJsonSchema as jest.Mock<typeof dereferenceJsonSchema> | ||
).mockResolvedValueOnce(undefined); | ||
|
||
result = await parse(derefMock, [jsonSchemaFixture], uriOptionsFixture); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call dereferenceJsonSchema()', () => { | ||
expect(dereferenceJsonSchema).toHaveBeenCalledTimes(1); | ||
expect(dereferenceJsonSchema).toHaveBeenCalledWith( | ||
derefMock, | ||
jsonSchemaFixture, | ||
new Map(), | ||
uriOptionsFixture, | ||
); | ||
}); | ||
|
||
it('should return JsonSchemaParseResult', () => { | ||
const expected: JsonSchemaParseResult = { | ||
referenceMap: new Map(), | ||
schemas: [jsonSchemaFixture], | ||
}; | ||
|
||
expect(result).toStrictEqual(expected); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('having an array of ParseJsonSchemaOptions', () => { | ||
let jsonSchemaFixture: JsonSchema; | ||
let uriOptionsFixture: UriOptions; | ||
|
||
beforeAll(() => { | ||
jsonSchemaFixture = JsonRootSchemaFixtures.any; | ||
uriOptionsFixture = { | ||
encapsulatingDocumentBaseUri: 'sample://uri', | ||
}; | ||
}); | ||
|
||
describe('when called', () => { | ||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
( | ||
dereferenceJsonSchema as jest.Mock<typeof dereferenceJsonSchema> | ||
).mockResolvedValueOnce(undefined); | ||
|
||
result = await parse(derefMock, [ | ||
{ | ||
schema: jsonSchemaFixture, | ||
uriOptions: uriOptionsFixture, | ||
}, | ||
]); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call dereferenceJsonSchema()', () => { | ||
expect(dereferenceJsonSchema).toHaveBeenCalledTimes(1); | ||
expect(dereferenceJsonSchema).toHaveBeenCalledWith( | ||
derefMock, | ||
jsonSchemaFixture, | ||
new Map(), | ||
uriOptionsFixture, | ||
); | ||
}); | ||
|
||
it('should return JsonSchemaParseResult', () => { | ||
const expected: JsonSchemaParseResult = { | ||
referenceMap: new Map(), | ||
schemas: [jsonSchemaFixture], | ||
}; | ||
|
||
expect(result).toStrictEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
packages/parser/json-schema-parser/src/jsonSchema/202012/calculations/parse.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,80 @@ | ||
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12'; | ||
|
||
import { DereferenceFunction } from '../models/DereferenceFunction'; | ||
import { JsonSchemaParseResult } from '../models/JsonSchemaParseResult'; | ||
import { ParseJsonSchemaOptions } from '../models/ParseJsonSchemaOptions'; | ||
import { UriOptions } from '../models/UriOptions'; | ||
import { dereferenceJsonSchema } from './dereferenceJsonSchema'; | ||
|
||
export async function parse( | ||
deref: DereferenceFunction, | ||
schemas: JsonSchema[], | ||
options: UriOptions, | ||
): Promise<JsonSchemaParseResult>; | ||
export async function parse( | ||
deref: DereferenceFunction, | ||
options: ParseJsonSchemaOptions[], | ||
): Promise<JsonSchemaParseResult>; | ||
export async function parse( | ||
...args: | ||
| [DereferenceFunction, JsonSchema[], UriOptions] | ||
| [DereferenceFunction, ParseJsonSchemaOptions[]] | ||
): Promise<JsonSchemaParseResult> { | ||
const [deref, schemasOrOptions, optionsOrUndefined]: | ||
| [DereferenceFunction, JsonSchema[], UriOptions] | ||
| [DereferenceFunction, ParseJsonSchemaOptions[], undefined] = args as | ||
| [DereferenceFunction, JsonSchema[], UriOptions] | ||
| [DereferenceFunction, ParseJsonSchemaOptions[], undefined]; | ||
|
||
if (optionsOrUndefined === undefined) { | ||
return parseFromMultipleOptions(deref, schemasOrOptions); | ||
} else { | ||
return parseFromSingleOptions(deref, schemasOrOptions, optionsOrUndefined); | ||
} | ||
} | ||
|
||
async function parseFromSingleOptions( | ||
deref: DereferenceFunction, | ||
schemas: JsonSchema[], | ||
options: UriOptions, | ||
): Promise<JsonSchemaParseResult> { | ||
const referenceMap: Map<string, JsonSchema> = new Map(); | ||
|
||
await Promise.all( | ||
schemas.map( | ||
async (schema: JsonSchema): Promise<void> => | ||
dereferenceJsonSchema(deref, schema, referenceMap, options), | ||
), | ||
); | ||
|
||
return { | ||
referenceMap, | ||
schemas, | ||
}; | ||
} | ||
|
||
async function parseFromMultipleOptions( | ||
deref: DereferenceFunction, | ||
options: ParseJsonSchemaOptions[], | ||
): Promise<JsonSchemaParseResult> { | ||
const referenceMap: Map<string, JsonSchema> = new Map(); | ||
|
||
await Promise.all( | ||
options.map( | ||
async (schemaOptions: ParseJsonSchemaOptions): Promise<void> => | ||
dereferenceJsonSchema( | ||
deref, | ||
schemaOptions.schema, | ||
referenceMap, | ||
schemaOptions.uriOptions, | ||
), | ||
), | ||
); | ||
|
||
return { | ||
referenceMap, | ||
schemas: options.map( | ||
(schemaOptions: ParseJsonSchemaOptions) => schemaOptions.schema, | ||
), | ||
}; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/parser/json-schema-parser/src/jsonSchema/202012/index.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,8 @@ | ||
import { parse } from './calculations/parse'; | ||
import { DereferenceFunction } from './models/DereferenceFunction'; | ||
import { ParseJsonSchemaOptions } from './models/ParseJsonSchemaOptions'; | ||
import { UriOptions } from './models/UriOptions'; | ||
|
||
export type { DereferenceFunction, ParseJsonSchemaOptions, UriOptions }; | ||
|
||
export { parse }; |
6 changes: 6 additions & 0 deletions
6
packages/parser/json-schema-parser/src/jsonSchema/202012/models/JsonSchemaParseResult.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,6 @@ | ||
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12'; | ||
|
||
export interface JsonSchemaParseResult { | ||
schemas: JsonSchema[]; | ||
referenceMap: Map<string, JsonSchema>; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/parser/json-schema-parser/src/jsonSchema/202012/models/ParseJsonSchemaOptions.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,8 @@ | ||
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12'; | ||
|
||
import { UriOptions } from './UriOptions'; | ||
|
||
export interface ParseJsonSchemaOptions { | ||
schema: JsonSchema; | ||
uriOptions: UriOptions; | ||
} |