Skip to content

fix: limit definitions based on use COMPASS-8941 #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/schema-accessor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Schema as InternalSchema } from './schema-analyzer';
import { convertors } from './schema-convertors';
import InternalToExpandedConvertor from './schema-convertors/internalToExpanded';
import InternalToMongoDBConvertor from './schema-convertors/internalToMongoDB';
import InternalToStandardConvertor from './schema-convertors/internalToStandard';
import { ExpandedJSONSchema, MongoDBJSONSchema, StandardJSONSchema } from './types';

export interface SchemaAccessor {
Expand All @@ -24,9 +26,15 @@ export class InternalSchemaBasedAccessor implements SchemaAccessor {
private standardJSONSchema?: StandardJSONSchema;
private mongodbJSONSchema?: MongoDBJSONSchema;
private ExpandedJSONSchema?: ExpandedJSONSchema;
public internalToStandardConvertor: InternalToStandardConvertor;
public internalToExpandedConvertor: InternalToExpandedConvertor;
public internalToMongoDBConvertor: InternalToMongoDBConvertor;

constructor(internalSchema: InternalSchema) {
this.internalSchema = internalSchema;
this.internalToStandardConvertor = new InternalToStandardConvertor();
this.internalToExpandedConvertor = new InternalToExpandedConvertor();
this.internalToMongoDBConvertor = new InternalToMongoDBConvertor();
}

async getInternalSchema(): Promise<InternalSchema> {
Expand All @@ -38,20 +46,20 @@ export class InternalSchemaBasedAccessor implements SchemaAccessor {
* https://json-schema.org/draft/2020-12/schema
*/
async getStandardJsonSchema(options: Options = {}): Promise<StandardJSONSchema> {
return this.standardJSONSchema ??= await convertors.internalSchemaToStandard(this.internalSchema, options);
return this.standardJSONSchema ??= await this.internalToStandardConvertor.convert(this.internalSchema, options);
}

/**
* Get MongoDB's $jsonSchema
*/
async getMongoDBJsonSchema(options: Options = {}): Promise<MongoDBJSONSchema> {
return this.mongodbJSONSchema ??= await convertors.internalSchemaToMongoDB(this.internalSchema, options);
return this.mongodbJSONSchema ??= await this.internalToMongoDBConvertor.convert(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);
return this.ExpandedJSONSchema ??= await this.internalToExpandedConvertor.convert(this.internalSchema, options);
}
}
9 changes: 0 additions & 9 deletions src/schema-convertors/index.ts

This file was deleted.

75 changes: 49 additions & 26 deletions src/schema-convertors/internalToExpanded.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import internalSchemaToExpanded from './internalToExpanded';
import { RELAXED_EJSON_DEFINITIONS } from './internalToStandard';
import InternalToExpandedConvertor from './internalToExpanded';

describe('internalSchemaToExpanded', async function() {
describe('Converts: ', async function() {
Expand Down Expand Up @@ -336,12 +336,21 @@ describe('internalSchemaToExpanded', async function() {
}
]
};
const standard = await internalSchemaToExpanded(internal);
assert.deepStrictEqual(standard, {
const convertor = new InternalToExpandedConvertor();
const expanded = await convertor.convert(internal);
const expectedDefinitions: any = RELAXED_EJSON_DEFINITIONS;
delete expectedDefinitions.BSONSymbol;
delete expectedDefinitions.CodeWScope;
delete expectedDefinitions.DBPointer;
delete expectedDefinitions.DBRef;
delete expectedDefinitions.Date;
delete expectedDefinitions.MinKey;
delete expectedDefinitions.Undefined;
assert.deepStrictEqual(expanded, {
type: 'object',
'x-bsonType': 'object',
required: [],
$defs: RELAXED_EJSON_DEFINITIONS,
$defs: expectedDefinitions,
properties: {
_id: {
$ref: '#/$defs/ObjectId',
Expand Down Expand Up @@ -593,12 +602,16 @@ describe('internalSchemaToExpanded', async function() {
}
]
};
const standard = await internalSchemaToExpanded(internal);
assert.deepStrictEqual(standard, {
const convertor = new InternalToExpandedConvertor();
const expanded = await convertor.convert(internal);
const expectedDefinitions = {
Double: RELAXED_EJSON_DEFINITIONS.Double
};
assert.deepStrictEqual(expanded, {
type: 'object',
'x-bsonType': 'object',
required: ['author'],
$defs: RELAXED_EJSON_DEFINITIONS,
$defs: expectedDefinitions,
properties: {
author: {
type: 'object',
Expand Down Expand Up @@ -704,12 +717,13 @@ describe('internalSchemaToExpanded', async function() {
}
]
};
const standard = await internalSchemaToExpanded(internal);
assert.deepStrictEqual(standard, {
const convertor = new InternalToExpandedConvertor();
const expanded = await convertor.convert(internal);
assert.deepStrictEqual(expanded, {
type: 'object',
'x-bsonType': 'object',
required: [],
$defs: RELAXED_EJSON_DEFINITIONS,
$defs: {},
properties: {
genres: {
type: 'array',
Expand Down Expand Up @@ -867,12 +881,13 @@ describe('internalSchemaToExpanded', async function() {
}
]
};
const standard = await internalSchemaToExpanded(internal);
assert.deepStrictEqual(standard, {
const convertor = new InternalToExpandedConvertor();
const expanded = await convertor.convert(internal);
assert.deepStrictEqual(expanded, {
type: 'object',
'x-bsonType': 'object',
required: [],
$defs: RELAXED_EJSON_DEFINITIONS,
$defs: {},
properties: {
genres: {
type: 'array',
Expand Down Expand Up @@ -1002,12 +1017,13 @@ describe('internalSchemaToExpanded', async function() {
}
]
};
const standard = await internalSchemaToExpanded(internal);
assert.deepStrictEqual(standard, {
const convertor = new InternalToExpandedConvertor();
const expanded = await convertor.convert(internal);
assert.deepStrictEqual(expanded, {
type: 'object',
'x-bsonType': 'object',
required: ['arrayMixedType'],
$defs: RELAXED_EJSON_DEFINITIONS,
$defs: {},
properties: {
arrayMixedType: {
type: 'array',
Expand Down Expand Up @@ -1111,12 +1127,13 @@ describe('internalSchemaToExpanded', async function() {
}
]
};
const standard = await internalSchemaToExpanded(internal);
assert.deepStrictEqual(standard, {
const convertor = new InternalToExpandedConvertor();
const expanded = await convertor.convert(internal);
assert.deepStrictEqual(expanded, {
type: 'object',
'x-bsonType': 'object',
required: [],
$defs: RELAXED_EJSON_DEFINITIONS,
$defs: {},
properties: {
mixedType: {
'x-metadata': {
Expand Down Expand Up @@ -1252,12 +1269,13 @@ describe('internalSchemaToExpanded', async function() {
}
]
};
const standard = await internalSchemaToExpanded(internal);
assert.deepStrictEqual(standard, {
const convertor = new InternalToExpandedConvertor();
const expanded = await convertor.convert(internal);
assert.deepStrictEqual(expanded, {
type: 'object',
'x-bsonType': 'object',
required: [],
$defs: RELAXED_EJSON_DEFINITIONS,
$defs: {},
properties: {
mixedComplexType: {
'x-metadata': {
Expand Down Expand Up @@ -1360,12 +1378,16 @@ describe('internalSchemaToExpanded', async function() {
}
]
};
const standard = await internalSchemaToExpanded(internal);
assert.deepStrictEqual(standard, {
const convertor = new InternalToExpandedConvertor();
const expanded = await convertor.convert(internal);
const expectedDefinitions = {
ObjectId: RELAXED_EJSON_DEFINITIONS.ObjectId
};
assert.deepStrictEqual(expanded, {
type: 'object',
'x-bsonType': 'object',
required: ['mixedType'],
$defs: RELAXED_EJSON_DEFINITIONS,
$defs: expectedDefinitions,
properties: {
mixedType: {
'x-metadata': {
Expand Down Expand Up @@ -1507,7 +1529,8 @@ describe('internalSchemaToExpanded', async function() {
]
};
const abortController = new AbortController();
const promise = internalSchemaToExpanded(internal, { signal: abortController.signal });
const convertor = new InternalToExpandedConvertor();
const promise = convertor.convert(internal, { signal: abortController.signal });
abortController.abort(new Error('Too long, didn\'t wait.'));
await assert.rejects(promise, {
name: 'Error',
Expand Down
Loading
Loading