Skip to content

Commit 546941c

Browse files
committed
add tests and fix
1 parent 40ac8ff commit 546941c

File tree

3 files changed

+83
-19
lines changed

3 files changed

+83
-19
lines changed

src/schema-convertors/internalToMongoDB.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ async function parseType(type: SchemaType, signal?: AbortSignal): Promise<MongoD
6464
return schema;
6565
}
6666

67+
function isSimpleTypesOnly(types: MongoDBJSONSchema[]): types is { bsonType: string }[] {
68+
return types.every(definition => !!definition.bsonType && Object.keys(definition).length === 1);
69+
}
70+
6771
async function parseTypes(types: SchemaType[], signal?: AbortSignal): Promise<MongoDBJSONSchema> {
6872
await allowAbort(signal);
6973
const definedTypes = types.filter(type => type.bsonType.toLowerCase() !== 'undefined');
@@ -72,13 +76,13 @@ async function parseTypes(types: SchemaType[], signal?: AbortSignal): Promise<Mo
7276
return parseType(definedTypes[0], signal);
7377
}
7478
const parsedTypes = await Promise.all(definedTypes.map(type => parseType(type, signal)));
75-
if (definedTypes.some(type => ['Document', 'Array'].includes(type.bsonType))) {
79+
if (isSimpleTypesOnly(parsedTypes)) {
7680
return {
77-
anyOf: parsedTypes
81+
bsonType: parsedTypes.map(({ bsonType }) => bsonType)
7882
};
7983
}
8084
return {
81-
bsonType: definedTypes.map((type) => convertInternalType(type.bsonType))
85+
anyOf: parsedTypes
8286
};
8387
}
8488

src/schema-convertors/internalToStandard.test.ts

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import internalSchemaToStandard, { RELAXED_EJSON_DEFINITIONS } from './internalT
33

44
describe('internalSchemaToStandard', async function() {
55
describe('Converts: ', async function() {
6-
it.only('all the types', async function() {
6+
it('all the types', async function() {
77
const internal = {
88
count: 1,
99
fields: [
@@ -893,7 +893,6 @@ describe('internalSchemaToStandard', async function() {
893893
]
894894
};
895895
const standard = await internalSchemaToStandard(internal);
896-
console.log(JSON.stringify(standard));
897896
assert.deepStrictEqual(standard, {
898897
type: 'object',
899898
required: [],
@@ -986,7 +985,8 @@ describe('internalSchemaToStandard', async function() {
986985
key: {
987986
type: 'string'
988987
}
989-
}
988+
},
989+
required: []
990990
},
991991
objectId: {
992992
$ref: '#/$defs/ObjectId'
@@ -1525,7 +1525,7 @@ describe('internalSchemaToStandard', async function() {
15251525
});
15261526
});
15271527

1528-
it('complex mixed type', async function() {
1528+
it('complex mixed type (with array and object)', async function() {
15291529
const internal = {
15301530
count: 2,
15311531
fields: [
@@ -1657,6 +1657,72 @@ describe('internalSchemaToStandard', async function() {
16571657
}
16581658
});
16591659
});
1660+
1661+
it('complex mixed type (with $refs)', async function() {
1662+
const internal = {
1663+
count: 2,
1664+
fields: [
1665+
{
1666+
name: 'mixedType',
1667+
path: [
1668+
'mixedType'
1669+
],
1670+
count: 2,
1671+
type: [
1672+
'String',
1673+
'ObjectId'
1674+
],
1675+
probability: 1,
1676+
hasDuplicates: false,
1677+
types: [
1678+
{
1679+
name: 'String',
1680+
path: [
1681+
'mixedType'
1682+
],
1683+
count: 1,
1684+
probability: 0.3333333333333333,
1685+
unique: 1,
1686+
hasDuplicates: false,
1687+
values: [
1688+
'abc'
1689+
],
1690+
bsonType: 'String'
1691+
},
1692+
{
1693+
name: 'ObjectId',
1694+
path: [
1695+
'objectId'
1696+
],
1697+
count: 1,
1698+
probability: 0.8,
1699+
unique: 1,
1700+
hasDuplicates: false,
1701+
values: [
1702+
'642d766c7300158b1f22e975'
1703+
],
1704+
bsonType: 'ObjectId'
1705+
}
1706+
]
1707+
}
1708+
]
1709+
};
1710+
const standard = await internalSchemaToStandard(internal);
1711+
assert.deepStrictEqual(standard, {
1712+
type: 'object',
1713+
required: ['mixedType'],
1714+
$defs: RELAXED_EJSON_DEFINITIONS,
1715+
properties: {
1716+
mixedType: {
1717+
anyOf: [{
1718+
type: 'string'
1719+
}, {
1720+
$ref: '#/$defs/ObjectId'
1721+
}]
1722+
}
1723+
}
1724+
});
1725+
});
16601726
});
16611727

16621728
it('can be aborted', async function() {

src/schema-convertors/internalToStandard.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export const RELAXED_EJSON_DEFINITIONS = Object.freeze({
241241
});
242242

243243
const convertInternalType = (internalType: string) => {
244-
const type = InternalTypeToStandardTypeMap[internalType];
244+
const type = { ...InternalTypeToStandardTypeMap[internalType] };
245245
if (!type) throw new Error(`Encountered unknown type: ${internalType}`);
246246
return type;
247247
};
@@ -272,8 +272,8 @@ async function parseType(type: SchemaType, signal?: AbortSignal): Promise<Standa
272272
return schema;
273273
}
274274

275-
function isSimpleTypesOnly(types: StandardTypeDefinition[]): types is { type: JSONSchema4TypeName }[] {
276-
return types.every(definition => !definition.$ref);
275+
function isSimpleTypesOnly(types: StandardJSONSchema[]): types is { type: JSONSchema4TypeName }[] {
276+
return types.every(definition => !!definition.type && Object.keys(definition).length === 1);
277277
}
278278

279279
async function parseTypes(types: SchemaType[], signal?: AbortSignal): Promise<StandardJSONSchema> {
@@ -284,19 +284,13 @@ async function parseTypes(types: SchemaType[], signal?: AbortSignal): Promise<St
284284
return parseType(definedTypes[0], signal);
285285
}
286286
const parsedTypes = await Promise.all(definedTypes.map(type => parseType(type, signal)));
287-
if (definedTypes.some(type => ['Document', 'Array'].includes(type.bsonType))) {
287+
if (isSimpleTypesOnly(parsedTypes)) {
288288
return {
289-
anyOf: parsedTypes
290-
};
291-
}
292-
const convertedTypes = definedTypes.map((type) => convertInternalType(type.bsonType));
293-
if (isSimpleTypesOnly(convertedTypes)) {
294-
return {
295-
type: convertedTypes.map(({ type }) => type)
289+
type: parsedTypes.map(({ type }) => type)
296290
};
297291
}
298292
return {
299-
anyOf: convertedTypes
293+
anyOf: parsedTypes
300294
};
301295
}
302296

0 commit comments

Comments
 (0)