Skip to content

Commit

Permalink
refactor(json-schema-parser): update typings with root schema ones
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Sep 10, 2023
1 parent 66b397a commit 39a6254
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import {
JsonRootSchema,
JsonSchema,
} from '@cuaklabs/json-schema-types/2020-12';
import { Uri } from '@cuaklabs/uri';

import { traverseJsonSchema } from '../actions/traverseJsonSchema';
Expand All @@ -10,8 +13,8 @@ import { getJsonSchemaBaseUri } from './getJsonSchemaBaseUri';

export async function dereferenceJsonSchema(
deref: DereferenceFunction,
schema: JsonSchema,
referenceMap: Map<string, JsonSchema>,
schema: JsonRootSchema | JsonSchema,
referenceMap: Map<string, JsonRootSchema | JsonSchema>,
uriOptions: UriOptions | undefined,
): Promise<void> {
const baseUri: string = getJsonSchemaBaseUri(schema, uriOptions);
Expand Down Expand Up @@ -42,7 +45,10 @@ export async function dereferenceJsonSchema(
);
}

function getSchemaUris(schema: JsonSchema, baseUri: string): string[] {
function getSchemaUris(
schema: JsonRootSchema | JsonSchema,
baseUri: string,
): string[] {
const schemaUris: string[] = [];

traverseJsonSchema(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import {
JsonRootSchema,
JsonSchema,
} from '@cuaklabs/json-schema-types/2020-12';
import { getBaseUri } from '@cuaklabs/uri';

import { UriOptions } from '../models/UriOptions';

export function getJsonSchemaBaseUri(
schema: JsonSchema,
schema: JsonRootSchema | JsonSchema,
options?: UriOptions,
): string {
const documentBaseUri: string | undefined =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import {
JsonRootSchema,
JsonSchema,
} from '@cuaklabs/json-schema-types/2020-12';

import { DereferenceFunction } from '../models/DereferenceFunction';
import { JsonSchemaParseResult } from '../models/JsonSchemaParseResult';
Expand All @@ -8,7 +11,7 @@ import { dereferenceJsonSchema } from './dereferenceJsonSchema';

export async function parse(
deref: DereferenceFunction,
schemas: JsonSchema[],
schemas: (JsonRootSchema | JsonSchema)[],
options: UriOptions,
): Promise<JsonSchemaParseResult>;
export async function parse(
Expand All @@ -17,13 +20,13 @@ export async function parse(
): Promise<JsonSchemaParseResult>;
export async function parse(
...args:
| [DereferenceFunction, JsonSchema[], UriOptions]
| [DereferenceFunction, (JsonRootSchema | JsonSchema)[], UriOptions]
| [DereferenceFunction, ParseJsonSchemaOptions[]]
): Promise<JsonSchemaParseResult> {
const [deref, schemasOrOptions, optionsOrUndefined]:
| [DereferenceFunction, JsonSchema[], UriOptions]
| [DereferenceFunction, (JsonRootSchema | JsonSchema)[], UriOptions]
| [DereferenceFunction, ParseJsonSchemaOptions[], undefined] = args as
| [DereferenceFunction, JsonSchema[], UriOptions]
| [DereferenceFunction, (JsonRootSchema | JsonSchema)[], UriOptions]
| [DereferenceFunction, ParseJsonSchemaOptions[], undefined];

if (optionsOrUndefined === undefined) {
Expand All @@ -35,14 +38,14 @@ export async function parse(

async function parseFromSingleOptions(
deref: DereferenceFunction,
schemas: JsonSchema[],
schemas: (JsonRootSchema | JsonSchema)[],
options: UriOptions,
): Promise<JsonSchemaParseResult> {
const referenceMap: Map<string, JsonSchema> = new Map();
const referenceMap: Map<string, JsonRootSchema | JsonSchema> = new Map();

await Promise.all(
schemas.map(
async (schema: JsonSchema): Promise<void> =>
async (schema: JsonRootSchema | JsonSchema): Promise<void> =>
dereferenceJsonSchema(deref, schema, referenceMap, options),
),
);
Expand All @@ -57,7 +60,7 @@ async function parseFromMultipleOptions(
deref: DereferenceFunction,
options: ParseJsonSchemaOptions[],
): Promise<JsonSchemaParseResult> {
const referenceMap: Map<string, JsonSchema> = new Map();
const referenceMap: Map<string, JsonRootSchema | JsonSchema> = new Map();

await Promise.all(
options.map(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import {
JsonRootSchema,
JsonSchema,
} from '@cuaklabs/json-schema-types/2020-12';

import { DereferencedSchemaResult } from './DereferencedSchemaResult';

export type DereferenceFunction = (
schema: JsonSchema,
schema: JsonRootSchema | JsonSchema,
baseUri: string,
uri: string,
) => Promise<DereferencedSchemaResult>;
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import {
JsonRootSchema,
JsonSchema,
} from '@cuaklabs/json-schema-types/2020-12';

import { UriOptions } from '..';
import { UriOptions } from '../models/UriOptions';

export interface DereferencedSchemaResult {
schema: JsonSchema;
schema: JsonRootSchema | JsonSchema;
uriOptions: UriOptions;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import {
JsonRootSchema,
JsonSchema,
} from '@cuaklabs/json-schema-types/2020-12';

export interface JsonSchemaParseResult {
schemas: JsonSchema[];
referenceMap: Map<string, JsonSchema>;
referenceMap: Map<string, JsonRootSchema | JsonSchema>;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import {
JsonRootSchema,
JsonSchema,
} from '@cuaklabs/json-schema-types/2020-12';

import { UriOptions } from './UriOptions';

export interface ParseJsonSchemaOptions {
schema: JsonSchema;
schema: JsonRootSchema | JsonSchema;
uriOptions: UriOptions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import {
export interface TraverseJsonSchemaCallbackParams {
jsonPointer: string;
parentJsonPointer: string | undefined;
parentSchema: JsonSchema | undefined;
schema: JsonSchema | JsonRootSchema;
parentSchema: JsonRootSchema | JsonSchema | undefined;
schema: JsonRootSchema | JsonSchema;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import {
JsonRootSchema,
JsonSchema,
} from '@cuaklabs/json-schema-types/2020-12';

export interface TraverseJsonSchemaParams {
jsonPointer?: string;
schema: JsonSchema;
schema: JsonRootSchema | JsonSchema;
}

0 comments on commit 39a6254

Please sign in to comment.