Skip to content

Commit

Permalink
adress pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paula-stacho committed Feb 4, 2025
1 parent 0596dc8 commit e7e599b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/schema-accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,24 @@ export class InternalSchemaBasedAccessor implements SchemaAccessor {
return this.internalSchema;
}

/**
* Get standard JSON Schema - as per
* https://json-schema.org/draft/2020-12/schema
*/
async getStandardJsonSchema(options: Options = {}): Promise<StandardJSONSchema> {
return this.standardJSONSchema ??= await convertors.internalSchemaToStandard(this.internalSchema, options);
}

/**
* Get MongoDB's $jsonSchema
*/
async getMongoDBJsonSchema(options: Options = {}): Promise<MongoDBJSONSchema> {
return this.mongodbJSONSchema ??= await convertors.internalSchemaToMongoDB(this.internalSchema, options);
}

/**
* Get expanded JSON Schema - with additional properties
*/
async getExpandedJSONSchema(options: Options = {}): Promise<ExpandedJSONSchema> {
return this.ExpandedJSONSchema ??= await convertors.internalSchemaToExpanded(this.internalSchema, options);
}
Expand Down
15 changes: 5 additions & 10 deletions src/schema-convertors/internalToMongoDB.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/**
* Transforms the internal schema to $jsonSchema
*/
import { ArraySchemaType, DocumentSchemaType, Schema as InternalSchema, SchemaType } from '../schema-analyzer';
import { MongoDBJSONSchema } from '../types';
import { allowAbort } from './util';

const InternalTypeToBsonTypeMap: Record<
export const InternalTypeToBsonTypeMap: Record<
SchemaType['name'] | 'Double' | 'BSONSymbol',
string
> = {
Expand Down Expand Up @@ -36,15 +40,6 @@ const convertInternalType = (type: string) => {
return bsonType;
};

async function allowAbort(signal?: AbortSignal) {
return new Promise<void>((resolve, reject) =>
setTimeout(() => {
if (signal?.aborted) return reject(signal?.reason || new Error('Operation aborted'));
resolve();
})
);
}

async function parseType(type: SchemaType, signal?: AbortSignal): Promise<MongoDBJSONSchema> {
await allowAbort(signal);
const schema: MongoDBJSONSchema = {
Expand Down
2 changes: 1 addition & 1 deletion src/schema-convertors/internalToStandard.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import Ajv2020 from 'ajv';
import Ajv2020 from 'ajv/dist/2020';
import internalSchemaToStandard, { RELAXED_EJSON_DEFINITIONS } from './internalToStandard';

describe('internalSchemaToStandard', async function() {
Expand Down
10 changes: 1 addition & 9 deletions src/schema-convertors/internalToStandard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JSONSchema4TypeName } from 'json-schema';
import { ArraySchemaType, DocumentSchemaType, Schema as InternalSchema, SchemaType } from '../schema-analyzer';
import { StandardJSONSchema } from '../types';
import { allowAbort } from './util';

type StandardTypeDefinition = { type: JSONSchema4TypeName, $ref?: never; } | { $ref: string, type?: never };

Expand Down Expand Up @@ -246,15 +247,6 @@ const convertInternalType = (internalType: string) => {
return type;
};

async function allowAbort(signal?: AbortSignal) {
return new Promise<void>((resolve, reject) =>
setTimeout(() => {
if (signal?.aborted) return reject(signal?.reason || new Error('Operation aborted'));
resolve();
})
);
}

async function parseType(type: SchemaType, signal?: AbortSignal): Promise<StandardJSONSchema> {
await allowAbort(signal);
const schema: StandardJSONSchema = convertInternalType(type.bsonType);
Expand Down
8 changes: 8 additions & 0 deletions src/schema-convertors/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export async function allowAbort(signal?: AbortSignal) {
return new Promise<void>((resolve, reject) =>
setTimeout(() => {
if (signal?.aborted) return reject(signal?.reason || new Error('Operation aborted'));
resolve();
})
);
}

0 comments on commit e7e599b

Please sign in to comment.