Skip to content

Commit e7e599b

Browse files
committed
adress pr comments
1 parent 0596dc8 commit e7e599b

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

src/schema-accessor.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,24 @@ export class InternalSchemaBasedAccessor implements SchemaAccessor {
3333
return this.internalSchema;
3434
}
3535

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

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

51+
/**
52+
* Get expanded JSON Schema - with additional properties
53+
*/
4454
async getExpandedJSONSchema(options: Options = {}): Promise<ExpandedJSONSchema> {
4555
return this.ExpandedJSONSchema ??= await convertors.internalSchemaToExpanded(this.internalSchema, options);
4656
}

src/schema-convertors/internalToMongoDB.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
/**
2+
* Transforms the internal schema to $jsonSchema
3+
*/
14
import { ArraySchemaType, DocumentSchemaType, Schema as InternalSchema, SchemaType } from '../schema-analyzer';
25
import { MongoDBJSONSchema } from '../types';
6+
import { allowAbort } from './util';
37

4-
const InternalTypeToBsonTypeMap: Record<
8+
export const InternalTypeToBsonTypeMap: Record<
59
SchemaType['name'] | 'Double' | 'BSONSymbol',
610
string
711
> = {
@@ -36,15 +40,6 @@ const convertInternalType = (type: string) => {
3640
return bsonType;
3741
};
3842

39-
async function allowAbort(signal?: AbortSignal) {
40-
return new Promise<void>((resolve, reject) =>
41-
setTimeout(() => {
42-
if (signal?.aborted) return reject(signal?.reason || new Error('Operation aborted'));
43-
resolve();
44-
})
45-
);
46-
}
47-
4843
async function parseType(type: SchemaType, signal?: AbortSignal): Promise<MongoDBJSONSchema> {
4944
await allowAbort(signal);
5045
const schema: MongoDBJSONSchema = {

src/schema-convertors/internalToStandard.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert';
2-
import Ajv2020 from 'ajv';
2+
import Ajv2020 from 'ajv/dist/2020';
33
import internalSchemaToStandard, { RELAXED_EJSON_DEFINITIONS } from './internalToStandard';
44

55
describe('internalSchemaToStandard', async function() {

src/schema-convertors/internalToStandard.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { JSONSchema4TypeName } from 'json-schema';
22
import { ArraySchemaType, DocumentSchemaType, Schema as InternalSchema, SchemaType } from '../schema-analyzer';
33
import { StandardJSONSchema } from '../types';
4+
import { allowAbort } from './util';
45

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

@@ -246,15 +247,6 @@ const convertInternalType = (internalType: string) => {
246247
return type;
247248
};
248249

249-
async function allowAbort(signal?: AbortSignal) {
250-
return new Promise<void>((resolve, reject) =>
251-
setTimeout(() => {
252-
if (signal?.aborted) return reject(signal?.reason || new Error('Operation aborted'));
253-
resolve();
254-
})
255-
);
256-
}
257-
258250
async function parseType(type: SchemaType, signal?: AbortSignal): Promise<StandardJSONSchema> {
259251
await allowAbort(signal);
260252
const schema: StandardJSONSchema = convertInternalType(type.bsonType);

src/schema-convertors/util.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export async function allowAbort(signal?: AbortSignal) {
2+
return new Promise<void>((resolve, reject) =>
3+
setTimeout(() => {
4+
if (signal?.aborted) return reject(signal?.reason || new Error('Operation aborted'));
5+
resolve();
6+
})
7+
);
8+
}

0 commit comments

Comments
 (0)