Skip to content

Commit

Permalink
Merge pull request #25 from cuaklabs/feat/update-deref-return-type
Browse files Browse the repository at this point in the history
Update dereferenceJsonSchema to return DereferencedSchemaResult
  • Loading branch information
notaphplover authored Sep 10, 2023
2 parents e73d1de + a78837f commit f266fb4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/parser/json-schema-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Added `parse`.
- Added `DereferencedSchemaResult`.
- Added `DereferenceFunction`
- Added `ParseJsonSchemaOptions`
- Added `UriOptions`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe(dereferenceJsonSchema.name, () => {

describe('when called, and traverseJsonSchema() calls callback twice with an schema with a reference', () => {
let dereferencedSchemaFixture: JsonSchema;
let dereferencedUriOptionsFixture: UriOptions;
let subSchemaFixture: JsonSchemaObject;
let baseUriFixture: string;
let referenceMapFixture: Map<string, JsonSchema>;
Expand All @@ -93,6 +94,9 @@ describe(dereferenceJsonSchema.name, () => {

beforeAll(async () => {
dereferencedSchemaFixture = JsonRootSchemaFixtures.any;
dereferencedUriOptionsFixture = {
retrievalUri: 'dereferenced://uri/options',
};
subSchemaFixture = JsonRootSchemaFixtures.withRef;
baseUriFixture = 'base://fixture';
referenceMapFixture = new Map();
Expand Down Expand Up @@ -123,7 +127,10 @@ describe(dereferenceJsonSchema.name, () => {
)
.mockImplementationOnce(() => undefined);

derefMock.mockResolvedValueOnce(dereferencedSchemaFixture);
derefMock.mockResolvedValueOnce({
schema: dereferencedSchemaFixture,
uriOptions: dereferencedUriOptionsFixture,
});

result = await dereferenceJsonSchema(
derefMock,
Expand All @@ -147,7 +154,7 @@ describe(dereferenceJsonSchema.name, () => {
expect(getJsonSchemaBaseUri).toHaveBeenNthCalledWith(
2,
dereferencedSchemaFixture,
uriOptionsFixture,
dereferencedUriOptionsFixture,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import { Uri } from '@cuaklabs/uri';

import { traverseJsonSchema } from '../actions/traverseJsonSchema';
import { DereferencedSchemaResult } from '../models/DereferencedSchemaResult';
import { DereferenceFunction } from '../models/DereferenceFunction';
import { TraverseJsonSchemaCallbackParams } from '../models/TraverseJsonSchemaCallbackParams';
import { UriOptions } from '../models/UriOptions';
Expand All @@ -23,19 +24,19 @@ export async function dereferenceJsonSchema(

await Promise.all(
missingSchemaUris.map(async (schemaUri: string): Promise<void> => {
const dereferencedSchema: JsonSchema = await deref(
const dereferencedSchemaResult: DereferencedSchemaResult = await deref(
schema,
baseUri,
schemaUri,
);

referenceMap.set(schemaUri, dereferencedSchema);
referenceMap.set(schemaUri, dereferencedSchemaResult.schema);

await dereferenceJsonSchema(
deref,
dereferencedSchema,
dereferencedSchemaResult.schema,
referenceMap,
uriOptions,
dereferencedSchemaResult.uriOptions,
);
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { parse } from './calculations/parse';
import { DereferencedSchemaResult } from './models/DereferencedSchemaResult';
import { DereferenceFunction } from './models/DereferenceFunction';
import { JsonSchemaParseResult } from './models/JsonSchemaParseResult';
import { ParseJsonSchemaOptions } from './models/ParseJsonSchemaOptions';
import { UriOptions } from './models/UriOptions';

export type { DereferenceFunction, ParseJsonSchemaOptions, UriOptions };
export type {
DereferencedSchemaResult,
DereferenceFunction,
JsonSchemaParseResult,
ParseJsonSchemaOptions,
UriOptions,
};

export { parse };
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';

import { DereferencedSchemaResult } from './DereferencedSchemaResult';

export type DereferenceFunction = (
schema: JsonSchema,
baseUri: string,
uri: string,
) => Promise<JsonSchema>;
) => Promise<DereferencedSchemaResult>;
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 '..';

export interface DereferencedSchemaResult {
schema: JsonSchema;
uriOptions: UriOptions;
}

0 comments on commit f266fb4

Please sign in to comment.